欢迎来到天天文库
浏览记录
ID:39547011
大小:79.50 KB
页数:10页
时间:2019-07-06
《C#带图标的组合框》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、·Downloadsourceanddemoproject-14.4KbIntroductionWhenIstartedprogrammingin.NET,IwonderedwheretheImageCombocontrolhadgone(IusedtoprograminVB6).The.NETFrameworkdidn'tseemtohavethisfunctionalitybuilt-in,butit'sveryeasytoaddit.UsingthecodeThecodeconsistsoftwoclasses:Imag
2、eComboandImageComboItem.IfyouwanttousetheminC#,justcopythemintoyourproject;forother.NETlanguages,youcanaddareferencetothelibrary.ThecontrolinheritsfromComboBoxandintroducesonenewmember:theImageListproperty,whichdoesn'trequireanyfurtherexplanation.Thecontrolisownerdr
3、awnandthereisacustomdrawingmethoddefined,sodon'tchangeitsDrawModepropertyifyouwanttoseetheimages.TheImageComboItemclassinheritsfromObject.YoucansetacustomForeColorandithasaMarkpropertywhichdeterminesiftheitemisshowninboldfontstyle(doesnotworkifownerfontisalreadybold
4、).ToaddanitemtoanImageCombowithtext"Icon0"andimageindex0,usethefollowingcode:imageCombo.Items.Add(newImageComboItem("Icon0",0));Codelisting:ImageComboclassusingSystem;usingSystem.Drawing;namespaceSystem.Windows.Forms{publicclassImageCombo:ComboBox{privateImageListim
5、gs=newImageList();//constructorpublicImageCombo(){//setdrawmodetoownerdrawthis.DrawMode=DrawMode.OwnerDrawFixed;}//ImageListpropertypublicImageListImageList{get{returnimgs;}set{imgs=value;}}//customizeddrawingprocessprotectedoverridevoidOnDrawItem(DrawItemEventArgse
6、){//drawbackground&focusrecte.DrawBackground();e.DrawFocusRectangle();//checkifitisanitemfromtheItemscollectionif(e.Index<0)//notanitem,drawthetext(indented)e.Graphics.DrawString(this.Text,e.Font,newSolidBrush(e.ForeColor),e.Bounds.Left+imgs.ImageSize.Width,e.Bounds
7、.Top);else{//checkifitemisanImageComboItemif(this.Items[e.Index].GetType()==typeof(ImageComboItem)){//getitemtodrawImageComboItemitem=(ImageComboItem)this.Items[e.Index];//getforecolor&fontColorforecolor=(item.ForeColor!=Color.FromKnownColor(KnownColor.Transparent))
8、?item.ForeColor:e.ForeColor;Fontfont=item.Mark?newFont(e.Font,FontStyle.Bold):e.Font;//-1:noimageif(item.ImageIndex!=-1){//drawimage,thend
此文档下载收益归作者所有