新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
多彩世界實(shí)現(xiàn)C#Listbox自繪
使用控件的DrawMode屬性來實(shí)現(xiàn)控件的自繪,首先將C# Listbox的DrawMode設(shè)置為OwnerDrawVariable,然后實(shí)現(xiàn)DrawItem ,MeasuerItem方法。

10年積累的成都做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有香河免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
編寫如下代碼:
- private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
- {
- e.DrawBackground();
- Rectangle r = new Rectangle(0, 0, lbCustomDraw.Width, 100);
- bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
- LinearGradientBrush lgb = null;
- if (!selected)
- {
- lgb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.Horizontal);
- }
- else
- {
- lgb = new LinearGradientBrush(r, Color.Cyan, Color.White, LinearGradientMode.Horizontal);
- }
- e.Graphics.FillRectangle(lgb, e.Bounds);
- e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds);
- Rectangle r2 = e.Bounds;
- string displayText = (string)lbCustomDraw.Items[e.Index];
- SizeF size = e.Graphics.MeasureString(displayText, this.Font);
- r2.Y = (int)(r2.Height / 2) - (int)(size.Height / 2) + e.Bounds.Y;
- r2.X = 2;
- e.Graphics.DrawString(displayText, this.Font, Brushes.Black, r2);
- e.DrawFocusRectangle();
- }
- private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
- {
- string displayText = (string)lbCustomDraw.Items[e.Index];
- SizeF size = e.Graphics.MeasureString(displayText, this.Font);
- size.Height += 10;
- e.ItemHeight = (int)size.Height;
- }
最終效果:
多彩世界 實(shí)現(xiàn)C# Listbox自繪
以上就是C# Listbox的自繪方法,很漂亮吧。
網(wǎng)站名稱:多彩世界實(shí)現(xiàn)C#Listbox自繪
網(wǎng)頁(yè)地址:http://fisionsoft.com.cn/article/cogspoo.html


咨詢
建站咨詢
