响应ComboBox.DrawItem事件,该事件的DrawItemEventArgs参数提供以下几个有价值的对象:
Index 所要绘制的项的索引
Graphics GDI+的主对象,用他的Graphics.DrawImage方法在你希望的位置上绘制图象。
Bounds 所要进行绘制的区域。
DrawItemEventArgs还有其他几个对象请参考SDK,有字体和背景色什么的。
Index 所要绘制的项的索引
Graphics GDI+的主对象,用他的Graphics.DrawImage方法在你希望的位置上绘制图象。
Bounds 所要进行绘制的区域。
DrawItemEventArgs还有其他几个对象请参考SDK,有字体和背景色什么的。
解决方案 »
- [急急急]GridView中设置Hyperlink问题
- .net 2.0中 2个集合循环,寻找高效率方法..
- 在C#中如何获得textbox中字符串的长度
- 请教: 要实现一个office或者vs IDE 中的redo,undo弹出列表的式样,请问该怎么实现。(数据已经存在。)
- DataSet 转换为string
- 一个很有意义的计算题!
- 没有分了,抱歉啊,但是问题还得问啊,关于调用一个事件的
- 问一个类型转换的简单问题!
- C#调用C写的DLL,如何传递结构指针?
- C# 怎么将文件转换成数组之类的,并在数组末尾+1,使文件加密让别人打不开,解密时再减去1让文件还原?
- 俞悦出生就能c,是否杂交?
- 请教!Socket问题!急~~~~~
兄弟能不能给各实例呀,先谢了
请参考这篇文章
如果DropDownStyle 是 DropDownList,代码这样写:private void DatabaseList_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{ Rectangle textRect = new Rectangle(this.SmallDBIcon.Width + e.Bounds.X , e.Bounds.Y+1, e.Bounds.Width-4, e.Font.Height);
//SolidBrush textBrush = new SolidBrush(SystemColors.WindowText); e.Graphics.DrawIcon(this.SmallDBIcon, e.Bounds.X, e.Bounds.Y);
if(e.Index >= 0)
{
e.Graphics.DrawString(this.DatabaseList.Items[e.Index].ToString(), e.Font, SystemBrushes.WindowText, textRect, StringFormat.GenericDefault);
}
}如果DropDownStyle是DropDown,就把if判断去掉
private void DatabaseList_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Rectangle textRect = new Rectangle(this.SmallDBIcon.Width + e.Bounds.X , e.Bounds.Y+1, e.Bounds.Width-4, e.Font.Height);
//SolidBrush textBrush = new SolidBrush(SystemColors.WindowText);//下面这句是画图的语句
e.Graphics.DrawIcon(this.SmallDBIcon, e.Bounds.X, e.Bounds.Y); e.Graphics.DrawString(this.DatabaseList.Items[e.Index].ToString(), e.Font, SystemBrushes.WindowText, textRect, StringFormat.GenericDefault);
}
{
this.comboBox1.Items.Add(" one");
this.comboBox1.Items.Add(" two");
this.comboBox1.Items.Add(" three");
this.comboBox1.Items.Add(" four");
this.comboBox1.Items.Add(" five");
this.comboBox1.Text=this.comboBox1.Items[0].ToString(); this.comboBox1.DrawMode=System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.comboBox1.DrawItem+=new DrawItemEventHandler(comboBox1_DrawItem);
this.comboBox1.MeasureItem+=new MeasureItemEventHandler(comboBox1_MeasureItem); } private void comboBox1_MeasureItem(object sender,System.Windows.Forms.MeasureItemEventArgs e)
{
e.ItemHeight=40;
e.ItemWidth=100; } private void comboBox1_DrawItem(object sender,System.Windows.Forms.DrawItemEventArgs e)
{
//图标
Icon []myicon=new Icon[5];
myicon[0]=new Icon("f:\\1.ico");
myicon[1]=new Icon("f:\\2.ico");
myicon[2]=new Icon("f:\\3.ico");
myicon[3]=new Icon("f:\\4.ico");
myicon[4]=new Icon("f:\\5.ico");
//设置绘图环境
Graphics g=e.Graphics;
//设置字体
Font myfont=new Font("宋体",15,System.Drawing.FontStyle.Bold|FontStyle.Italic);
Brush mybrush=System.Drawing.Brushes.Red;
e.DrawFocusRectangle();
e.DrawBackground();
//绘图
g.DrawIcon(myicon[e.Index],e.Bounds.X,e.Bounds.Y);//每一个item项
g.DrawString(this.comboBox1.Items[e.Index].ToString(),myfont,mybrush,50,0);
}