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

站在用戶的角度思考問題,與客戶深入溝通,找到六枝網(wǎng)站設(shè)計(jì)與六枝網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋六枝地區(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();
- }
從這個(gè)例子,我們發(fā)現(xiàn)在C#下面重畫控件,比在vc++6.0中定義自繪方便多了
當(dāng)前題目:C#ListBox學(xué)習(xí)筆記
分享URL:http://m.fisionsoft.com.cn/article/djdhgjc.html


咨詢
建站咨詢
