当前位置:编程学习 > C#/ASP.NET >>

[C#]简单自绘带图片的 ComboBox

先看看效果:

 

 

 


\  \

\

 

先构造一个子项的类:

 

[csharp] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing;// 
 
namespace ComboBox_Draw 

    //自定义组合框项 
    class MyItem 
    { 
        //项文本内容 
        private String Text; 
        
        //项图片 
        public Image Img; 
 
        //构造函数 
        public MyItem(String text, Image img) 
        { 
            Text = text; 
            Img = img; 
        } 
 
        //重写ToString函数,返回项文本 
        public override string ToString() 
        { 
            return Text; 
        }  www.zzzyk.com
    } 

 

然后看重写DrawItem事件:

 

[csharp] 
private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e) 
        { 
            //鼠标选中在这个项上 
            if ((e.State & DrawItemState.Selected) != 0) 
            { 
                //渐变画刷 
                LinearGradientBrush brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(255, 251, 237), 
                                                 Color.FromArgb(255, 236, 181), LinearGradientMode.Vertical); 
                //填充区域 
                Rectangle borderRect = new Rectangle(3, e.Bounds.Y, e.Bounds.Width - 5, e.Bounds.Height - 2); 
 
                e.Graphics.FillRectangle(brush, borderRect); 
 
                //画边框 
                Pen pen = new Pen(Color.FromArgb(229, 195, 101)); 
                e.Graphics.DrawRectangle(pen, borderRect); 
            } 
            else 
            { 
                SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255)); 
                e.Graphics.FillRectangle(brush, e.Bounds); 
            } 
 
            //获得项图片,绘制图片 
            MyItem item = (MyItem)comboBox1.Items[e.Index]; 
            Image img = item.Img; 
            
            //图片绘制的区域 
            Rectangle imgRect = new Rectangle(6, e.Bounds.Y + 3, 45,45); 
            e.Graphics.DrawImage(img, imgRect); 
             
            //文本内容显示区域 
            Rectangle textRect = 
                    new Rectangle(imgRect.Right + 2, imgRect.Y, e.Bounds.Width - imgRect.Width, e.Bounds.Height - 2); 
             
            //获得项文本内容,绘制文本 
            String itemText = comboBox1.Items[e.Index].ToString(); 
 
            //文本格式垂直居中 
            StringFormat strFormat = new StringFormat(); 
            strFormat.LineAlignment = StringAlignment.Center; 
            e.Graphics.DrawString(itemText, new Font("微软雅黑", 12), Brushes.Black, textRect, strFormat); 
        } 
 


最后是窗体Load事件:最后是窗体Load事件:

 

[csharp] 
private void Form1_Load(object sender, EventArgs e) 

    //添加项 
    comboBox1.Items.Add(new MyItem("000000", Image.FromFile(Application.StartupPath + "\\0.gif"))); 
    comboBox1.Items.Add(new MyItem("111111", Image.FromFile(Application.StartupPath + "\\1.gif"))); 
    comboBox1.Items.Add(new MyItem("222222", Image.FromFile(Application.StartupPath + "\\2.gif"))); 
 &nb

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,