想到做到这个效果:
这个图片是在http://bbs.csdn.net/topics/390752796这个帖子中看到的
------解决思路----------------------
标准的办法就是重绘~
------解决思路----------------------
用WPF的Combobox 重写 item的模板 ,非常容易都实现了。干嘛研究GDI+
------解决思路----------------------
有点小麻烦,给你代码自己研究吧,代码没具体优化,网上借的,已经调试没问题,无非就是重构下子项(包含的图片、文字等信息),自定义的下拉框控件中重绘下拉列表项,想怎么画就怎么画,GDI,图片都成,参照你给的那个网址内容,鼠标移动上去的时候还可以在变换下悬浮样式(坐标控制,会有点麻烦)
代码用的是imagelist,可以换成iamge熟悉直接传入,未实现画叉的部分,原来一样,按现在的绘制方式绘制到右边就OK了
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestWeldSymbolUI
{
public class ImageCombo : ComboBox
{
private ImageList imgs = new ImageList();
// constructor
public ImageCombo()
{
// set draw mode to owner draw
this.DrawMode = DrawMode.OwnerDrawFixed;
}
// ImageList property
public ImageList ImageList
{
get
{
return imgs;
}
set
{
imgs = value;
}
}
// customized drawing process
protected override void OnDrawItem(DrawItemEventArgs e)
{
// draw background & focus rect
e.DrawBackground();
e.DrawFocusRectangle();
// check if it is an item from the Items collection
if (e.Index < 0)
// not an item, draw the text (indented)
e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
else
{
// check if item is an ImageComboItem
if (this.Items[e.Index].GetType() == typeof(ImageComboItem))
{
// get item to draw
ImageComboItem item = (ImageComboItem) this.Items[e.Index];
// get forecolor & font
Color forecolor = (item.ForeColor != Color.FromKnownColor(KnownColor.Transparent)) ? item.ForeColor : e.ForeColor;
Font font = item.Mark ? new Font(e.Font, FontStyle.Bold) : e.Font;
// -1: no image
if (item.ImageIndex != -1)
{
// draw image, then draw text next to it
this.ImageList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, item.ImageIndex);
e.Graphics.DrawString(item.Text, font, new SolidBrush(forecolor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
}
else
// draw text (indented)
e.Graphics.DrawString(item.Text, font, new SolidBrush(forecolor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
}
else
// it is not an ImageComboItem, draw it
e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top);
}
base.OnDrawItem (e);
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TestWeldSymbolUI
{
public class ImageComboItem : object
{
// forecolor: transparent = inherit
private Color forecolor = Color.FromKnownColor(KnownColor.Transparent);
private bool mark = false;
private int imageindex = -1;
private object tag = null;
private string text = null;
// constructors
public ImageComboItem()
{
}
public ImageComboItem(string Text)
{
text = Text;
}
public ImageComboItem(string Text, int ImageIndex)
{
text = Text;
imageindex = ImageIndex;
}
public ImageComboItem(string Text, int ImageIndex, bool Mark)
{
text = Text;
imageindex = ImageIndex;
mark = Mark;
}
public ImageComboItem(string Text, int ImageIndex, bool Mark, Color ForeColor)
{
text = Text;
imageindex = ImageIndex;
mark = Mark;
forecolor = ForeColor;
}
public ImageComboItem(string Text, int ImageIndex, bool Mark, Color ForeColor, object Tag)
{
text = Text;
imageindex = ImageIndex;
mark = Mark;
forecolor = ForeColor;
tag = Tag;
}
// forecolor
public Color ForeColor
{
get
{
return forecolor;
}
set
{
forecolor = value;
}
}
// image index
public int ImageIndex
{
get
{
return imageindex;
}
set
{
imageindex = value;
}
}
// mark (bold)
public bool Mark
{
get
{
return mark;
}
set
{
mark = value;
}
}
// tag
public object Tag
{
get
{
return tag;
}
set
{
tag = value;
}
}
// item text
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
// ToString() should return item text
public override string ToString()
{
return text;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
TestWeldSymbolUI.ImageComboItem a = new TestWeldSymbolUI.ImageComboItem();
a.Text = "adsdff";
a.ImageIndex = 1;
a.Mark = true;
a.ForeColor = Color.Beige;
a.Tag = "aaaaaaaa";
imageCombo1.Items.Add(a);
}
catch
{
}
}
}
}
------解决思路----------------------
关键是删除功能怎么实现
------解决思路----------------------
如果不会WPF,也不会GDI+,那么你把那么多删除按钮合并成一个就好了,当前选择哪一个,点击删除就删掉哪一个