当前位置: 首页 > news >正文

让ComboBox显示图片--PictureComboBox

WinForm程序,让ComboBox的项显示为图片和文字来提高用户体验。

主要实现方式为重写ComboBox的OnDrawItem方法,自己进行ComboBox项的绘制。

效果图:

实现步骤如下:

1.写一个类ComboBoxEx继承自ComboBox 

 

2.在ComboBoxEx构造函数中添加默认属性

View Code
1       public ComboBoxEx()
2         {
3             DrawMode = DrawMode.OwnerDrawFixed;
4             DropDownStyle = ComboBoxStyle.DropDownList;
5             ItemHeight = 50;
6             Width = 200;
7          }

3.在ComboBoxEx类中添加内部类ItemEx

View Code
public class ItemEx
        {
            public ItemEx(string text, Image img)
            {
                Text = text;
                Image = img;
            }

            public string Text { get; set; }
            public Image Image { get; set; }

            public override string ToString()
            {
                return Text;
            }
        }

4.重写ComboBox的OnDrawItem方法

  a.画鼠标选中时的边框,使选项有选中的状态以区别未选中的项

View Code
            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);
            }

  b.绘制图片

View Code
//获得项图片,绘制图片
            ItemEx item = (ItemEx)Items[e.Index];
            Image img = item.Image;

            //图片绘制的区域
            Rectangle imgRect = new Rectangle(6, e.Bounds.Y + 3, 45, 45);
            e.Graphics.DrawImage(img, imgRect);

  c.绘制文本

View Code
            //文本内容显示区域
            Rectangle textRect =
                    new Rectangle(imgRect.Right + 2, imgRect.Y, e.Bounds.Width - imgRect.Width, e.Bounds.Height - 2);

            //获得项文本内容,绘制文本
            String itemText = Items[e.Index].ToString();

            //文本格式垂直居中
            StringFormat strFormat = new StringFormat();
            strFormat.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(itemText, new Font("微软雅黑", 12), Brushes.Black, textRect, strFormat);

至此PictureComboBox已完成!

整个代码如下:

View Code
    public class ComboBoxEx : ComboBox
    {
        public ComboBoxEx()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
            DropDownStyle = ComboBoxStyle.DropDownList;
            ItemHeight = 50;
            Width = 200;
         }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (Items.Count == 0||e.Index==-1)
                return;
            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);
            }

            //获得项图片,绘制图片
            ItemEx item = (ItemEx)Items[e.Index];
            Image img = item.Image;

            //图片绘制的区域
            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 = Items[e.Index].ToString();

            //文本格式垂直居中
            StringFormat strFormat = new StringFormat();
            strFormat.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(itemText, new Font("微软雅黑", 12), Brushes.Black, textRect, strFormat);
            base.OnDrawItem(e);
        }
        /// <summary>
        /// 内部类
        /// </summary>
        public class ItemEx
        {
            public ItemEx(string text, Image img)
            {
                Text = text;
                Image = img;
            }

            public string Text { get; set; }
            public Image Image { get; set; }

            public override string ToString()
            {
                return Text;
            }
        }
    }

5.测试

View Code
        public Form1()
        {
            InitializeComponent();
            comboBoxEx1.Items.Add(new PictureComboBox.ComboBoxEx.ItemEx("000000", Image.FromFile(Application.StartupPath + "\\0.gif")));
            comboBoxEx1.Items.Add(new PictureComboBox.ComboBoxEx.ItemEx("111111", Image.FromFile(Application.StartupPath + "\\1.gif")));
            comboBoxEx1.Items.Add(new PictureComboBox.ComboBoxEx.ItemEx("222222", Image.FromFile(Application.StartupPath + "\\2.gif")));
            comboBoxEx1.Items.Add(new PictureComboBox.ComboBoxEx.ItemEx("333333", Image.FromFile(Application.StartupPath + "\\3.gif")));
        }

 

转载请注明出处:http://www.cnblogs.com/refactor/

转载于:https://www.cnblogs.com/refactor/archive/2012/09/06/2672463.html

相关文章:

  • 多线程丢失更新、加锁
  • ds存储上增加lun的容量,aix下的相应卷组大小怎么自动增加
  • 如何利用ccform自定义表单来增加自己的控件,关于Sys_FrmEle表结构与数据存储设计?...
  • id和class的区别
  • 字符串模板匹配
  • linux Perforce 使用
  • Android Activity生命周期
  • 敏捷结果30天之第十一天:高效能、慢生活
  • C++:复制构造函数在什么时候被调用?
  • js模拟hashtable
  • 取消锚(a/)点击后页面跳转的几种方法
  • 程序员面试题100题第26题——和为n连续正数序列
  • 软考 (一) 感触
  • 第二十三模板 15模板和友元
  • Android之loader
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • 【剑指offer】让抽象问题具体化
  • JavaScript学习总结——原型
  • nodejs:开发并发布一个nodejs包
  • vue自定义指令实现v-tap插件
  • 半理解系列--Promise的进化史
  • 从PHP迁移至Golang - 基础篇
  • 分享一份非常强势的Android面试题
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 检测对象或数组
  • 七牛云假注销小指南
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 前端学习笔记之观察者模式
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 前端自动化解决方案
  • 阿里云API、SDK和CLI应用实践方案
  • 第二十章:异步和文件I/O.(二十三)
  • 如何正确理解,内页权重高于首页?
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • ![CDATA[ ]] 是什么东东
  • #NOIP 2014# day.1 T2 联合权值
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (蓝桥杯每日一题)love
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • (三)elasticsearch 源码之启动流程分析
  • (四)linux文件内容查看
  • (译)2019年前端性能优化清单 — 下篇
  • (转)德国人的记事本
  • (转载)OpenStack Hacker养成指南
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法
  • .md即markdown文件的基本常用编写语法
  • .Net Core和.Net Standard直观理解
  • .NET Core引入性能分析引导优化