-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComboBoxEx.cs
117 lines (102 loc) · 3.21 KB
/
ComboBoxEx.cs
1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System.Drawing;
using System.Windows.Forms;
namespace eBookRelease
{
/// <summary>
/// 查找路径时的下拉列表,由于列表项前面要加一个图标,所以自己写了一个类
/// </summary>
public class ComboBoxEx : ComboBox
{
private ImageList imageList;
public ImageList ImageList
{
get { return imageList; }
set { imageList = value; }
}
public ComboBoxEx()
{
DrawMode = DrawMode.OwnerDrawVariable;
}
protected override void OnDrawItem(DrawItemEventArgs ea)
{
ea.DrawBackground();
ea.DrawFocusRectangle();
ComboBoxExItem item;
Size imageSize = imageList.ImageSize;
Rectangle bounds = ea.Bounds;
try
{
item = (ComboBoxExItem)Items[ea.Index];
if (item.ImageIndex != -1)
{
if (item.ImageIndex != ImgUtil.GetIconIndex(Constants.LIBRARYEXT))
{
imageList.Draw(ea.Graphics, bounds.Left + imageSize.Width, bounds.Top, item.ImageIndex);
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left + 2*imageSize.Width, bounds.Top);
}
else
{
imageList.Draw(ea.Graphics, bounds.Left, bounds.Top, item.ImageIndex);
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left + imageSize.Width, bounds.Top);
}
}
else
{
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
catch
{
if (ea.Index != -1)
{
ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
else
{
ea.Graphics.DrawString(Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
base.OnDrawItem(ea);
}
}
/// <summary>
/// 查找路径的下拉列表项
/// </summary>
class ComboBoxExItem
{
private string text;
public string Text
{
get { return text; }
set { text = value; }
}
private int imageIndex;
public int ImageIndex
{
get { return imageIndex; }
set { imageIndex = value; }
}
public ComboBoxExItem()
: this("")
{
}
public ComboBoxExItem(string text)
: this(text, -1)
{
}
public ComboBoxExItem(string text, int imageIndex)
{
this.text = text;
this.imageIndex = imageIndex;
}
public override string ToString()
{
return text;
}
}
}