精易论坛

标题: [C#] 简单自绘带图片的 ComboBox [打印本页]

作者: isaced    时间: 2012-7-21 14:44
标题: [C#] 简单自绘带图片的 ComboBox
本帖最后由 isaced 于 2012-7-21 14:56 编辑

很高兴看到精益论坛的C#板块慢慢活跃起来,希望大家可以更多的分享处自己的东西和经验。

这是一篇C#自绘控件的教程,抛砖引玉,很简单,技术含量不高,大师请路过。{:soso_e100:}

完了大家可以自己试试,帖子源码和素材都贴上来,欢迎跟帖交流。


发到CSDN论坛上,瞬间被首页推荐了。


效果展示:




不废话了,正文开始。

先构造一个子项的类:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;//

  6. namespace ComboBox_Draw
  7. {
  8.     //自定义组合框项
  9.     class MyItem
  10.     {
  11.         //项文本内容
  12.         private String Text;
  13.       
  14.         //项图片
  15.         public Image Img;

  16.         //构造函数
  17.         public MyItem(String text, Image img)
  18.         {
  19.             Text = text;
  20.             Img = img;
  21.         }

  22.         //重写ToString函数,返回项文本
  23.         public override string ToString()
  24.         {
  25.             return Text;
  26.         }
  27.     }
  28. }
复制代码
然后看重写DrawItem事件:
  1. private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e)
  2.         {
  3.             //鼠标选中在这个项上
  4.             if ((e.State & DrawItemState.Selected) != 0)
  5.             {
  6.                 //渐变画刷
  7.                 LinearGradientBrush brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(255, 251, 237),
  8.                                                  Color.FromArgb(255, 236, 181), LinearGradientMode.Vertical);
  9.                 //填充区域
  10.                 Rectangle borderRect = new Rectangle(3, e.Bounds.Y, e.Bounds.Width - 5, e.Bounds.Height - 2);

  11.                 e.Graphics.FillRectangle(brush, borderRect);

  12.                 //画边框
  13.                 Pen pen = new Pen(Color.FromArgb(229, 195, 101));
  14.                 e.Graphics.DrawRectangle(pen, borderRect);
  15.             }
  16.             else
  17.             {
  18.                 SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255));
  19.                 e.Graphics.FillRectangle(brush, e.Bounds);
  20.             }

  21.             //获得项图片,绘制图片
  22.             MyItem item = (MyItem)comboBox1.Items[e.Index];
  23.             Image img = item.Img;
  24.            
  25.             //图片绘制的区域
  26.             Rectangle imgRect = new Rectangle(6, e.Bounds.Y + 3, 45,45);
  27.             e.Graphics.DrawImage(img, imgRect);
  28.             
  29.             //文本内容显示区域
  30.             Rectangle textRect =
  31.                     new Rectangle(imgRect.Right + 2, imgRect.Y, e.Bounds.Width - imgRect.Width, e.Bounds.Height - 2);
  32.             
  33.             //获得项文本内容,绘制文本
  34.             String itemText = comboBox1.Items[e.Index].ToString();

  35.             //文本格式垂直居中
  36.             StringFormat strFormat = new StringFormat();
  37.             strFormat.LineAlignment = StringAlignment.Center;
  38.             e.Graphics.DrawString(itemText, new Font("微软雅黑", 12), Brushes.Black, textRect, strFormat);
  39.         }
复制代码
最后是窗体Load事件:最后是窗体Load事件:
  1.        private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             //添加项
  4.             comboBox1.Items.Add(new MyItem("000000", Image.FromFile(Application.StartupPath + "\\0.gif")));
  5.             comboBox1.Items.Add(new MyItem("111111", Image.FromFile(Application.StartupPath + "\\1.gif")));
  6.             comboBox1.Items.Add(new MyItem("222222", Image.FromFile(Application.StartupPath + "\\2.gif")));
  7.             comboBox1.Items.Add(new MyItem("333333", Image.FromFile(Application.StartupPath + "\\3.gif")));

  8.             //默认选中项索引
  9.             comboBox1.SelectedIndex = 0;

  10.             //自绘组合框需要设置的一些属性
  11.             comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
  12.             comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
  13.             comboBox1.ItemHeight = 50;
  14.             comboBox1.Width = 200;

  15.             //添加DrawItem事件处理函数
  16.             comboBox1.DrawItem += ComboBox1_DrawItem;

  17.         }
