自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

有顏色的世界:C# listbox的item顏色改變方法

開發(fā) 后端
大家都喜歡多彩的世界,筆者也想運(yùn)用鍵盤來敲出一個(gè)多彩的畫面,下面給大家介紹一種C# listbox的item顏色改變的方法。

大家都喜歡多彩的世界,筆者也想運(yùn)用鍵盤來敲出一個(gè)多彩的畫面,下面給大家介紹一種C# listbox的item顏色改變的方法。

(1)C# listbox實(shí)現(xiàn)變色之前,需要先設(shè)置屬性:

該事件由所有者描述的ListBox使用。僅當(dāng)DrawMode屬性設(shè)置為DrawMode.OwnerDrawFixed或DrawMode.OwnerDrawVariable時(shí),才引發(fā)該事件??梢允褂迷撌录韴?zhí)行在ListBox中繪制項(xiàng)所需的任務(wù)。

如果具有大小可變的項(xiàng)(當(dāng)DrawMode屬性設(shè)置為DrawMode.OwnerDrawVariable時(shí)),在繪制項(xiàng)前,引發(fā)MeasureItem事件??梢詾镸easureItem事件創(chuàng)建事件處理程序,以在DrawItem事件的事件處理程序中指定要繪制的項(xiàng)的大小。有關(guān)處理事件的更多信息,請(qǐng)參見使用事件。

(2)重寫C# listbox的drawitem事件

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

(3)從C#  listbox的item顏色改變的例子中,我們發(fā)現(xiàn)在c#下面重畫控件,比在vc++6.0中定義自繪方便多了。

【編輯推薦】

  1. 解析C# Socket編程實(shí)現(xiàn)訪問網(wǎng)絡(luò)的原理
  2. C# switch語句簡(jiǎn)單描述
  3. C# ServiceController類剖析
  4. C#抓取網(wǎng)頁(yè)程序的實(shí)現(xiàn)淺析
  5. C#內(nèi)存管理詳細(xì)分析
責(zé)任編輯:阡陌 來源: 網(wǎng)絡(luò)轉(zhuǎn)載
相關(guān)推薦

2009-09-08 15:12:07

C# ListBox

2009-09-08 16:34:49

C# Listbox

2009-09-08 14:54:40

C# listBox控

2009-09-08 16:22:27

c# listBox

2019-09-10 15:20:26

Linux終端命令

2009-09-08 15:50:44

c# listbox

2009-08-12 15:50:40

C# ListBox

2009-08-19 11:21:02

C# ListBox控

2009-09-08 16:10:03

C# ListBox

2015-09-09 09:12:28

ios矢量圖顏色

2009-08-14 14:19:50

Enhanced LiC#構(gòu)建

2009-09-08 15:39:00

2023-04-13 07:45:15

WebGL片元著色器

2013-06-25 11:06:07

Android開發(fā)顏色定義方法

2009-09-08 16:01:58

C# ListBox

2009-09-08 17:37:54

C# listbox控

2010-05-06 14:43:29

Visual Stud

2009-09-10 12:00:09

C# listbox

2009-12-28 15:48:14

WPF窗口顏色

2021-08-16 11:58:15

CSS顏色屬性前端
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)