1using System;
2using System.Drawing;
3using System.Drawing.Drawing2D;
4using System.Windows.Forms;
5using System.ComponentModel;
6using System.Drawing.Design;
7namespace CommonDll.CommonControl
8...{
9 /**//// <summary>
10 /// Colorful ComboBox, Very beautiful
11 /// </summary>
12 public class ColorfulListBox : System.Windows.Forms.ListBox
13 ...{
14 private Color startColor = Color.White;
15 private Color endColor = Color.Blue;
16 private Color selectColor = Color.Red;
17 private int imageIndex = -1;
18 private int selectimageIndex = -1;
19 private ImageList imageList = null;
20 private LinearGradientMode linearMode;
21
22 /**//// <summary>
23 /// The color of Item that was selected
24 /// </summary>
25 [ Category("NecessaryOptions"), Description( "The color of Item that was selected " ) ]
26 public Color SelectColor
27 ...{
28 get
29 ...{
30 return this.selectColor;
31 }
32 set
33 ...{
34 this.selectColor = value;
35 }
36 }
37
38 /**//// <summary>
39 /// specify start color of LinearGradientBrush object
40 /// </summary>
41 [ Category("NecessaryOptions"), Description( "The start color of Item " ) ]
42 public Color StartColor
43 ...{
44 get
45 ...{
46 return this.startColor;
47 }
48 set
49 ...{
50 this.startColor = value;
51 }
52 }
53 /**//// <summary>
54 /// specify end color of LinearGradientBrush object
55 /// </summary>
56 [ Category("NecessaryOptions"), Description( "The end color of Item " ) ]
57 public Color EndColor
58 ...{
59 get
60 ...{
61 return this.endColor;
62 }
63 set
64 ...{
65 this.endColor = value;
66 }
67 }
68 /**//// <summary>
69 /// Index of image of Draw
70 /// </summary>
71 [ Category("NecessaryOptions"), Description("select image of item displays"), Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), TypeConverter(typeof(ImageIndexConverter)), Localizable(true), DefaultValue(-1)]
72 public int ImageIndex
73 ...{
74 get
75 ...{
76 if (((this.imageIndex != -1) && (this.imageList != null)) && (this.imageIndex >= this.imageList.Images.Count))
77 ...{
78 return (this.imageList.Images.Count - 1);
79 }
80 return this.imageIndex;
81 }
82 set
83 ...{
84 if (value < -1)
85 ...{
86 throw new Exception( "ERROR" );
87 }
88 if (this.imageIndex != value)
89 ...{
90 this.imageIndex = value;
91 base.Invalidate();
92 }
93 }
94 }
95
96 /**//// <summary>
97 /// Index of image that was selected
98 /// </summary>
99 [ Category("NecessaryOptions"), Description(" Index of image that item was selected"), Editor("System.Windows.Forms.Design.ImageIndexEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), TypeConverter(typeof(ImageIndexConverter)), Localizable(true), DefaultValue(-1)]
100 public int SelectImageIndex
101 ...{
102 get
103 ...{
104 if (((this.selectimageIndex != -1) && (this.imageList != null)) && (this.selectimageIndex >= this.imageList.Images.Count))
105 ...{
106 return (this.imageList.Images.Count - 1);
107 }
108 return this.selectimageIndex;
109 }
110 set
111 ...{
112 if (value < -1)
113 ...{
114 throw new Exception( "ERROR" );
115 }
116 if (this.selectimageIndex != value)
117 ...{
118 this.selectimageIndex = value;
119 base.Invalidate();
120 }
121 }
122 }
123
124 /**//// <summary>
125 /// the imageList of item
126 /// </summary>
127 [ Category("NecessaryOptions"), Description( "Image List that will be display by item " ) ]
128 public ImageList MyImageList
129 ...{
130 get
131 ...{
132 return this.imageList;
133 }
134 set
135 ...{
136 this.imageList = value;
137 }
138 }
139 /**//// <summary>
140 /// Specify the mode of LinearGradient
141 /// </summary>
142 [ Category("NecessaryOptions"), Description( "The Mode of LinearFradient " ) ]
143 public LinearGradientMode LinearMode
144 ...{
145 get
146 ...{
147 return this.linearMode;
148 }
149 set
150 ...{
151 this.linearMode = value;
152 }
153 }
154 /**//// <summary>
155 /// Draw Item
156 /// </summary>
157 /// <param name="e"></param>
158 protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
159 ...{
160 if( this.MyImageList == null || this.Items.Count == 0 )
161 ...{
162 return;
163 }
164 Graphics g = e.Graphics;
165 LinearGradientBrush lgb = null;
166
167 //string str = this.Items[ e.Index ].ToString( );
168 //比较这两个语句,你会发现什么错误?
169 //对了,如果使用第一句,会造成绘制成:System.Data.DataRowView字样!!!!!!!!!!
170 //让你不知所措,甚至气急败坏。beacuse the item maybe not a string!!!
171 string str = this.GetItemText( this.Items[ e.Index ] ); //从物理层面强行转换此对象为字符串类型
172
173
174 //取得一般情况下项目显示的图标
175 Image im = this.MyImageList.Images[ this.ImageIndex ];
176
177 //取得将要绘制的字符串的长度
178 SizeF sizeString = g.MeasureString( str, this.Font );
179
180 //取得图标的大小
181 Size sizeImage = new Size( im.Width, im.Height );
182
183 //设置将要绘制的图标的坐标
184 Point pImage = new Point( 3, e.Bounds.Height / 2 - im.Height / 2 + e.Bounds.Y );
185
186 //设置将要绘制的字符串的坐标
187 Point pString = new Point( im.Width + 5, e.Bounds.Height /2 - ( int )sizeString.Height / 2 + e.Bounds.Y + 1 );
188
189 Rectangle rImage = new Rectangle( pImage, sizeImage );
190 Rectangle rString = new Rectangle( pString, new Size( e.Bounds.Width - sizeImage.Width + 3, e.Bounds.Height ));
191 Rectangle rLgb = e.Bounds;
192 if( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected )
193 ...{
194 lgb = new LinearGradientBrush( rLgb, this.StartColor, this.EndColor, this.LinearMode );
195 g.FillRectangle( lgb, rLgb );
196 g.DrawImage( this.MyImageList.Images[ this.SelectImageIndex ], rImage );
197 g.DrawString( str, this.Font, new SolidBrush( this.SelectColor ), rString );
198 }
199 else
200 ...{
201 g.FillRectangle( Brushes.White, rLgb );
202 g.DrawImage( im, rImage );
203 g.DrawString( str, this.Font, new SolidBrush( this.ForeColor ), rString );
204 }
205 }
206 }
207}
208
effect as follows: