基本信息
源码名称:winform ComboBox 下拉框 显示图片效果 附完整源码
源码大小:0.04M
文件格式:.zip
开发语言:C#
更新时间:2013-06-23
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
ComboBox 显示图片
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Net; using System.Text; using System.Windows.Forms; namespace ComboBox_Image { public partial class Form1 : Form { public Form1() { InitializeComponent(); //添加项 comboBox1.Items.Add(new MyItem("1111111", this.GetImage("http://www.baidu.com/img/bdlogo.gif"))); comboBox1.Items.Add(new MyItem("2222222", this.GetImage("http://www.baidu.com/img/bdlogo.gif"))); comboBox1.Items.Add(new MyItem("3333333", this.GetImage("http://www.baidu.com/img/bdlogo.gif"))); comboBox1.Items.Add(new MyItem("4444444", this.GetImage("http://www.baidu.com/img/bdlogo.gif"))); comboBox1.Items.Add(new MyItem("5555555", this.GetImage("http://www.baidu.com/img/bdlogo.gif"))); //默认选中项索引 comboBox1.SelectedIndex = 0; //自绘组合框需要设置的一些属性 comboBox1.DrawMode = DrawMode.OwnerDrawVariable; comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; comboBox1.ItemHeight = 30; comboBox1.Width = 200; //添加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 1, 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(217, 223, 230)); 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, 25, 25); 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); } public 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; } } public System.Drawing.Image GetImage(string strUrl) { try { string url = @"http://hiphotos.baidu.com/huyangdiy/pic/item/fd1d340bfaec3e656059f3df.jpg"; url = strUrl; WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); Image image = Image.FromStream(stream); stream.Close(); //g.DrawImage(image, 0, 0); //g.Dispose(); return image; } catch { return null; } } } }