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

多彩世界 實(shí)現(xiàn)C# Listbox自繪

開(kāi)發(fā) 后端
想要實(shí)現(xiàn)漂亮的C# Listbox控件嗎?看此文章就對(duì)了,本文教你如何把Listbox控件變漂漂。

使用控件的DrawMode屬性來(lái)實(shí)現(xiàn)控件的自繪,首先將C# Listbox的DrawMode設(shè)置為OwnerDrawVariable,然后實(shí)現(xiàn)DrawItem ,MeasuerItem方法。

編寫如下代碼:

  1. private void listBox1_DrawItem(object sender, DrawItemEventArgs e)  
  2.       {  
  3.           e.DrawBackground();  
  4.           Rectangle r = new Rectangle(0, 0, lbCustomDraw.Width, 100);  
  5.           bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);  
  6.           LinearGradientBrush lgb = null;  
  7.           if (!selected)  
  8.           {  
  9.               lgb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.Horizontal);  
  10.           }  
  11.           else 
  12.           {  
  13.               lgb = new LinearGradientBrush(r, Color.Cyan, Color.White, LinearGradientMode.Horizontal);  
  14.           }  
  15.           e.Graphics.FillRectangle(lgb, e.Bounds);  
  16.           e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds);  
  17.           Rectangle r2 = e.Bounds;  
  18.           string displayText = (string)lbCustomDraw.Items[e.Index];  
  19.           SizeF size = e.Graphics.MeasureString(displayText, this.Font);  
  20.           r2.Y = (int)(r2.Height / 2) - (int)(size.Height / 2) + e.Bounds.Y;  
  21.           r2.X = 2;  
  22.           e.Graphics.DrawString(displayText, this.Font, Brushes.Black, r2);  
  23.           e.DrawFocusRectangle();  
  24.       }  
  25.       private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)  
  26.       {  
  27.           string displayText = (string)lbCustomDraw.Items[e.Index];  
  28.           SizeF size = e.Graphics.MeasureString(displayText, this.Font);  
  29.           size.Height += 10;  
  30.           e.ItemHeight = (int)size.Height;  
  31.       }  

最終效果:

多彩世界 實(shí)現(xiàn)C# Listbox自繪
多彩世界 實(shí)現(xiàn)C# Listbox自繪

以上就是C#  Listbox的自繪方法,很漂亮吧。

【編輯推薦】

  1. C#委托基礎(chǔ):談委托和接口
  2. 簡(jiǎn)單實(shí)現(xiàn)C# CheckBox單選的相關(guān)功能
  3. C# ServiceController類剖析
  4. C# HttpWebRequest提交數(shù)據(jù)方式淺析
  5. C#計(jì)算素?cái)?shù)序列淺談
責(zé)任編輯:阡陌 來(lái)源: 網(wǎng)絡(luò)轉(zhuǎn)載
相關(guān)推薦

2009-09-08 15:12:07

C# ListBox

2009-09-10 12:00:09

C# listbox

2009-08-12 15:50:40

C# ListBox

2009-08-19 11:21:02

C# ListBox控

2009-08-14 14:19:50

Enhanced LiC#構(gòu)建

2009-09-08 15:39:00

2009-09-08 14:43:41

C# listbox

2009-09-08 16:01:58

C# ListBox

2009-09-08 17:37:54

C# listbox控

2009-09-08 16:10:03

C# ListBox

2009-09-08 14:54:40

C# listBox控

2009-09-08 16:22:27

c# listBox

2010-03-04 10:34:04

Android操作系統(tǒng)

2009-09-08 15:50:44

c# listbox

2010-02-05 16:28:07

Android

2022-12-29 10:05:38

AIGC人工智能技術(shù)

2009-08-31 15:55:17

C#實(shí)現(xiàn)Strateg

2009-08-19 17:00:07

C#實(shí)現(xiàn)PrintPa

2009-08-20 14:22:17

C#實(shí)現(xiàn) Contro

2009-08-25 17:55:52

C#實(shí)現(xiàn)Strateg
點(diǎn)贊
收藏

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