用颜色填充下拉框(C#)
代码如下:
 1 private void CtrGifType_Load(object sender, EventArgs e)
        private void CtrGifType_Load(object sender, EventArgs e)
2 {
        {
3 //透明色初始化设置
            //透明色初始化设置
4 cmbTransparence.DrawMode = DrawMode.OwnerDrawFixed;
            cmbTransparence.DrawMode = DrawMode.OwnerDrawFixed;
5 cmbTransparence.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbTransparence.DropDownStyle = ComboBoxStyle.DropDownList;
6 cmbTransparence.DrawItem += new DrawItemEventHandler(cmbTransparence_DrawItem);
            cmbTransparence.DrawItem += new DrawItemEventHandler(cmbTransparence_DrawItem);
7 cmbTransparence.ItemHeight = 18;
            cmbTransparence.ItemHeight = 18;
8 cmbTransparence.BeginUpdate();
            cmbTransparence.BeginUpdate();
9 cmbTransparence.Items.Clear();
            cmbTransparence.Items.Clear();
10 //循环遍历添加每一项
            //循环遍历添加每一项
11 foreach (PropertyInfo proinfo in typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static))
            foreach (PropertyInfo proinfo in typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static))
12 {
            {
13 cmbTransparence.Items.Add(proinfo.Name);
                cmbTransparence.Items.Add(proinfo.Name);
14 }
            }
15 cmbTransparence.EndUpdate();
            cmbTransparence.EndUpdate();
16 }
        }
17
18 private void cmbTransparence_DrawItem(object sender, DrawItemEventArgs e)
        private void cmbTransparence_DrawItem(object sender, DrawItemEventArgs e)
19 {
        {
20 if (e.Index < 0) return;
            if (e.Index < 0) return;
21
22 e.DrawBackground();
            e.DrawBackground();
23
24 //得到绘制的矩形框
            //得到绘制的矩形框
25 Rectangle rect = new Rectangle(4, e.Bounds.Top + 2, e.Bounds.Height + 10, e.Bounds.Height - 4);
            Rectangle rect = new Rectangle(4, e.Bounds.Top + 2, e.Bounds.Height + 10, e.Bounds.Height - 4);
26
27 string colorName = cmbTransparence.Items[e.Index].ToString();
            string colorName = cmbTransparence.Items[e.Index].ToString();
28
29 //定义单色的画笔
            //定义单色的画笔
30 SolidBrush b = new SolidBrush(Color.FromName(colorName));
            SolidBrush b = new SolidBrush(Color.FromName(colorName));
31 //内部颜色填充
            //内部颜色填充
32 e.Graphics.FillRectangle(b, rect);
            e.Graphics.FillRectangle(b, rect);
33 e.Graphics.DrawRectangle(Pens.Black, rect);
            e.Graphics.DrawRectangle(Pens.Black, rect);
34
35 //设置显示的字体
            //设置显示的字体
36 Font pFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
            Font pFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
37
38 //在指定的矩形内绘制文本
            //在指定的矩形内绘制文本
39 e.Graphics.DrawString(colorName, pFont, Brushes.Black, new Rectangle(e.Bounds.X + rect.Width + 4, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            e.Graphics.DrawString(colorName, pFont, Brushes.Black, new Rectangle(e.Bounds.X + rect.Width + 4, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
40 //在指定的边界范围内绘制聚集框
            //在指定的边界范围内绘制聚集框
41 e.DrawFocusRectangle();
            e.DrawFocusRectangle();
42 }
        }
 private void CtrGifType_Load(object sender, EventArgs e)
        private void CtrGifType_Load(object sender, EventArgs e)2
 {
        {3
 //透明色初始化设置
            //透明色初始化设置4
 cmbTransparence.DrawMode = DrawMode.OwnerDrawFixed;
            cmbTransparence.DrawMode = DrawMode.OwnerDrawFixed;5
 cmbTransparence.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbTransparence.DropDownStyle = ComboBoxStyle.DropDownList;6
 cmbTransparence.DrawItem += new DrawItemEventHandler(cmbTransparence_DrawItem);
            cmbTransparence.DrawItem += new DrawItemEventHandler(cmbTransparence_DrawItem);7
 cmbTransparence.ItemHeight = 18;
            cmbTransparence.ItemHeight = 18;8
 cmbTransparence.BeginUpdate();
            cmbTransparence.BeginUpdate();9
 cmbTransparence.Items.Clear();
            cmbTransparence.Items.Clear();10
 //循环遍历添加每一项
            //循环遍历添加每一项11
 foreach (PropertyInfo proinfo in typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static))
            foreach (PropertyInfo proinfo in typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static))12
 {
            {13
 cmbTransparence.Items.Add(proinfo.Name);
                cmbTransparence.Items.Add(proinfo.Name);14
 }
            }15
 cmbTransparence.EndUpdate();
            cmbTransparence.EndUpdate();16
 }
        }17

18
 private void cmbTransparence_DrawItem(object sender, DrawItemEventArgs e)
        private void cmbTransparence_DrawItem(object sender, DrawItemEventArgs e)19
 {
        {20
 if (e.Index < 0) return;
            if (e.Index < 0) return;21

22
 e.DrawBackground();
            e.DrawBackground();23

24
 //得到绘制的矩形框
            //得到绘制的矩形框25
 Rectangle rect = new Rectangle(4, e.Bounds.Top + 2, e.Bounds.Height + 10, e.Bounds.Height - 4);
            Rectangle rect = new Rectangle(4, e.Bounds.Top + 2, e.Bounds.Height + 10, e.Bounds.Height - 4);26

27
 string colorName = cmbTransparence.Items[e.Index].ToString();
            string colorName = cmbTransparence.Items[e.Index].ToString();28

29
 //定义单色的画笔
            //定义单色的画笔30
 SolidBrush b = new SolidBrush(Color.FromName(colorName));
            SolidBrush b = new SolidBrush(Color.FromName(colorName));31
 //内部颜色填充
            //内部颜色填充32
 e.Graphics.FillRectangle(b, rect);
            e.Graphics.FillRectangle(b, rect);33
 e.Graphics.DrawRectangle(Pens.Black, rect);
            e.Graphics.DrawRectangle(Pens.Black, rect);34

35
 //设置显示的字体
            //设置显示的字体36
 Font pFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
            Font pFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);37

38
 //在指定的矩形内绘制文本
            //在指定的矩形内绘制文本39
 e.Graphics.DrawString(colorName, pFont, Brushes.Black, new Rectangle(e.Bounds.X + rect.Width + 4, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            e.Graphics.DrawString(colorName, pFont, Brushes.Black, new Rectangle(e.Bounds.X + rect.Width + 4, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));40
 //在指定的边界范围内绘制聚集框
            //在指定的边界范围内绘制聚集框41
 e.DrawFocusRectangle();
            e.DrawFocusRectangle();42
 }
        } 
                    
                     
                    
                 
                    
                 
 
         
        
 
             
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号