




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO/TS 26762:2025 EN Design and operation of allocation systems used in gas productions facilities
- 【正版授权】 ISO 19690-2:2018/Amd 1:2025 EN Disc springs - Part 2: Technical specifications - Amendment 1: Durability chart for not shot peened springs (group 3)
- 【正版授权】 IEC 61300-2-5:2022+AMD1:2025 CSV EN Fibre optic interconnecting devices and passive components - Basic test and measurement procedures - Part 2-5: Tests - Torsion
- 【正版授权】 IEC 60614-2-1:1982 EN-D Specification for conduits for electrical installations. Part 2: Particular specifications for conduits. Section One: Metal conduits
- 校园食品安全知识培训
- 药学执业考试试题及答案
- 法院文职面试题及答案
- 骨科填空考试题及答案
- 海关模拟面试题及答案
- 2025年湖北省中考语文真题(含答案)
- 解除共管账户协议书
- 心胸外科麻醉管理
- 《鸿蒙HarmonyOS应用开发基础》课件 第1-3章 初识鸿蒙、ArkTS(上)、ArkTS(下)
- 2025年医院血透室人员培训计划
- 《消防员心理素质培养》课件
- 倍智tas人才测评系统题库及答案
- 公安机关办理行政案件程序规定课件
- 九年级全一册英语单词默写表(人教版)
- 教育培训项目的质量控制与保障措施
- 会籍顾问礼仪培训
- 中医药质量与安全管理制度
评论
0/150
提交评论