SurForm.cs
22.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Asa.Face
{
public partial class SurForm : Form
{
private bool _focus; //焦点
private Point _cursorPos; //鼠标坐标
private Thread tGetCursor;
private int _place = 5; //左上1 上2 右上3 左4 中间5 右6 左下7 下8 右下9
private bool _maximize; //最大化
private Rectangle _windowRect;
private Point[] sizeTriangle;
private int _titleHeight = 36;
private Font _titleFont = new Font("宋体", 12f);
private Rectangle _titleRect;
private RectangleF _titleText;
private bool _titleCenter = false;
private int _titleDownTime = 0;
private Rectangle _borderRect; //边框
private Pen _borderPen;
private int _borderW = 2;
private ControlButton _close = new ControlButton();
private ControlButton _max = new ControlButton();
private ControlButton _min = new ControlButton();
private readonly Pen MIN_PEN = new Pen(Common.FORE_COLOR, 1);
private readonly Pen MAX_PEN = new Pen(Common.FORE_COLOR, 1);
private readonly Pen CLOSE_PEN = new Pen(Common.FORE_COLOR, 2);
private readonly Size BTN_SIZE = new Size(48, 32);
public SurForm()
{
InitializeComponent();
AutoScaleMode = AutoScaleMode.None;
CheckForIllegalCrossThreadCalls = false;
}
[Browsable(false)]
public new AutoScaleMode AutoScaleMode { set => base.AutoScaleMode = value; get => base.AutoScaleMode; }
[Browsable(false)]
public new FormBorderStyle FormBorderStyle { set => base.FormBorderStyle = value; get => base.FormBorderStyle; }
[Browsable(false)]
public override Font Font { get => base.Font; set => base.Font = value; }
[Browsable(true), DefaultValue(true)]
public bool ChangeSize { set; get; } = true;
[Browsable(true)]
public new bool MaximizeBox
{
set
{
base.MaximizeBox = value;
CalcSize();
Refresh();
}
get => base.MaximizeBox;
}
[Browsable(true)]
public new bool MinimizeBox
{
set
{
base.MinimizeBox = value;
CalcSize();
Refresh();
}
get => base.MinimizeBox;
}
[Browsable(true)]
public override string Text
{
get => base.Text;
set
{
base.Text = value;
CalcSize();
Refresh();
}
}
[Browsable(true), DefaultValue(2)]
public int BorderWidth
{
set
{
if (value < 0)
_borderW = 0;
else
_borderW = value;
CalcSize();
Refresh();
}
get
{
return _borderW;
}
}
[Browsable(true)]
public Font TitleFont
{
get => _titleFont;
set
{
_titleFont = value;
CalcSize();
Refresh();
}
}
[Browsable(true), Category(""), Description("标题栏高度"), DefaultValue(36)]
public int TitleHeight
{
set
{
_titleHeight = value;
//Padding = new Padding(9, 9 + _titleHeight, 9, 9);
CalcSize();
Refresh();
}
get
{
return _titleHeight;
}
}
[Browsable(true), DefaultValue(false)]
public bool TitleCenter
{
set
{
_titleCenter = value;
CalcSize();
Refresh();
}
get
{
return _titleCenter;
}
}
[Browsable(false)]
public Bitmap ToImage()
{
IntPtr hscrdc = API.GetWindowDC(Handle);
IntPtr hbitmap = API.CreateCompatibleBitmap(hscrdc, Width, Height);
IntPtr hmemdc = API.CreateCompatibleDC(hscrdc);
API.SelectObject(hmemdc, hbitmap);
API.PrintWindow(Handle, hmemdc, 0);
Bitmap bmp = Image.FromHbitmap(hbitmap);
API.DeleteDC(hscrdc);//删除用过的对象
API.DeleteDC(hmemdc);//删除用过的对象
return bmp;
}
[Browsable(false)]
public void MaxForm()
{
if (_maximize)
{
Left = _windowRect.Left;
Top = _windowRect.Top;
Width = _windowRect.Width;
Height = _windowRect.Height;
_maximize = false;
}
else
{
_windowRect = new Rectangle(Location, Size);
Screen screen = Screen.FromHandle(Handle);
Top = screen.WorkingArea.Top;
Left = screen.WorkingArea.Left;
Width = screen.WorkingArea.Width;
Height = screen.WorkingArea.Height;
_maximize = true;
}
CalcSize();
Refresh();
}
private void CursorPos()
{
bool without = false;
while (true)
{
if (_focus)
{
if (_cursorPos.X != MousePosition.X || _cursorPos.Y != MousePosition.Y)
{
_cursorPos = MousePosition;
SetCursor(this);
//System.IO.File.AppendAllText(@"C:\Users\Asa\Documents\qq.txt",DateTime.Now.ToLongTimeString()+" "+ this.Name +" "+ Environment.TickCount+ "\r\n");
if (MousePosition.X >= Left && MousePosition.X <= Left + Width && MousePosition.Y >= Top && MousePosition.Y <= Top + Height)
{
//在窗口内部
without = false;
}
else
{
//在窗口外部
if (!without)
{
_min.Over = false;
_max.Over = false;
_close.Over = false;
without = true;
Refresh();
}
}
}
}
Thread.Sleep(100);
}
}
private void SetCursor(Control ctl)
{
foreach (Control cc in ctl.Controls)
{
if (!cc.Enabled || !cc.Visible)
{
continue;
}
Point pt = cc.PointToClient(_cursorPos);
if(cc is System.Windows.Forms.TextBox)
{
continue;
}
else if (cc is BaseCtls)
{
BaseCtls tt = (BaseCtls)cc;
SetCursor(tt);
}
else if (cc is BaseCtl)
{
((BaseCtl)cc).Inside = cc.ClientRectangle.Contains(pt);
}
else if (cc is BasePnl)
{
SetCursor(cc);
((BasePnl)cc).Inside = cc.ClientRectangle.Contains(pt);
}
}
}
private void CalcSize()
{
_borderRect = new Rectangle(_borderW / 2, _borderW / 2, Width - _borderW, Height - _borderW);
int x = Width - _borderW - BTN_SIZE.Width;
_close.Rect = new Rectangle(x, _borderW, BTN_SIZE.Width, BTN_SIZE.Height);
_close.IconLine = Shape.FormClose(_close.Rect, 10);
sizeTriangle = Shape.FormSizeTriangle(ClientRectangle, 5);
if (MaximizeBox)
{
x -= 48;
_max.Rect = new Rectangle(x, _borderW, BTN_SIZE.Width, BTN_SIZE.Height);
if (_maximize)
_max.IconLine = Shape.FormRestore(_max.Rect, 10);
else
_max.IconLine = Shape.FormMax(_max.Rect, 10);
}
if (MinimizeBox)
{
x -= 48;
_min.Rect = new Rectangle(x, _borderW, BTN_SIZE.Width, BTN_SIZE.Height);
_min.IconLine = Shape.FormMin(_min.Rect, 10);
}
_titleRect = new Rectangle(_borderW, _borderW, x, _titleHeight);
_borderPen = new Pen(Common.BLUE_COLOR, _borderW);
SizeF sf = CreateGraphics().MeasureString(Text, _titleFont);
float xx;
if (_titleCenter)
xx = _titleRect.X + (_titleRect.Width - sf.Width) / 2f;
else
xx = _titleRect.X + (_titleRect.Height - sf.Height) / 2f;
_titleText = new RectangleF(xx, _titleRect.Y + (_titleRect.Height - sf.Height) / 2f, sf.Width, sf.Height);
}
private void ChangedCursor(Point pt)
{
int space = 6;
Cursor cursor = Cursors.Default;
if (pt.X <= space && pt.Y <= space) //左上角
{
cursor = Cursors.SizeNWSE;
_place = 1;
}
else if (pt.X > space && pt.X < Width - space && pt.Y <= space) //上边
{
cursor = Cursors.SizeNS;
_place = 2;
}
else if (pt.X >= Width - space && pt.Y <= space) //右上角
{
cursor = Cursors.SizeNESW;
_place = 3;
}
else if (pt.X <= space && pt.Y > space && pt.Y < Height - space) //左边
{
cursor = Cursors.SizeWE;
_place = 4;
}
else if (pt.X >= Width - space && pt.Y > space && pt.Y < Height - space) //右边
{
cursor = Cursors.SizeWE;
_place = 6;
}
else if (pt.X <= space && pt.Y >= Height - space) //左下角
{
cursor = Cursors.SizeNESW;
_place = 7;
}
else if (pt.X > space && pt.X < Width - space && pt.Y >= Height - space) //下边
{
cursor = Cursors.SizeNS;
_place = 8;
}
else if (pt.X >= Width - space - space && pt.Y >= Height - space - space) //右下角
{
cursor = Cursors.SizeNWSE;
_place = 9;
}
else if (pt.X > space && pt.X < Width - space && pt.Y > space && pt.Y < Height - space) //中间
{
cursor = Cursors.Default;
_place = 5;
}
if (cursor != Cursor)
Cursor = cursor;
}
private void BaseForm_Load(object sender, EventArgs e)
{
AutoScaleMode = AutoScaleMode.None;
FormBorderStyle = FormBorderStyle.None;
BackColor = Common.BACK_COLOR;
//Padding = new Padding(12, 12 + _titleHeight, 12, 12);
if (DesignMode)
{
_focus = true;
}
else
{
_cursorPos = MousePosition;
tGetCursor = new Thread(new ThreadStart(CursorPos));
tGetCursor.Start();
//System.IO.File.AppendAllText(@"C:\Users\Asa\Documents\qq.txt", Name + " " + StartPosition.ToString() + "\r\n");
switch (StartPosition)
{
case FormStartPosition.CenterParent:
if (Parent == null)
{
Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2;
Top = (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2;
}
else
{
Left = (Parent.Width - Width) / 2;
Top = (Parent.Height - Height) / 2;
}
break;
case FormStartPosition.CenterScreen:
Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2;
Top = (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2;
break;
}
}
CalcSize();
Refresh();
}
private void BaseForm_FormClosed(object sender, FormClosedEventArgs e)
{
//if (tGetCursor != null) tGetCursor.Abort();
}
private void SurForm_FormClosing(object sender, FormClosingEventArgs e)
{
//System.Diagnostics.Process.GetCurrentProcess().Kill();
//System.Threading.Thread.CurrentThread.Abort();
if (tGetCursor != null) tGetCursor.Abort();
}
private void BaseForm_Activated(object sender, EventArgs e)
{
_focus = true;
Refresh();
}
private void BaseForm_Deactivate(object sender, EventArgs e)
{
_focus = false;
Refresh();
}
private void BaseForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (!_maximize && ChangeSize)
{
switch (_place)
{
case 1:
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, 0xF004, 0);
break;
case 2:
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, 0xF003, 0);
break;
case 3:
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, 0xF005, 0);
break;
case 4:
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, 0xF001, 0);
break;
case 5: break;
case 6:
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, 0xF002, 0);
break;
case 7:
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, 0xF007, 0);
break;
case 8:
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, 0xF006, 0);
break;
case 9:
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, 0xF008, 0);
break;
}
}
if (_place == 5)
{
if (_close.Over)
{
_close.Down = true;
Refresh();
}
else if (_max.Over)
{
_max.Down = true;
Refresh();
}
else if (_min.Over)
{
_min.Down = true;
Refresh();
}
else
{
if (_titleRect.Contains(e.Location))
{
if (MaximizeBox)
{
if (Environment.TickCount - _titleDownTime < 500)
MaxForm();
else if (!_maximize)
{
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, API.SC_MOVE + API.HTCAPTION, 0);
}
_titleDownTime = Environment.TickCount;
}
else
{
if (!_maximize)
{
API.ReleaseCapture();
API.SendMessage(Handle, API.WM_SYSCOMMAND, API.SC_MOVE + API.HTCAPTION, 0);
}
}
}
}
}
}
}
private void BaseForm_MouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
}
else
{
int n = 0;
if (!_maximize && ChangeSize)
ChangedCursor(e.Location);
bool bb = _close.Rect.Contains(e.Location);
if (bb != _close.Over)
{
_close.Over = bb;
n++;
}
if (MaximizeBox)
{
bb = _max.Rect.Contains(e.Location);
if (bb != _max.Over)
{
_max.Over = bb;
n++;
}
}
if (MinimizeBox)
{
bb = _min.Rect.Contains(e.Location);
if (bb != _min.Over)
{
_min.Over = bb;
n++;
}
}
if (n > 0)
{
Refresh();
}
}
}
private void BaseForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
if (_close.Down && _close.Over)
{
_close.Over = false;
Refresh();
Close();
}
else if (_max.Down && _max.Over)
{
_max.Over = false;
MaxForm();
}
else if (_min.Down && _min.Over)
{
_min.Over = false;
Refresh();
WindowState = FormWindowState.Minimized;
}
_close.Down = false;
_max.Down = false;
_min.Down = false;
}
private void BaseForm_Resize(object sender, EventArgs e)
{
CalcSize();
Refresh();
}
private void BaseForm_Paint(object sender, PaintEventArgs e)
{
BufferedGraphicsContext current = BufferedGraphicsManager.Current;
BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = bg.Graphics;
g.Clear(Common.BACK_COLOR); //背景
//窗口焦点
if (_focus)
{
if (_borderW > 0)
g.DrawRectangle(_borderPen, _borderRect);
//g.FillRectangle(Common.TITLE_BRUSH, _titleRect);
g.DrawString(Text, _titleFont, Common.FORE_BRUSH, _titleText);
}
else
{
g.DrawString(Text, _titleFont, Common.FORE_BRUSH_LOST, _titleText);
}
//关闭按钮
if (_close.Down && _close.Over)
g.FillRectangle(Common.BLUE_BRUSH, _close.Rect);
else if (_close.Over)
g.FillRectangle(Common.OVER_BRUSH, _close.Rect);
for (int i = 0; i < _close.IconLine.Length; i += 2)
g.DrawLine(CLOSE_PEN, _close.IconLine[i], _close.IconLine[i + 1]);
//最大化按钮
if (MaximizeBox)
{
if (_max.Down && _max.Over)
g.FillRectangle(Common.BLUE_BRUSH, _max.Rect);
else if (_max.Over)
g.FillRectangle(Common.OVER_BRUSH, _max.Rect);
for (int i = 0; i < _max.IconLine.Length; i += 2)
g.DrawLine(MAX_PEN, _max.IconLine[i], _max.IconLine[i + 1]);
}
//最小化按钮
if (MinimizeBox)
{
if (_min.Down && _min.Over)
g.FillRectangle(Common.BLUE_BRUSH, _min.Rect);
else if (_min.Over)
g.FillRectangle(Common.OVER_BRUSH, _min.Rect);
for (int i = 0; i < _min.IconLine.Length; i += 2)
g.DrawLine(MIN_PEN, _min.IconLine[i], _min.IconLine[i + 1]);
}
//正常窗口显示右下角三角形
if (!_maximize && ChangeSize)
{
Pen pen = new Pen(Common.FORE_COLOR, 1) { DashStyle = System.Drawing.Drawing2D.DashStyle.Dot };
for (int i = 0; i < sizeTriangle.Length; i += 2)
g.DrawLine(pen, sizeTriangle[i], sizeTriangle[i + 1]);
}
//设计模式
if (DesignMode)
{
Pen p1 = new Pen(Color.Red, 1) { DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot };
g.DrawLine(p1, _borderW, _borderW + _titleHeight, Width - _borderW - 1, _borderW + _titleHeight);
}
bg.Render();
bg.Dispose();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0014) // 禁掉清除背景消息
return;
base.WndProc(ref m);
}
}
}