扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:张伟 来源:yesky 2007年11月14日
关键字:
private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { //确定画布 Graphics g = e.Graphics ; //绘制区域 Rectangle r = e.Bounds ; Font fn = null ; if ( e.Index >= 0 ) { //设置字体、字符串格式、对齐方式 fn = (Font)fontArray[e.Index]; string s = (string)comboBox1.Items[e.Index]; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; //根据不同的状态用不同的颜色表示 if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { e.Graphics.FillRectangle(new SolidBrush(Color.Red) , r); e.Graphics.DrawString( s , fn , new SolidBrush(Color.Black), r ,sf); e.DrawFocusRectangle(); } else { e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r); e.Graphics.DrawString( s , fn , new SolidBrush(Color.Red), r ,sf); e.DrawFocusRectangle(); } } } |
再来看看comboBox2的DrawItem事件处理函数,其代码如下:
private void comboBox2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { Graphics g = e.Graphics ; Rectangle r = e.Bounds ; if ( e.Index >= 0 ) { //设置字符串前矩形块rd的大小 Rectangle rd = r ; rd.Width = rd.Left + 20 ; Rectangle rt = r ; r.X = rd.Right ; //用不同的颜色画矩形块 SolidBrush b = (SolidBrush)brushArray[e.Index]; g.FillRectangle(b , rd); //设置字符串的格式 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { //字符串背景 e.Graphics.FillRectangle(new SolidBrush(Color.White) , r); //显示字符串 e.Graphics.DrawString( b.Color.Name, new Font("Ariel" ,8 , FontStyle.Bold ) , new SolidBrush(Color.Black), r ,sf); //绘制取得焦点时的虚线框 e.DrawFocusRectangle(); } else { e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r); e.Graphics.DrawString( b.Color.Name, new Font("Veranda" , 8 , FontStyle.Bold ) , new SolidBrush(Color.Red), r ,sf); e.DrawFocusRectangle(); } } } |
最后我们看看comboBox3的DrawItem事件处理函数,其源代码如下:
private void comboBox3_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { Graphics g = e.Graphics ; Rectangle r = e.Bounds ; Size imageSize = imageList1.ImageSize; Font fn = null ; if ( e.Index >= 0 ) { fn = (Font)fontArray[0]; string s = (string)comboBox3.Items[e.Index]; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { //画条目背景 e.Graphics.FillRectangle(new SolidBrush(Color.Red) , r); //绘制图像 imageList1.Draw(e.Graphics, r.Left, r.Top,e.Index); //显示字符串 e.Graphics.DrawString( s , fn , new SolidBrush(Color.Black), r.Left+imageSize.Width ,r.Top); //显示取得焦点时的虚线框 e.DrawFocusRectangle(); } else { e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r); imageList1.Draw(e.Graphics, r.Left, r.Top,e.Index); e.Graphics.DrawString( s , fn , new SolidBrush(Color.Black),r.Left+imageSize.Width ,r.Top); e.DrawFocusRectangle(); } } } |
看到这儿,聪明的读者也许会说,其实为组合框"变脸"很简单,只要修改各个组合框的DrawItem事件处理函数即可。如果你能明白这一点,我这篇文章的目的就达到了。
在本文快要结果之前我们还是来看看应用程序入口函数的代码:
static void Main() { Form frm=new Form1(); frm.ShowDialog(); } |
最后我要指出本文的局限性,那就是本文中组合框的"顺序性"很强,即组合框中条目格式与我们定义的字符串格式数组、画刷数组、图像数组顺序一样,但如果要求顺序不一样,例如在下面的情况下:用红黄绿三种颜色分别代表三个班级的颜色,即一班所学同学的姓名都用红色表示、二班所有同学的姓名都用黄色表示、三班所有同学的姓名都用绿色表示,这又如何实现呢?有些读者可能会想到用if-else-if语句,但是如果班级有10、100个甚至1000个你还用if-else-if语句吗?这个问题我们留在下一篇文章中解决。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。