C#ListBox学习笔记

学习C# ListBox使用,仅当 DrawMode 属性设置为 DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable 时,才引发该事件。可以使用该事件来执行在C# ListBox 中绘制项所需的任务。如果具有大小可变的项(当 DrawMode 属性设置为 DrawMode.OwnerDrawVariable 时),在绘制项前,引发 MeasureItem 事件。可以为 MeasureItem 事件创建事件处理程序,以在 DrawItem 事件的事件处理程序中指定要绘制的项的大小。有关处理事件的更多信息,请参见 使用事件。

站在用户的角度思考问题,与客户深入沟通,找到六枝网站设计与六枝网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、网站设计、企业官网、英文网站、手机端网站、网站推广、国际域名空间、网页空间、企业邮箱。业务覆盖六枝地区。

重写C# Listbox的drawitem事件

 
 
 
  1. privatevoidlistBox1_DrawItem(objectsender,System.Windows.Forms.DrawItemEventArgse){
  2. //SettheDrawModepropertytodrawfixedsizeditems.
  3. listBox1.DrawMode=DrawMode.OwnerDrawFixed;
  4. //DrawthebackgroundoftheListBoxcontrolforeachitem.
  5. e.DrawBackground();
  6. //Definethedefaultcolorofthebrushasblack.
  7. BrushmyBrush=Brushes.Black;
  8. //Determinethecolorofthebrushtodraweachitembasedontheindexoftheitemtodraw.
  9. switch(e.Index){
  10. case0:myBrush=Brushes.Red;break;
  11. case1:myBrush=Brushes.Orange;break;
  12. case2:myBrush=Brushes.Purple;break;
  13. }
  14. //DrawthecurrentitemtextbasedonthecurrentFontandthecustombrushsettings.
  15. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font,myBrush,
  16. e.Bounds,StringFormat.GenericDefault);
  17. //IftheListBoxhasfocus,drawafocusrectanglearoundtheselecteditem.
  18. e.DrawFocusRectangle();
  19. }

从这个例子,我们发现在C#下面重画控件,比在vc++6.0中定义自绘方便多了


网站栏目:C#ListBox学习笔记
网页链接:http://www.gydahua.com/article/djdhgjc.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流