ControlBase.cs
14.1 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Asa.FaceControl
{
public partial class ControlBase : UserControl
{
//=====属性变量=====
private int _borderWidth = 2;
private ControlShape _faceShape = ControlShape.Rectangle;
//=====私有变量=====
private readonly Dictionary<ControlShape, Action> dicShape;
private bool borderEnterChanged;
//=====子类变量=====
internal RectangleF borderRect;
internal GraphicsPath borderPath;
internal IFaceThemeColor theme;
internal bool borderEnter;
internal bool mouseLeftDown;
internal bool mouseRightDown;
internal StringFormat stringFormat = new(StringFormatFlags.NoClip);
internal readonly SolidBrush BRUSH_DISABLED;
public ControlBase()
{
InitializeComponent();
theme = new DarkThemeColor();
BRUSH_DISABLED = new SolidBrush(theme.DISABLED);
AutoScaleMode = AutoScaleMode.None;
BackColor = theme.BACK;
ForeColor = theme.FORE;
Padding = new Padding(3);
dicShape = new Dictionary<ControlShape, Action>
{
{ ControlShape.Rectangle, ShapeRectangle },
{ ControlShape.RoundRectangle, ShapeRoundRectangle },
{ ControlShape.Ellipse, ShapeEllipse },
{ ControlShape.EllipseRectangle, ShapeEllipseRectangle },
{ ControlShape.Circle, ShapeCircle }
};
}
//=====公共方法=====
public Bitmap ToImage()
{
IntPtr hSrce = API.GetWindowDC(Handle);
IntPtr hDest = API.CreateCompatibleDC(hSrce);
IntPtr hBmp = API.CreateCompatibleBitmap(hSrce, Width, Height);
IntPtr hOldBmp = API.SelectObject(hDest, hBmp);
if (API.BitBlt(hDest, 0, 0, Width, Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
{
Bitmap bmp = Image.FromHbitmap(hBmp);
API.SelectObject(hDest, hOldBmp);
API.DeleteObject(hBmp);
API.DeleteDC(hDest);
API.ReleaseDC(Handle, hSrce);
return bmp;
}
return null;
}
//=====子类方法=====
protected virtual void CalcSize()
{
//边框位置
float x = _borderWidth / 2f;
float y = _borderWidth / 2f;
float width = ClientSize.Width - _borderWidth - 1;
float height = ClientSize.Height - _borderWidth - 1;
borderRect = new RectangleF(x, y, width, height);
//边框路径
borderPath = new GraphicsPath();
dicShape[_faceShape].Invoke();
}
protected virtual void DrawEnabled(Graphics g)
{
borderEnterChanged = borderEnter;
}
protected virtual void DrawDisabled(Graphics g)
{
}
public virtual void LostFocused()
{
}
//=====事件=====
[Browsable(true)]
public new event EventHandler TextChanged;
//=====属性=====
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int BorderWidth
{
get => _borderWidth;
set
{
_borderWidth = value > 0 ? value : 0;
CalcSize();
Refresh();
}
}
//[Browsable(true)]
//public override Color BackColor
//{
// get => base.BackColor;
// set
// {
// base.BackColor = value;
// Refresh();
// }
//}
//[Browsable(true)]
//public override Color ForeColor
//{
// get => base.ForeColor;
// set
// {
// base.ForeColor = value;
// Refresh();
// }
//}
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public new ControlShape BorderStyle
{
get => _faceShape;
set
{
_faceShape = value;
CalcSize();
Refresh();
}
}
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
get => base.Font;
set
{
base.Font = value;
CalcSize();
Refresh();
}
}
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get => base.Text;
set
{
base.Text = value;
CalcSize();
Refresh();
TextChanged?.Invoke(this, new EventArgs());
}
}
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new string AccessibleDescription { set => base.AccessibleDescription = value; get => base.AccessibleDescription; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new string AccessibleName { set => base.AccessibleName = value; get => base.AccessibleName; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new AccessibleRole AccessibleRole { set => base.AccessibleRole = value; get => base.AccessibleRole; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new bool AllowDrop { set => base.AllowDrop = value; get => base.AllowDrop; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new AutoScaleMode AutoScaleMode { set => base.AutoScaleMode = AutoScaleMode.None; get => AutoScaleMode.None; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new bool AutoScroll { get => base.AutoScroll; set => base.AutoScroll = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new Size AutoScrollMargin { get => base.AutoScrollMargin; set => base.AutoScrollMargin = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new Size AutoScrollMinSize { get => base.AutoScrollMinSize; set => base.AutoScrollMinSize = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new bool AutoSize { get => base.AutoSize; set => base.AutoSize = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new AutoSizeMode AutoSizeMode { get => base.AutoSizeMode; set => base.AutoSizeMode = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new AutoValidate AutoValidate { get => base.AutoValidate; set => base.AutoValidate = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new Image BackgroundImage { get => base.BackgroundImage; set => base.BackgroundImage = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new ImageLayout BackgroundImageLayout { get => base.BackgroundImageLayout; set => base.BackgroundImageLayout = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new bool CausesValidation { get => base.CausesValidation; set => base.CausesValidation = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new ContextMenuStrip ContextMenuStrip { get => base.ContextMenuStrip; set => base.ContextMenuStrip = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new Cursor Cursor { get => base.Cursor; set => base.Cursor = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new bool DoubleBuffered { get => base.DoubleBuffered; set => base.DoubleBuffered = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new ImeMode ImeMode { get => base.ImeMode; set => base.ImeMode = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new Size MaximumSize { get => base.MaximumSize; set => base.MaximumSize = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new Size MinimumSize { get => base.MinimumSize; set => base.MinimumSize = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new RightToLeft RightToLeft { get => base.RightToLeft; set => base.RightToLeft = value; }
//[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
//public new bool UseWaitCursor { get => base.UseWaitCursor; set => base.UseWaitCursor = value; }
private void ShapeRectangle()
{
borderPath.AddRectangle(borderRect);
}
private void ShapeRoundRectangle()
{
float n = borderRect.Height / 8f;
borderPath.AddLine(borderRect.X + n, borderRect.Y, borderRect.Right - n, borderRect.Y);
borderPath.AddArc(borderRect.Right - n - n, borderRect.Y, n + n, n + n, 270, 90);
borderPath.AddLine(borderRect.Right, borderRect.Y + n, borderRect.Right, borderRect.Bottom - n);
borderPath.AddArc(borderRect.Right - n - n, borderRect.Bottom - n - n, n + n, n + n, 0, 90);
borderPath.AddLine(borderRect.Right - n, borderRect.Bottom, borderRect.X + n, borderRect.Bottom);
borderPath.AddArc(borderRect.X, borderRect.Bottom - n - n, n + n, n + n, 90, 90);
borderPath.AddLine(borderRect.X, borderRect.Bottom - n, borderRect.X, borderRect.Y + n);
borderPath.AddArc(borderRect.X, borderRect.Y, n + n, n + n, 180, 90);
}
private void ShapeEllipse()
{
borderPath.AddEllipse(borderRect);
}
private void ShapeEllipseRectangle()
{
float n = borderRect.Height / 2f;
borderPath.AddLine(borderRect.X + n, borderRect.Y, borderRect.Right - n, borderRect.Y);
borderPath.AddArc(borderRect.Right - n - n, borderRect.Y, n + n, n + n, 270, 180);
borderPath.AddLine(borderRect.Right - n, borderRect.Bottom, borderRect.X + n, borderRect.Bottom);
borderPath.AddArc(borderRect.X, borderRect.Y, n + n, n + n, 90, 180);
}
private void ShapeCircle()
{
if (borderRect.Width > borderRect.Height)
borderPath.AddEllipse((borderRect.Width - borderRect.Height) / 2f, borderRect.Y, borderRect.Height, borderRect.Height);
else
borderPath.AddEllipse(borderRect.X, (borderRect.Height - borderRect.Width) / 2f, borderRect.Width, borderRect.Width);
}
private void DrawBorder(Graphics g)
{
//边框以外的部分
Region reg = new(ClientRectangle);
reg.Exclude(borderPath);
g.FillRegion(new SolidBrush(theme.BACK), reg);
//边框
if (_borderWidth > 0)
{
Color color = theme.DISABLED;
if (Enabled)
color = borderEnter ? theme.BORDER_ENTER : theme.BORDER_LEAVE;
Pen pen = new(color, _borderWidth);
g.DrawPath(pen, borderPath);
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0014) //禁掉清除背景消息
return;
base.WndProc(ref m);
}
private void ControlBase_Load(object sender, EventArgs e)
{
CalcSize();
Refresh();
}
private void ControlBase_Resize(object sender, EventArgs e)
{
CalcSize();
Refresh();
}
private void ControlBase_Paint(object sender, PaintEventArgs e)
{
BufferedGraphicsContext current = BufferedGraphicsManager.Current;
BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = bg.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(BackColor); //背景
if (Enabled)
DrawEnabled(g);
else
DrawDisabled(g);
DrawBorder(g);
bg.Render();
bg.Dispose();
}
private void ControlBase_MouseEnter(object sender, EventArgs e)
{
//cursorInside = true;
}
private void ControlBase_MouseLeave(object sender, EventArgs e)
{
//cursorInside = false;
borderEnter = false;
Refresh();
}
private void ControlBase_MouseDown(object sender, MouseEventArgs e)
{
if (TopLevelControl is FormBase frm)
frm.SetLostFocus(frm, Handle);
if (e.Button == MouseButtons.Left)
{
mouseLeftDown = true;
mouseRightDown = false;
}
else if (e.Button == MouseButtons.Right)
{
mouseLeftDown = false;
mouseRightDown = true;
}
Refresh();
}
private void ControlBase_MouseUp(object sender, MouseEventArgs e)
{
mouseLeftDown = false;
mouseRightDown = false;
Refresh();
}
private void ControlBase_MouseMove(object sender, MouseEventArgs e)
{
borderEnter = borderPath.IsVisible(e.Location);
if (borderEnterChanged != borderEnter)
Refresh();
}
}
}