复制代码
素材下载

---------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------


{:soso_e183:}祝精益论坛越来越来越来越来越来越来好!{:soso_e183:}





另外,本人倡导注释,所以注释量比较大,方便新手。
对本文有什么问题可以直接回帖,也可以发到我的邮箱:[email protected]

[控件自绘]简单自绘带图片的ComboBox.zip

274.39 KB, 下载次数: 49, 下载积分: 精币 -2 枚

为了满足连代码都懒得Copy的人们,源码打包下载。


作者: 乔伊    时间: 2012-7-21 14:53
这个我喜欢。
作者: 乔伊    时间: 2012-7-21 14:56
转载主题就不要把原创复制过来了,改成转载
作者: isaced    时间: 2012-7-21 14:57
乔伊 发表于 2012-7-21 14:56
转载主题就不要把原创复制过来了,改成转载

知道了,下次直接发精益论坛。
作者: 乔伊    时间: 2012-7-21 14:58
isaced 发表于 2012-7-21 14:57
知道了,下次直接发精益论坛。

。。。。。= =。。。好好学习,天天向上
作者: isaced    时间: 2012-7-21 14:59
乔伊 发表于 2012-7-21 14:58
。。。。。= =。。。好好学习,天天向上

可惜论坛没有代码高亮插件,遗憾了。
作者: 乔伊    时间: 2012-7-21 15:00
isaced 发表于 2012-7-21 14:59
可惜论坛没有代码高亮插件,遗憾了。

嗯的,我看你代码是有背景格式的,这个是用HTML还是DZ代码?代码高亮在弄了,不过不是很完善。所以木有发布。
作者: isaced    时间: 2012-7-21 15:03
乔伊 发表于 2012-7-21 15:00
嗯的,我看你代码是有背景格式的,这个是用HTML还是DZ代码?代码高亮在弄了,不过不是很完善。所以木有发 ...

VS里直接Copy的,然后粘贴为代码。
作者: 乔伊    时间: 2012-7-21 15:06
isaced 发表于 2012-7-21 15:03
VS里直接Copy的,然后粘贴为代码。

粘贴为代码?
作者: isaced    时间: 2012-7-21 15:12
乔伊 发表于 2012-7-21 15:06
粘贴为代码?



作者: 乔伊    时间: 2012-7-21 15:13
懂了。好的。
作者: mishicc    时间: 2012-8-14 23:29
这个不错 谢谢的
作者: 乔伊    时间: 2012-8-14 23:40
楼主的我用了,很不错嘿嘿= =
作者: isaced    时间: 2012-8-15 19:06
乔伊 发表于 2012-8-14 23:40
楼主的我用了,很不错嘿嘿= =

真的么。
作者: 乔伊    时间: 2012-8-15 20:28
isaced 发表于 2012-8-15 19:06
真的么。

嗯,我在尝试把listbox改了。。因为我需要自会列表,根据你的这个方法正在尝试
作者: isaced    时间: 2012-8-15 21:15
乔伊 发表于 2012-8-15 20:28
嗯,我在尝试把listbox改了。。因为我需要自会列表,根据你的这个方法正在尝试

哦,祝你成功!
作者: 我是盼盼哟    时间: 2013-2-26 16:57
哇哦,LZ好手法。
作者: 微软粉    时间: 2013-2-27 21:17
这个我喜欢。


本文章来自:精易论坛(http://125.confly.eu.org/thread-115225-1-1.html)请保留此链接, 感谢!
作者: no1234    时间: 2013-4-20 11:09
好好,厉害,重绘一点也不会
作者: yinghao2005    时间: 2014-5-19 02:11
支持顶 顶
作者: venway    时间: 2014-7-9 13:45
感谢分享 学习了
作者: lcj21    时间: 2018-3-31 21:59
支持一下,谢谢分享!
作者: s8198587    时间: 2024-9-22 09:28





欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4