comboBox with images

先看效果图:


实现步骤如下:


添加MyItem类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;//

namespace rePaint
{
    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;
        }
    }
}

在主程序中:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D; 

namespace rePaint
{
    public partial class Form1 : Form
    {
        private int itemHeight=30;
        public Form1()
        {
            InitializeComponent();
        }

        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")));
            comboBox1.Items.Add(new MyItem("333333", Image.FromFile(Application.StartupPath + @"\3.gif")));

            //默认选中项索引
            comboBox1.SelectedIndex = 0;//不能删除

            //自绘组合框需要设置的一些属性
            comboBox1.DrawMode = DrawMode.OwnerDrawFixed;//不能设置为:DrawMode.OwnerDrawVariable
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

            comboBox1.ItemHeight = itemHeight;

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

        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 - 4, 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(2, e.Bounds.Y + 1, itemHeight - 2, itemHeight - 2);//(x,y,width,height)
            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);
        } 
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值