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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42
