




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Download source and demo project - 14.4 Kb IntroductionWhen I started programming in .NET, I wondered where the ImageCombo control had gone (I used to program in VB6). The .NET Framework didnt seem to have this functionality built-in, but its very easy to add it.Using the codeThe code consists of two classes: ImageCombo and ImageComboItem. If you want to use them in C#, just copy them into your project; for other .NET languages, you can add a reference to the library.The control inherits from ComboBox and introduces one new member: the ImageList property, which doesnt require any further explanation. The control is owner drawn and there is a custom drawing method defined, so dont change its DrawMode property if you want to see the images.The ImageComboItem class inherits from Object. You can set a customForeColor and it has a Mark property which determines if the item is shown in bold font style (does not work if owner font is already bold).To add an item to an ImageCombo with text Icon 0 and image index 0, use the following code:imageCombo.Items.Add(new ImageComboItem(Icon 0, 0);Code listing: ImageCombo classusing System;using System.Drawing;namespace System.Windows.Forms public class ImageCombo : ComboBox private ImageList imgs = new ImageList(); / constructor public ImageCombo() / set draw mode to owner draw this.DrawMode = DrawMode.OwnerDrawFixed; / ImageList property public ImageList ImageList get return imgs; set imgs = value; / customized drawing process protected override void OnDrawItem(DrawItemEventArgs e) / draw background & focus rect e.DrawBackground(); e.DrawFocusRectangle(); / check if it is an item from the Items collection if (e.Index 0) / not an item, draw the text (indented) e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top); else / check if item is an ImageComboItem if (this.Itemse.Index.GetType() = typeof(ImageComboItem) / get item to draw ImageComboItem item = (ImageComboItem) this.Itemse.Index; / get forecolor & font Color forecolor = (item.ForeColor != Color.FromKnownColor(KnownColor.Transparent) ? item.ForeColor : e.ForeColor; Font font = item.Mark ? new Font(e.Font, FontStyle.Bold) : e.Font; / -1: no image if (item.ImageIndex != -1) / draw image, then draw text next to it this.ImageList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, item.ImageIndex); e.Graphics.DrawString(item.Text, font, new SolidBrush(forecolor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top); else / draw text (indented) e.Graphics.DrawString(item.Text, font, new SolidBrush(forecolor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top); else / it is not an ImageComboItem, draw it e.Graphics.DrawString(this.Itemse.Index.ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + imgs.ImageSize.Width, e.Bounds.Top); base.OnDrawItem (e); Code listing: ImageComboItem classusing System;using System.Drawing;namespace System.Windows.Forms public class ImageComboItem : object / forecolor: transparent = inherit private Color forecolor = Color.FromKnownColor( KnownColor.Transparent); private bool mark = false; private int imageindex = -1; private object tag = null; private string text = null; / constructors public ImageComboItem() public ImageComboItem(string Text) text = Text; public ImageComboItem(string Text, int ImageIndex) text = Text; imageindex = ImageIndex; public ImageComboItem(string Text, int ImageIndex, bool Mark) text = Text; imageindex = ImageIndex; mark = Mark; public ImageComboItem(string Text, int ImageIndex, bool Mark, Color ForeColor) text = Text; imageindex = ImageIndex; mark = Mark; forecolor = ForeColor; public ImageComboItem(string Text, int ImageIndex, bool Mark, Color ForeColor, object Tag) text = Text; imageindex = ImageIndex; mark = Mark; forecolor = ForeColor; tag = Tag; / forecolor public Color ForeColor get return forecolor; set forecolor = value; / image index public int ImageIndex get return imageindex; set imageindex = value; / mark (bold) public bool Mark get return mark; set mark = value; / tag public object Tag get return tag; set tag = value; / item text public string Text get return text; set text = value; / ToString() should return item text public override string ToString() return text; 使用方法:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;namespace Demopublic class MainWindow : System.Windows.Forms.FormMTAThread() static void Main() Application.EnableVisualStyles();Application.DoEvents();Application.Run(new MainWindow();private System.Windows.Forms.ImageCombo imageCombo;private System.Windows.Forms.ImageList imageList;private System.Windows.Forms.ComboBox comboBox;private System.Windows.Forms.ImageCombo imageComboList;private System.Windows.Forms.ImageCombo imageComboSimple;private System.ComponentModel.IContainer components;public MainWindow()InitializeComponent();protected override void Dispose( bool disposing )if( disposing )if(components != null)components.Dispose();base.Dispose( disposing );#region Windows Form Designer generated code/ / Required method for Designer support - do not modify/ the contents of this method with the code editor./ private void InitializeComponent()ponents = new System.ComponentModel.Container();System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainWindow);this.imageCombo = new System.Windows.Forms.ImageCombo();this.imageList = new System.Windows.Forms.ImageList(ponents);boBox = new System.Windows.Forms.ComboBox();this.imageComboList = new System.Windows.Forms.ImageCombo();this.imageComboSimple = new System.Windows.Forms.ImageCombo();this.SuspendLayout();/ / imageCombo/ this.imageCombo.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;this.imageCombo.ImageList = this.imageList;this.imageCombo.Location = new System.Drawing.Point(8, 40);this.imageCombo.Name = imageCombo;this.imageCombo.Size = new System.Drawing.Size(160, 22);this.imageCombo.TabIndex = 1;/ / imageList/ this.imageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;this.imageList.ImageSize = new System.Drawing.Size(16, 16);this.imageList.ImageStream = (System.Windows.Forms.ImageListStreamer)(resources.GetObject(imageList.ImageStream);this.imageList.TransparentColor = System.Drawing.Color.Transparent;/ / comboBox/ boBox.Location = new System.Drawing.Point(8, 8);boBox.Name = comboBox;boBox.Size = new System.Drawing.Size(160, 21);boBox.TabIndex = 0;boBox.Text = ComboBox;/ / imageComboList/ this.imageComboList.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;this.imageComboList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;this.imageComboList.ImageList = this.imageList;this.imageComboList.Location = new System.Drawing.Point(8, 72);this.imageComboList.Name = imageComboList;this.imageComboList.Size = new System.Drawing.Size(160, 22);this.imageComboList.TabIndex = 2;/ / imageComboSimple/ this.imageComboSimple.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;this.imageComboSimple.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;this.imageComboSimple.ImageList = this.imageList;this.imageComboSimple.Location = new System.Drawing.Point(8, 104);this.imageComboSimple.Name = imageComboSimple;this.imageComboSimple.Size = new System.Drawing.Size(160, 104);this.imageComboSimple.TabIndex = 3;/ / MainWindow/ this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);this.ClientSize = new System.Drawing.Size(202, 216);this.Controls.Add(this.imageComboSimple);this.Controls.Add(boBox);this.Controls.Add(this.imageCombo);this.Controls.Add(this.imageComboList);this.Font = new System.Drawing.Font(Tahoma, 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, (System.Byte)(0);this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;this.Name = MainWindow;this.Text = Demo;this.Load += new System.EventHandler(this.MainWindow_Load);this.ResumeLayout(false);#endregionprivate void MainWindow_Load(object sender, System.EventArgs e)/ add some items (normal combobox);comboBox.Items.Add(Item);comboBox.Items.Add(new ImageComboItem(You can add ImageComboItems to a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年3月湖北东津国投集团及子公司社会招聘拟聘用人员模拟试卷有完整答案详解
- 工程债权债务转让协议范文8篇
- 2025年及未来5年中国互联网+烘焙食品市场供需格局及未来发展趋势报告
- 2025国网国际发展有限公司第二批高校毕业生录用人选的考前自测高频考点模拟试题及答案详解(全优)
- 2025年福建省罗源县城市管理和综合执法局内勤人员招聘模拟试卷附答案详解(完整版)
- 2025贵州安顺参加“第十三届贵州人才博览会”引才模拟试卷带答案详解
- 班组安全生产培训资料课件
- 2025安徽芜湖经济技术开发区招聘中学非编教师55人模拟试卷及一套答案详解
- 2025呼伦贝尔牙克石市第三批招聘16名城镇公益性岗位劳动保障协理员考前自测高频考点模拟试题附答案详解(完整版)
- 2025年福建省宁德市霞浦县实验幼儿园招聘若干人模拟试卷及答案详解(全优)
- 2025年《数字孪生与虚拟调试技术应用》课程标准
- 生物●安徽卷丨2024年安徽省普通高中学业水平选择性考试生物试卷及答案
- 蓝牙耳机委托加工协议书
- 北京车牌出租协议书
- 忠诚协议书和婚内财产协议
- 2025-2030汽车贷款行业市场深度分析及发展策略研究报告
- 反诈知识进校园主题团课
- SCR脱硝催化剂体积及反应器尺寸计算表
- 煤巷掘进工作面瓦斯超限管控措施培训课件
- 投标代理人委托书
- 2025届高三英语一轮复习人教版(2019)必修第二册单词默写纸
评论
0/150
提交评论