/
ShengColorChooseComboBox.cs
368 lines (308 loc) · 11.4 KB
/
ShengColorChooseComboBox.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Sheng.Winform.Controls.Drawing;
namespace Sheng.Winform.Controls
{
public partial class ShengColorChooseComboBox : UserControl
{
private Bitmap showColorBitmap;
private Graphics showColorGraphics;
private TextBox txtBack;
private TextBox txtValue;
private Button btnComboButton;
private PictureBox picColor;
private Form formDropDown;
/// <summary>
/// 弹出框的所有者窗体
/// 主要是为了弹出框中的ColorDialog服务,可能会造成主窗体失去焦点而不能刷新选择的颜色
/// 把IDE主窗体设置过来以解决
/// </summary>
public Form OwnerForm
{
get;
set;
}
public event EventHandler OnSelectedChange;
/// <summary>
/// 颜色选取框
/// </summary>
private ShengColorChooseComboBoxDropDown dropUserControl = new ShengColorChooseComboBoxDropDown();
public ShengColorChooseComboBox()
{
InitializeComponent();
this.dropUserControl.ColorChooseComboBox = this;
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
this.txtValue = new TextBox();
this.txtValue.BorderStyle = BorderStyle.None;
this.txtValue.ReadOnly = true;
this.txtValue.BackColor = SystemColors.Window;
this.txtValue.ForeColor = SystemColors.ControlText;
this.txtValue.TextChanged+=new EventHandler(txtValue_TextChanged);
this.btnComboButton = new Button();
this.btnComboButton.Text = "";
this.btnComboButton.Click += new EventHandler(ToggleTreeView);
//this.btnComboButton.FlatStyle = FlatStyle.Flat;
this.formDropDown = new Form();
this.formDropDown.FormBorderStyle = FormBorderStyle.None;
this.formDropDown.BringToFront();
this.formDropDown.StartPosition = FormStartPosition.Manual;
this.formDropDown.ShowInTaskbar = false;
this.formDropDown.BackColor = SystemColors.Control;
this.formDropDown.Deactivate += new EventHandler(frmTreeView_Deactivate);
this.picColor = new PictureBox();
this.picColor.Width = 24;
this.picColor.BackColor = SystemColors.Window;
//放一个文本框做背景用于统一外观
this.txtBack = new TextBox();
this.txtBack.Enabled = false;
this.txtBack.BackColor = SystemColors.Window;
this.Controls.Add(txtBack);
this.Controls.AddRange(new Control[] { picColor, btnComboButton, txtValue });
}
/// <summary>
/// 是否显示文本框的边框
/// </summary>
public bool TextBoxBorder
{
set
{
this.txtBack.Visible = value;
}
}
public Color TextForeColor
{
set { this.txtValue.ForeColor = value; }
}
public Color TextBackColor
{
set { this.txtValue.BackColor = value; }
}
/// <summary>
/// 文本只读属性
/// </summary>
public bool TextReadOnly
{
set { this.txtValue.ReadOnly = value; }
}
/// <summary>
/// 选择的颜色的显示名
/// </summary>
public override string Text
{
get
{
return this.txtValue.Text;
}
set
{
this.txtValue.Text = value;
}
}
private string value;
/// <summary>
/// 选择的颜色的定义值
/// </summary>
public string Value
{
get
{
if (this.value == null)
{
return String.Empty;
}
return this.value;
}
set
{
this.value = value;
this.dropUserControl.SelectedColorValue = value;
if (value == null || value == String.Empty)
{
this.picColor.Image = null;
this.txtValue.Text = String.Empty;
return;
}
this.Text = value.Split('.')[1];
this.dropUserControl.SelectedColor = ColorRepresentationHelper.GetColorByValue(value);
if (showColorBitmap == null)
{
showColorBitmap = new Bitmap(this.picColor.Width, this.picColor.Height);
showColorGraphics = Graphics.FromImage(showColorBitmap);
}
showColorGraphics.Clear(this.dropUserControl.SelectedColor);
showColorGraphics.DrawRectangle(Pens.Black, 0, 0, this.picColor.Width - 1, this.picColor.Height - 1);
this.picColor.Image = showColorBitmap;
}
}
private void frmTreeView_Deactivate(object sender, EventArgs e)
{
if (!this.btnComboButton.RectangleToScreen(this.btnComboButton.ClientRectangle).Contains(Cursor.Position))
{
this.formDropDown.Hide();
}
this.txtValue.Text = this.dropUserControl.SelectedColorName;
this.value = this.dropUserControl.SelectedColorValue;
if (this.dropUserControl.SelectedColorName != null && this.dropUserControl.SelectedColorName != String.Empty)
{
if (showColorBitmap == null)
{
showColorBitmap = new Bitmap(this.picColor.Width, this.picColor.Height);
showColorGraphics = Graphics.FromImage(showColorBitmap);
}
showColorGraphics.Clear(this.dropUserControl.SelectedColor);
showColorGraphics.DrawRectangle(Pens.Black, 0, 0, this.picColor.Width - 1, this.picColor.Height - 1);
this.picColor.Image = showColorBitmap;
}
else
{
this.picColor.Image = null;
}
}
/// <summary>
/// 点击三角按钮,显示Form
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToggleTreeView(object sender, EventArgs e)
{
if (!this.formDropDown.Visible)
{
if (this.formDropDown.Controls.Count == 0)
{
this.formDropDown.Controls.Add(this.dropUserControl);
this.formDropDown.Size = this.dropUserControl.Size;
//this.formDropDown.Owner = this.FindForm();
}
Rectangle CBRect = this.RectangleToScreen(this.ClientRectangle);
//this.formDropDown.BackColor = Color.White;
//this.formDropDown.Location = new Point(CBRect.X, CBRect.Y + this.txtBack.Height);
//设置弹出窗口的位置,默认显示在ComboBox的下部,但是如果下部不足以显示,调整位置到ComboBox的上方
Point formLocation = new Point(CBRect.X, CBRect.Y + this.txtBack.Height);
if (formLocation.Y + formDropDown.Height > Screen.PrimaryScreen.WorkingArea.Height)
{
formLocation = new Point(CBRect.X, CBRect.Y - formDropDown.Height);
}
this.formDropDown.Location = formLocation;
//this.DropUserControl.SetValue(this.txtValue.Text);
this.formDropDown.Show();
this.dropUserControl.ShowCurrentTabPage();
this.formDropDown.BringToFront();
}
else
{
this.formDropDown.Hide();
}
}
private void TreeViewComboBox_Load(object sender, EventArgs e)
{
ReLayout();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (txtBack == null)
{
return;
}
ReLayout();
}
private void ReLayout()
{
this.txtBack.Size = this.Size;
this.txtBack.SendToBack();
this.btnComboButton.Size = new Size(20, this.txtBack.ClientRectangle.Height + 2);
this.btnComboButton.Location = new Point(this.Width - this.btnComboButton.Width - 1, 1);
this.txtValue.Height = this.txtBack.ClientRectangle.Height;
this.txtValue.Width = this.txtBack.Width - this.btnComboButton.Width - 36;
//this.txtValue.Location = new Point(2, 3);
this.txtValue.Location = new Point(30, 4);
this.picColor.Height = this.txtBack.ClientRectangle.Height;
this.picColor.Location = new Point(2, 2);
}
public void SetDropDownFormOwner(bool ownerForm)
{
if (ownerForm)
{
if (this.OwnerForm != null)
{
this.formDropDown.Owner = this.OwnerForm;
}
else
{
this.formDropDown.Owner = this.FindForm();
}
}
else
{
this.formDropDown.Owner = null;
if (this.OwnerForm != null)
{
this.OwnerForm.Select();
}
else
{
this.FindForm().Select();
}
}
}
private void txtValue_TextChanged(object sender, EventArgs e)
{
if (this.OnSelectedChange != null)
{
OnSelectedChange(sender, e);
}
}
/// <summary>
/// 下拉选项按钮
/// 但是绘制的按钮没有xp样式的问题一时不好解决
/// </summary>
private class ComboButton : Button
{
ButtonState state;
/// <summary>
///
/// </summary>
public ComboButton()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(MouseEventArgs e)
{
state = ButtonState.Pushed;
base.OnMouseDown(e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnMouseUp(MouseEventArgs e)
{
state = ButtonState.Normal;
base.OnMouseUp(e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawComboButton(e.Graphics, 0, 0, this.Width, this.Height, state);
}
}
}
}