美化combox 用法 —— 为其添加图片,改变显示方式

对C# 中Combox控件的一些简单的操作,改变其显示特性,主要是改变其中的项的属性

我们需要注意的是:每次为Combox添加项的时候,就会触发DrawItem事件,而我们正是通过DrawItem来改变Combox的显示特性的

 

 

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Collections;  
  10.   
  11. namespace Combox_Image  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         ArrayList brushArray = new ArrayList() ;  
  16.         ArrayList fontArray = new ArrayList() ;  
  17.   
  18.         public Form1()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.   
  23.         private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)  
  24.         {  
  25.             //确定画布  
  26.             Graphics g = e.Graphics;  
  27.             //绘制区域  
  28.             Rectangle r = e.Bounds;  
  29.             Font fn = null;  
  30.             if (e.Index >= 0)  
  31.             {  
  32.                 //设置字体、字符串格式、对齐方式  
  33.                 fn = (Font)fontArray[e.Index];  
  34.                 string s = (string)comboBox1.Items[e.Index];  
  35.                 StringFormat sf = new StringFormat();  
  36.                 sf.Alignment = StringAlignment.Near;  
  37.                 //根据不同的状态用不同的颜色表示  
  38.                 if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))  
  39.                 {  
  40.                     e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);  
  41.                     e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);  
  42.                     e.DrawFocusRectangle();  
  43.                 }  
  44.                 else  
  45.                 {  
  46.                     e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);  
  47.                     e.Graphics.DrawString(s, fn, new SolidBrush(Color.Red), r, sf);  
  48.                     e.DrawFocusRectangle();  
  49.                 }  
  50.             }  
  51.   
  52.         }  
  53.   
  54.         private void Form1_Load(object sender, EventArgs e)  
  55.         {  
  56.             fontArray.Add(new Font("Ariel", 8, FontStyle.Bold));  
  57.             fontArray.Add(new Font("Courier", 8, FontStyle.Italic));  
  58.             fontArray.Add(new Font("Veranda", 8, FontStyle.Bold));  
  59.             fontArray.Add(new Font("System", 8, FontStyle.Strikeout));  
  60.             fontArray.Add(new Font("Century SchoolBook", 8, FontStyle.Underline));  
  61.             fontArray.Add(new Font("Helevctia", 8, FontStyle.Italic));  
  62.             //创建画刷  
  63.             brushArray.Add(new SolidBrush(Color.Red));  
  64.             brushArray.Add(new SolidBrush(Color.Blue));  
  65.             brushArray.Add(new SolidBrush(Color.Green));  
  66.             brushArray.Add(new SolidBrush(Color.Yellow));  
  67.             brushArray.Add(new SolidBrush(Color.Black));  
  68.             brushArray.Add(new SolidBrush(Color.Azure));  
  69.             brushArray.Add(new SolidBrush(Color.Firebrick));  
  70.             brushArray.Add(new SolidBrush(Color.DarkMagenta));  
  71.             brushArray.Add(new SolidBrush(Color.DarkTurquoise));  
  72.             brushArray.Add(new SolidBrush(Color.Khaki));  
  73.             //画comboBox1,注意它要调用comboBox1_DrawItem来画  
  74.             comboBox1.Items.Add("中国");  
  75.             comboBox1.Items.Add("巴西");  
  76.             comboBox1.Items.Add("哥斯达黎加");  
  77.             comboBox1.Items.Add("土耳其");  
  78.             comboBox1.Items.Add("韩国");  
  79.             comboBox1.Items.Add("日本");  
  80.             //画comboBox2,注意它要调用comboBox2_DrawItem来画  
  81.             comboBox2.Items.Add("");  
  82.             comboBox2.Items.Add("");  
  83.             comboBox2.Items.Add("");  
  84.             comboBox2.Items.Add("");  
  85.             comboBox2.Items.Add("");  
  86.             comboBox2.Items.Add("");  
  87.             comboBox2.Items.Add("");  
  88.             comboBox2.Items.Add("");  
  89.             comboBox2.Items.Add("");  
  90.             comboBox2.Items.Add("");  
  91.             //画comboBox3,注意它要调用comboBox3_DrawItem来画  
  92.             comboBox3.Items.Add("赵微");  
  93.             comboBox3.Items.Add("舒淇");  
  94.             comboBox3.Items.Add("谌豹");  
  95.             comboBox3.Items.Add("郑巧玲");  
  96.         }  
  97.   
  98.         private void comboBox2_DrawItem(object sender, DrawItemEventArgs e)  
  99.         {  
  100.             Graphics g = e.Graphics;  
  101.             Rectangle r = e.Bounds;  
  102.             if (e.Index >= 0)  
  103.             {  
  104.                 //设置字符串前矩形块rd的大小  
  105.                 Rectangle rd = r;  
  106.                 rd.Width = rd.Left + 20;  
  107.                 Rectangle rt = r;  
  108.                 r.X = rd.Right;  
  109.                 //用不同的颜色画矩形块  
  110.                 SolidBrush b = (SolidBrush)brushArray[e.Index];  
  111.                 g.FillRectangle(b, rd);  
  112.                 //设置字符串的格式  
  113.                 StringFormat sf = new StringFormat();  
  114.                 sf.Alignment = StringAlignment.Near;  
  115.                 if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))  
  116.                 {  
  117.                     //字符串背景  
  118.                     e.Graphics.FillRectangle(new SolidBrush(Color.White), r);  
  119.                     //显示字符串  
  120.                     e.Graphics.DrawString(b.Color.Name, new Font("Ariel", 8, FontStyle.Bold), new SolidBrush(Color.Black), r, sf);  
  121.                     //绘制取得焦点时的虚线框  
  122.                     e.DrawFocusRectangle();  
  123.                 }  
  124.                 else  
  125.                 {  
  126.                     e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);  
  127.                     e.Graphics.DrawString(b.Color.Name, new Font("Veranda", 8, FontStyle.Bold), new SolidBrush(Color.Red), r, sf);  
  128.                     e.DrawFocusRectangle();  
  129.                 }  
  130.             }  
  131.   
  132.         }  
  133.   
  134.         private void comboBox3_DrawItem(object sender, DrawItemEventArgs e)  
  135.         {  
  136.             Graphics g = e.Graphics;  
  137.             Rectangle r = e.Bounds;  
  138.             Size imageSize = imageList1.ImageSize;  
  139.             Font fn = null;  
  140.             if (e.Index >= 0)  
  141.             {  
  142.                 fn = (Font)fontArray[0];  
  143.                 string s = (string)comboBox3.Items[e.Index];  
  144.                 StringFormat sf = new StringFormat();  
  145.                 sf.Alignment = StringAlignment.Near;  
  146.                 if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))  
  147.                 {  
  148.                     //画条目背景  
  149.                     e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);  
  150.                     //绘制图像  
  151.                     imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);  
  152.                     //显示字符串  
  153.                     e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);  
  154.                     //显示取得焦点时的虚线框  
  155.                     e.DrawFocusRectangle();  
  156.                 }  
  157.                 else  
  158.                 {  
  159.                     e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);  
  160.                     imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);  
  161.                     e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);  
  162.                     e.DrawFocusRectangle();  
  163.                 }  
  164.             }  
  165.         }  
  166.     }  
  167. }  

 三个Combox的效果如下图所示: 

 

 


