新聞中心
學習C# ListBox使用,僅當 DrawMode 屬性設置為 DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable 時,才引發(fā)該事件??梢允褂迷撌录韴?zhí)行在C# ListBox 中繪制項所需的任務。如果具有大小可變的項(當 DrawMode 屬性設置為 DrawMode.OwnerDrawVariable 時),在繪制項前,引發(fā) MeasureItem 事件。可以為 MeasureItem 事件創(chuàng)建事件處理程序,以在 DrawItem 事件的事件處理程序中指定要繪制的項的大小。有關處理事件的更多信息,請參見 使用事件。

站在用戶的角度思考問題,與客戶深入溝通,找到六枝網(wǎng)站設計與六枝網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:做網(wǎng)站、網(wǎng)站設計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務覆蓋六枝地區(qū)。
重寫C# Listbox的drawitem事件
- privatevoidlistBox1_DrawItem(objectsender,System.Windows.Forms.DrawItemEventArgse){
- //SettheDrawModepropertytodrawfixedsizeditems.
- listBox1.DrawMode=DrawMode.OwnerDrawFixed;
- //DrawthebackgroundoftheListBoxcontrolforeachitem.
- e.DrawBackground();
- //Definethedefaultcolorofthebrushasblack.
- BrushmyBrush=Brushes.Black;
- //Determinethecolorofthebrushtodraweachitembasedontheindexoftheitemtodraw.
- switch(e.Index){
- case0:myBrush=Brushes.Red;break;
- case1:myBrush=Brushes.Orange;break;
- case2:myBrush=Brushes.Purple;break;
- }
- //DrawthecurrentitemtextbasedonthecurrentFontandthecustombrushsettings.
- e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font,myBrush,
- e.Bounds,StringFormat.GenericDefault);
- //IftheListBoxhasfocus,drawafocusrectanglearoundtheselecteditem.
- e.DrawFocusRectangle();
- }
從這個例子,我們發(fā)現(xiàn)在C#下面重畫控件,比在vc++6.0中定義自繪方便多了
當前名稱:C#ListBox學習筆記
當前路徑:http://www.dlmjj.cn/article/djdhgjc.html


咨詢
建站咨詢
