C#comBox下拉显示图片

ComBox显示图像,结合ImageList的Draw方法:

combox属性设置:

 comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
 comboBox1.DrawMode = DrawMode.OwnerDrawFixed;

DrawItem方法中进行实现:

 private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            
            Graphics r = e.Graphics;
            Rectangle rec = e.Bounds;
            Size imageSize = imageList1.ImageSize;
            if (e.Index >=0)
            {   //设置文本的方式
                string s = comboBox1.Items[e.Index].ToString();
                StringFormat SF = new StringFormat();
                SF.Alignment = StringAlignment.Near;
                //如果当前项没有焦点、键盘加速键
                if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                {
                    imageList1.Draw(e.Graphics, rec.Left, rec.Top, e.Index);
                    e.Graphics.DrawString(s, new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Black), rec.Left + imageSize.Width, rec.Top);
                    e.DrawFocusRectangle();          //取得焦点时候的虚线框
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.White), rec);   //画条目背景
                    imageList1.Draw(e.Graphics, rec.Left, rec.Top, e.Index);
                    e.Graphics.DrawString(s, new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Black), rec.Left + imageSize.Width, rec.Top);
                    e.DrawFocusRectangle();
                }

            }
        }

最后的结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值