为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

继承的应用-为ComboBox控件添加图片

2017-10-23 7页 doc 21KB 12阅读

用户头像

is_977556

暂无简介

举报
继承的应用-为ComboBox控件添加图片继承的应用-为ComboBox控件添加图片 为ComboBox控件添加图片 .NET自带的ComboBox控件没有添加图片的功能,在我们学习了继承以后,我们可以继承原有的ComboBox的功能并且加入扩展的新功能。 下面我们对ComboBox进行扩展,实现在ComboBox中显示图片的功能: 1.添加类 ComboBoxExItem: class ComboBoxExItem { private string _text; public string Text { get { return _text; } ...
继承的应用-为ComboBox控件添加图片
继承的应用-为ComboBox控件添加图片 为ComboBox控件添加图片 .NET自带的ComboBox控件没有添加图片的功能,在我们学习了继承以后,我们可以继承原有的ComboBox的功能并且加入扩展的新功能。 下面我们对ComboBox进行扩展,实现在ComboBox中显示图片的功能: 1.添加类 ComboBoxExItem: class ComboBoxExItem { private string _text; public string Text { get { return _text; } set { _text = value; } } private int _imageIndex; public int ImageIndex { get { return _imageIndex; } set { _imageIndex = value; } } public ComboBoxExItem() : this("") { } public ComboBoxExItem(string text) : this(text, -1) { } public ComboBoxExItem(string text, int imageIndex) { _text = text; _imageIndex = imageIndex; } public override string ToString() { return _text; } } 2.添加类:ComboBoxEx class ComboBoxEx : ComboBox { private ImageList imageList; public ImageList ImageList { get { return imageList; } set { imageList = value; } } public ComboBoxEx() { DrawMode = DrawMode.OwnerDrawFixed; } protected override void OnDrawItem(DrawItemEventArgs ea) { ea.DrawBackground(); ea.DrawFocusRectangle(); ComboBoxExItem item; Size imageSize = imageList.ImageSize; Rectangle bounds = ea.Bounds; try { item = (ComboBoxExItem)Items[ea.Index]; if (item.ImageIndex != -1) { imageList.Draw(ea.Graphics, bounds.Left, bounds.Top, item.ImageIndex); ea.Graphics.DrawString(item.Text, ea.Font, new SolidBrush(ea.ForeColor), bounds.Left + imageSize.Width, bounds.Top); } else { ea.Graphics.DrawString(item.Text, ea.Font, new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top); } } catch { if (ea.Index != -1) { ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font, new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top); } else { ea.Graphics.DrawString(Text, ea.Font, new SolidBrush(ea.ForeColor), bounds.Left, bounds.Top); } } base.OnDrawItem(ea); } } 当上述两个步骤完成之后,我们就可以使用ComboBoxEx来代替ComboBox来实现带图片的ComboBox了 以下是应用的代码:(窗体的加载代码) private void InitializeComponent() { this.components = new System.ComponentModel.Container(); ComboBoxExItem comboBoxExItem1 = new ComboBoxExItem(); ComboBoxExItem comboBoxExItem2 = new ComboBoxExItem(); ComboBoxExItem comboBoxExItem3 = new ComboBoxExItem(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.comboBox1 = new ComboBoxEx(); this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.SuspendLayout(); // // comboBox1 // this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; this.comboBox1.ImageList = this.imageList1; comboBoxExItem1.ImageIndex = 0; comboBoxExItem1.Text = "【IT精彩世界】"; comboBoxExItem2.ImageIndex = 1; comboBoxExItem2.Text = "【IT精彩世界】"; comboBoxExItem3.ImageIndex = 2; comboBoxExItem3.Text = "【IT精彩世界】"; this.comboBox1.Items.AddRange(new object[] { comboBoxExItem1, comboBoxExItem2, comboBoxExItem3}); this.comboBox1.Location = new System.Drawing.Point(48, 109); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(200, 22); this.comboBox1.TabIndex = 0; // // imageList1 // this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); this.imageList1.TransparentColor = System.Drawing.Color.Transparent; this.imageList1.Images.SetKeyName(0, "welcome.JPG"); this.imageList1.Images.SetKeyName(1, "welcome.JPG"); this.imageList1.Images.SetKeyName(2, "welcome.JPG"); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.comboBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); }
/
本文档为【继承的应用-为ComboBox控件添加图片】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索