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

10年積累的成都做網(wǎng)站、成都網(wǎng)站建設經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先做網(wǎng)站設計后付款的網(wǎng)站建設流程,更有香河免費網(wǎng)站建設讓你可以放心的選擇與我們合作。
編寫如下代碼:
- 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;
- }
最終效果:
多彩世界 實現(xiàn)C# Listbox自繪
以上就是C# Listbox的自繪方法,很漂亮吧。
網(wǎng)頁題目:多彩世界實現(xiàn)C#Listbox自繪
分享鏈接:http://www.dlmjj.cn/article/cogspoo.html


咨詢
建站咨詢