原文地址:http://blog.csdn.net/nidexuanzhe/article/details/6264765

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这篇文章中我们重点需要实现的是(3)、(4)两项功能,下面我们来介绍具体实现的方法。 第一步,实现ImageComboBoxItem类。 要实现显示图标,当然要给每个项添加与图标相关的信息了,ImageComboBoxItem类应该包括以下内容:文本(Text)、缩进的级别(Level)、图标的索引(ImageIndex、ImageKey),用户数据(Tag)。ImageComboBoxItem类实现了ISerializable接口,实现自定义序列化。ImageComboBoxItem类的类视图如下: 图3 ImageComboxItem类视图 ImageComboBoxItem类的代码如下: [Serializable] [DefaultProperty("Text")] [TypeConverter( typeof(ExpandableObjectConverter))] public class ImageComboBoxItem : IDisposable, ISerializable ...{ Fields#region Fields private ImageComboBox _imageComboBox; private string _text = "ImageComboBoxItem"; private ImageComboBoxItemImageIndexer _imageIndexer; private object _tag; private int _level; #endregion Constructors#region Constructors public ImageComboBoxItem() ...{ } public ImageComboBoxItem(string text) : this(text, -1, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex) : this(text, imageIndex, 0) ...{ } public ImageComboBoxItem( string text, string imageKey) : this(text, imageKey, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex, int level) : this() ...{ _text = text; ImageIndexer.Index = imageIndex; _level = level; } public ImageComboBoxItem( string text, string imageKey, int level) : this() ...{ _text = text; ImageIndexer.Key = imageKey; _level = level; } protected ImageComboBoxItem( SerializationInfo info, StreamingContext context) : this() ...{ Deserialize(info, context); } #endregion Properties#region Properties [Localizable(true)]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值