1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Runtime.InteropServices;
7
using HANDLE = System.IntPtr;
8
using HWND = System.IntPtr;
9
namespace WuyinMuManager
10
{
11
/// <summary>
12
/// Form1 的摘要说明。
13
/// </summary>
14
public class WuyinMsnForm : System.Windows.Forms.Form
15
{
16
private System.ComponentModel.IContainer components=null;
17
Point location = new Point(0,0);
18
Point p= new Point(0,0);
19
Size size = new Size(600,400);
20
bool isMax = false;
21
private System.Windows.Forms.ContextMenu ControlMenu;
22
private System.Windows.Forms.MenuItem menuItem6;
23
private System.Windows.Forms.MenuItem menuRestore;
24
private System.Windows.Forms.MenuItem menuMove;
25
private System.Windows.Forms.MenuItem menuSize;
26
private System.Windows.Forms.MenuItem menuMin;
27
private System.Windows.Forms.MenuItem menuMax;
28
private System.Windows.Forms.MenuItem menuClose;
29
bool cursor=false;
30
Graphics g = null;
31
bool IsDiaLog = false;
32
public const int WM_NCPAINT = 0x85;
33
Color DeskTop = Color.FromArgb(122,150,223);
34
Color Caption = Color.FromArgb(216,228,248);
35
36
protected override void WndProc(ref Message m)
37
{
38
39
RectangleF rectf;
40
switch (m.Msg)
41
{
42
case WM_NCPAINT :
43
IntPtr hrgn = m.WParam;
44
if ( hrgn != (IntPtr)1 )
45
{
46
Region rgn = Region.FromHrgn(hrgn);
47
g = CreateGraphics();
48
rectf = rgn.GetBounds(g);
49
PaintNonClientArea(g, rectf);
50
}
51
else
52
{
53
g = CreateGraphics();
54
rectf = new RectangleF(0,-23, Width, 23);
55
PaintNonClientArea(g, rectf);
56
g.Dispose();
57
}
58
59
//base.WndProc(ref m);
60
break;
61
default :
62
base.WndProc (ref m);
63
break;
64
}
65
}
66
public bool isDialog
67
{
68
get
69
{
70
return IsDiaLog ;
71
}
72
set
73
{
74
IsDiaLog =value;
75
}
76
}
77
78
void PaintNonClientArea(Graphics g,RectangleF r)
79
{
80
//this.PainWindow(g);
81
}
82
83
public WuyinMsnForm()
84
{
85
//
86
// Windows 窗体设计器支持所必需的
87
//
88
InitializeComponent();
89
this.MinimumSize = new Size(160,20);
90
//int MyMenu = GetSystemMenu(this.Handle,0);
91
this.FormBorderStyle=FormBorderStyle.None;
92
//LoadMenu((IntPtr)MyMenu,"");
93
//MessageBox.Show(MyMenu.ToString());
94
//
95
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
96
//
97
}
98
/// <summary>
99
/// 清理所有正在使用的资源。
100
/// </summary>
101
protected override void Dispose( bool disposing )
102
{
103
if( disposing )
104
{
105
if(components != null)
106
{
107
components.Dispose();
108
}
109
}
110
base.Dispose( disposing );
111
}
112
protected override void OnPaint(PaintEventArgs e)
113
{
114
//int handle = Win32.User.GetWindowDC(this.Handle);
115
g = this.CreateGraphics();
116
//g = Graphics.FromHdc(((IntPtr)handle));
117
this.PainWindow(g);
118
//Win32.RECT Rect = new Win32.RECT();
119
//Win32.User.GetWindowRect((IntPtr)handle,ref Rect);
120
//Win32.User.ReleaseDC(this.Handle,(IntPtr)handle);
121
}
122
public override string Text
123
{
124
get
125
{
126
return base.Text;
127
}
128
set
129
{
130
DrawText(this.CreateGraphics());
131
base.Text = value;
132
}
133
}
134
135
protected override void OnMouseMove(MouseEventArgs e)
136
{
137
//this.Form1_MouseMove(this,e);
138
base.OnMouseMove (e);
139
}
140
141
void PainWindow(Graphics gx)
142
{
143
Bitmap bmp = new Bitmap(this.Width,this.Height);
144
Graphics g = Graphics.FromImage(bmp);
145
Pen pen;
146
//左边框1
147
pen = new Pen(Color.FromArgb(133,150,202));
148
g.DrawLine(pen,new Point(0,0),new Point(0,this.Height));
149
//上边框1
150
g.DrawLine(pen,new Point(0,0),new Point(this.Width,0));
151
pen = new Pen(Color.White);
152
g.DrawLine(pen,new Point(1,1),new Point(1,this.Height-1));
153
g.DrawLine(pen,new Point(1,1),new Point(this.Width-1,1));
154
//右边框1
155
pen = new Pen(Color.FromArgb(90,108,162));
156
g.DrawLine(pen,new Point(this.Width-1,1),new Point(this.Width-1,this.Height));
157
//下边框1
158
g.DrawLine(pen,new Point(1,this.Height-1),new Point(this.Width,this.Height-1));
159
pen = new Pen(Color.FromArgb(191,202,235));
160
g.DrawLine(pen,new Point(this.Width-2,2),new Point(this.Width-2,this.Height-2));
161
g.DrawLine(pen,new Point(2,this.Height-2),new Point(this.Width-2,this.Height-2));
162
Brush brush = new SolidBrush(Color.FromArgb(206,216,239));
163
g.FillRectangle(brush,2,2,this.Width-4,this.Height-4);
164
if(this.BackColor.ToKnownColor()!=KnownColor.Control)
165
{
166
brush = new SolidBrush(this.BackColor);
167
g.FillRectangle(brush,4,4,this.Width-8,this.Height-8);
168
}
169
else
170
{
171
this.BackColor = Caption;
172
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(4,4),new Size(this.Width-8,20)),Color.FromArgb(248,248,248),Color.FromArgb(220,226,240),3600,true);
173
g.FillRectangle(brush,4,4,this.Width-8,this.Height-8);
174
}
175
//画标题栏
176
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(4,4),new Size(this.Width-8,20)),Color.FromArgb(192,208,240),Color.FromArgb(248,248,248),120,true);
177
g.FillRectangle(brush,4,4,this.Width-8,20);
178
//画图标和标题
179
DrawIcon(g);
180
DrawText(g);
181
//画按钮
182
if(!this.isDialog)
183
{
184
this.DrawMinButton1(g);
185
if(this.MaximizeBox)
186
this.DrawMaxButton1(g);
187
}
188
this.DrawCloseButton1(g);
189
gx.DrawImage(bmp,0,0);
190
}
191
void DrawIcon(Graphics g)
192
{
193
g.DrawIcon(this.Icon,new Rectangle(new Point(6,6),new Size(16,16)));
194
}
195
void DrawText(Graphics g)
196
{
197
Brush brush = new SolidBrush(Color.FromArgb(61,77,129));
198
g.DrawString(this.Text,new Font("tohoma",9),brush,new Point(26,6),StringFormat.GenericDefault);
199
}
200
void DrawMinButton1(Graphics g)
201
{
202
//最小化
203
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));
204
g.FillRectangle(brush,this.Width-57,6,16,16);
205
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.FromArgb(199,207,231),Color.FromArgb(248,248,248),System.Drawing.Drawing2D. LinearGradientMode.Vertical);
206
g.FillRectangle(brush,this.Width-56,7,14,14);
207
Pen pen;
208
if(this.MinimizeBox)
209
pen = new Pen(Color.FromArgb(48,64,120),3);
210
else
211
pen = new Pen(Color.Silver,3);
212
g.DrawLine(pen,new Point(this.Width-53,17),new Point(this.Width-46,17));
213
}
214
void DrawMinButton2(Graphics g)
215
{
216
//最小化
217
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));
218
g.FillRectangle(brush,this.Width-57,6,16,16);
219
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.White,Color.Snow,System.Drawing.Drawing2D. LinearGradientMode.Vertical);
220
g.FillRectangle(brush,this.Width-56,7,14,14);
221
Pen pen;
222
if(this.MinimizeBox)
223
pen = new Pen(Color.FromArgb(48,64,120),3);
224
else
225
pen = new Pen(Color.Silver,3);
226
g.DrawLine(pen,new Point(this.Width-53,17),new Point(this.Width-46,17));
227
}
228
void DrawMaxButton1(Graphics g)
229
{
230
//最大化或者恢复
231
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));
232
g.FillRectangle(brush,this.Width-40,6,16,16);
233
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.FromArgb(199,207,231),Color.FromArgb(248,248,248),System.Drawing.Drawing2D. LinearGradientMode.Vertical);
234
g.FillRectangle(brush,this.Width-39,7,14,14);
235
if(!isMax)
236
{
237
brush = new SolidBrush(Color.FromArgb(48,64,120));
238
g.FillRectangle(brush,this.Width-37,9,10,10);
239
Pen pen = new Pen(Color.White,6);
240
g.DrawLine(pen,new Point(this.Width-36,15),new Point(this.Width-28,15));
241
}
242
else
243
{
244
brush = new SolidBrush(Color.FromArgb(48,64,120));
245
g.FillRectangle(brush,this.Width-34,9,7,7);
246
Pen pen = new Pen(Color.White,4);
247
g.DrawLine(pen,new Point(this.Width-33,13),new Point(this.Width-28,13));
248
g.FillRectangle(brush,this.Width-37,12,7,7);
249
g.DrawLine(pen,new Point(this.Width-36,16),new Point(this.Width-31,16));
250
}
251
}
252
void DrawMaxButton2(Graphics g)
253
{
254
//最大化或者恢复
255
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));
256
g.FillRectangle(brush,this.Width-40,6,16,16);
257
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.White,Color.Snow,System.Drawing.Drawing2D. LinearGradientMode.Vertical);
258
g.FillRectangle(brush,this.Width-39,7,14,14);
259
if(!isMax)
260
{
261
brush = new SolidBrush(Color.FromArgb(48,64,120));
262
g.FillRectangle(brush,this.Width-37,9,10,10);
263
Pen pen = new Pen(Color.White,6);
264
g.DrawLine(pen,new Point(this.Width-36,15),new Point(this.Width-28,15));
265
}
266
else
267
{
268
brush = new SolidBrush(Color.FromArgb(48,64,120));
269
g.FillRectangle(brush,this.Width-34,9,7,7);
270
Pen pen = new Pen(Color.White,4);
271
g.DrawLine(pen,new Point(this.Width-33,13),new Point(this.Width-28,13));
272
g.FillRectangle(brush,this.Width-37,12,7,7);
273
g.DrawLine(pen,new Point(this.Width-36,16),new Point(this.Width-31,16));
274
}
275
}
276
void DrawCloseButton1(Graphics g)
277
{
278
//关闭
279
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));
280
g.FillRectangle(brush,this.Width-23,6,16,16);
281
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.FromArgb(199,207,231),Color.FromArgb(248,248,248),System.Drawing.Drawing2D. LinearGradientMode.Vertical);
282
g.FillRectangle(brush,this.Width-22,7,14,14);
283
Pen pen = new Pen(Color.FromArgb(48,64,120),2);
284
g.DrawLine(pen,new Point(this.Width-20,9),new Point(this.Width-11,18));
285
g.DrawLine(pen,new Point(this.Width-11,9),new Point(this.Width-20,18));
286
}
287
void DrawCloseButton2(Graphics g)
288
{
289
//关闭
290
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));
291
g.FillRectangle(brush,this.Width-23,6,16,16);
292
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.Red,Color.Snow,System.Drawing.Drawing2D. LinearGradientMode.Vertical);
293
g.FillRectangle(brush,this.Width-22,7,14,14);
294
Pen pen = new Pen(Color.FromArgb(48,64,120),2);
295
g.DrawLine(pen,new Point(this.Width-20,9),new Point(this.Width-11,18));
296
g.DrawLine(pen,new Point(this.Width-11,9),new Point(this.Width-20,18));
297
}
298
Windows 窗体设计器生成的代码
385
protected override void OnCreateControl()
386
{
387
base.OnCreateControl ();
388
}
389
private void Form1_DoubleClick(object sender, EventArgs e)
390
{
391
if(!this.isDialog)
392
{
393
Point p = new Point(0,0);
394
GetCursorPos(out p);
395
if(p.X - this.Location.X>=this.Width-70 || p.Y - this.Location.Y>=20)
396
return;
397
if(this.Size.Width >=Screen.AllScreens[0].WorkingArea.Size.Width && this.Size.Height>=Screen.AllScreens[0].WorkingArea.Size.Height)
398
Restore();
399
else
400
{
401
Max();
402
}
403
}
404
}
405
[DllImport("user32")] public static extern int GetCursorPos(out Point lpPoint);
406
[DllImport("user32")] public static extern int GetSystemMenu(HWND hwnd, int bRevert);
407
[DllImport("user32")] public static extern int LoadMenu(HANDLE hInstance, string lpString);
408
//[DllImport("user32")] public static extern int MoveWindow(HWND hwnd, int x, int y, int nWidth, int nHeight, int bRepaint);
409
void Restore()
410
{
411
//MessageBox.Show(String.Format("W:{0},H:{1}",size.Width,size.Height));
412
this.Size=this.size;
413
this.Location = this.location;
414
this.WindowState=FormWindowState.Normal;
415
this.isMax=false;
416
this.Invalidate();
417
this.menuMax.Enabled=true;
418
this.menuRestore.Enabled=false;
419
this.menuSize.Enabled=true;
420
this.menuMove.Enabled=true;
421
//this.PainWindow(g);
422
}
423
void Max()
424
{
425
//MessageBox.Show(String.Format("W:{0},H:{1}",size.Width,size.Height));
426
//this.location = this.Location;
427
//this.size = this.Size;
428
this.isMax = true;
429
this.Size = Screen.AllScreens[0].WorkingArea.Size;
430
this.Location=new Point(0,0);
431
this.Invalidate();
432
this.menuMax.Enabled=false;
433
this.menuRestore.Enabled=true;
434
this.menuSize.Enabled=false;
435
this.menuMove.Enabled=false;
436
//this.PainWindow(g);
437
}
438
void CgBt(MouseEventArgs e)
439
{
440
if(e.X>this.Width-23 && e.X<this.Width-7 && e.Y>7 && e.Y<20)
441
{
442
this.DrawCloseButton2(g);
443
return;
444
}
445
else
446
{
447
this.DrawCloseButton1(g);
448
}
449
if(this.isDialog)
450
return;
451
if(e.X>this.Width-57 && e.X<this.Width-41 && e.Y>7 && e.Y<22)
452
{
453
this.DrawMinButton2(g);
454
return;
455
}
456
else
457
{
458
this.DrawMinButton1(g);
459
}
460
if(e.X>this.Width-40 && e.X<this.Width-24 && e.Y>7 && e.Y<22)
461
{
462
this.DrawMaxButton2(g);
463
return;
464
}
465
else
466
{
467
this.DrawMaxButton1(g);
468
}
469
}
470
private void Form1_MouseMove(object sender, MouseEventArgs e)
471
{
472
if(e.X>this.Width-70 && e.Y<25)
473
CgBt(e);
474
if(isMax)
475
return;
476
CursorResizeForm(e,this);
477
if(e.Button==MouseButtons.Left && e.Y>0 && e.Y<30 && e.X<this.Width-70 && e.Y<=20)
478
this.Location=new Point(Control.MousePosition.X-p.X,Control.MousePosition.Y-p.Y);
479
}
480
private void CursorResizeForm(MouseEventArgs e,Control control)
481
{ //鼠标控制窗体大小
482
if(e.Y>control.Height-10 && e.Y<control.Height && e.X>control.Width-10 && e.X<control.Width)
483
control.Cursor=System.Windows.Forms.Cursors.SizeNWSE;
484
else if(e.X>control.Width-6 && e.X<control.Width && control.Cursor!=System.Windows.Forms.Cursors.SizeNWSE)
485
control.Cursor=System.Windows.Forms.Cursors.SizeWE;
486
else if(e.Y>control.Height-6 && e.Y<control.Height && control.Cursor!=System.Windows.Forms.Cursors.SizeNWSE)
487
control.Cursor=System.Windows.Forms.Cursors.SizeNS;
488
else if(e.X>0 && e.X<6 && control.Cursor!=System.Windows.Forms.Cursors.SizeNESW)
489
control.Cursor=System.Windows.Forms.Cursors.SizeWE;
490
else if(!cursor)
491
control.Cursor=Cursors.Default;
492
if(e.Button==MouseButtons.Left)
493
{
494
if(control.Cursor==System.Windows.Forms.Cursors.SizeWE && e.X>16)
495
control.Width=Control.MousePosition.X-control.Left+3;
496
else if(control.Cursor==Cursors.SizeNWSE)
497
{
498
control.Width=Control.MousePosition.X-control.Left+3;
499
control.Height=Control.MousePosition.Y-control.Top+3;
500
}
501
else if(control.Cursor==System.Windows.Forms.Cursors.SizeNS)
502
control.Height=Control.MousePosition.Y-control.Top+3;
503
}
504
}
505
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
506
{
507
if(this.isDialog)
508
return;
509
cursor=true;
510
p=new Point(e.X,e.Y);
511
//location = this.Location;
512
//size = this.Size;
513
if(e.Button==MouseButtons.Right && e.X>0 && e.X<this.Width - 70 && e.Y>0 && e.Y<22)
514
{
515
this.ControlMenu.Show(this,new Point(e.X,e.Y));
516
return;
517
}
518
if(e.Button==MouseButtons.Left && e.X>0 && e.X<26 && e.Y>0 && e.Y<22)
519
this.ControlMenu.Show(this,new Point(0,20));
520
}
521
private void Form1_MouseUp(object sender, MouseEventArgs e)
522
{
523
if(e.Button!=MouseButtons.Left)
524
return;
525
cursor=false;
526
//三个控制按钮的
527
if(e.X>this.Width-24 && e.X<this.Width-6 && e.Y>7 && e.Y<20)
528
Close();
529
if(this.isDialog)
530
return;
531
if(e.X>this.Width-57 && e.X<this.Width-48 && e.Y>7 && e.Y<20)
532
this.WindowState=FormWindowState.Minimized;
533
if(e.X>this.Width-48 && e.X<this.Width-23 && e.Y>7 && e.Y<20)
534
{
535
if(isMax)
536
Restore();
537
else
538
Max();
539
}
540
}
541
private void Form1_SizeChanged(object sender, EventArgs e)
542
{
543
if(this.Size.Width<=20 || this.Size.Height<=20)
544
return;
545
if(!isMax)
546
{
547
this.location = this.Location;
548
this.size=this.Size;
549
}
550
//this.PainWindow(g);
551
}
552
protected override void DefWndProc(ref Message m)
553
{
554
//MessageBox.Show(m.ToString());
555
base.DefWndProc (ref m);
556
}
557
558
private void Form1_KeyDown(object sender, KeyEventArgs e)
559
{
560
if(e.Alt && e.KeyCode==Keys.Space)
561
this.ControlMenu.Show(this,new Point(4,22));
562
}
563
private void menuClose_Click(object sender, System.EventArgs e)
564
{
565
Close();
566
}
567
private void menuMax_Click(object sender, System.EventArgs e)
568
{
569
Max();
570
}
571
private void menuMin_Click(object sender, System.EventArgs e)
572
{
573
this.WindowState=FormWindowState.Minimized;
574
}
575
private void menuSize_Click(object sender, System.EventArgs e)
576
{
577
Pen pen = new Pen(Color.Black,4);
578
g.DrawLine(pen,new Point(0,0),new Point(0,this.Height));
579
g.DrawLine(pen,new Point(0,0),new Point(this.Width,0));
580
g.DrawLine(pen,new Point(this.Width,0),new Point(this.Width,this.Height));
581
g.DrawLine(pen,new Point(0,this.Height),new Point(this.Width,this.Height));
582
this.Cursor = Cursors.SizeAll;
583
}
584
private void menuMove_Click(object sender, System.EventArgs e)
585
{
586
Pen pen = new Pen(Color.Black,4);
587
g.DrawLine(pen,new Point(0,0),new Point(0,this.Height));
588
g.DrawLine(pen,new Point(0,0),new Point(this.Width,0));
589
g.DrawLine(pen,new Point(this.Width,0),new Point(this.Width,this.Height));
590
g.DrawLine(pen,new Point(0,this.Height),new Point(this.Width,this.Height));
591
mouse_event(MouseEvents.MOUSEEVENTF_MOVE,this.Location.X + this.Width/2,this.Location.Y + this.Height/2,0,0);
592
//MoveWindow(this.Handle,0,0,this.Width,this.Height,0);
593
this.Cursor = Cursors.SizeAll;
594
}
595
/// <summary>
596
/// 模拟鼠标事件
597
/// </summary>
598
/// <param name="dwFlags">鼠标事件的Enum</param>
599
/// <param name="dx">X座标</param>
600
/// <param name="dy">Y座标</param>
601
/// <param name="cButtons"></param>
602
/// <param name="dwExtraInfo"></param>
603
[DllImport("user32.dll")]
604
public static extern void mouse_event (MouseEvents dwFlags,long dx, long dy,long cButtons,long dwExtraInfo);
605
/// <summary>
606
/// 定义模拟鼠标的常数
607
/// </summary>
608
public enum MouseEvents:uint
609
{
610
MOUSEEVENTF_LEFTDOWN = 0x2,
611
MOUSEEVENTF_LEFTUP = 0x4,
612
MOUSEEVENTF_MIDDLEDOWN = 0x20,
613
MOUSEEVENTF_MIDDLEUP = 0x40,
614
MOUSEEVENTF_MOVE = 0x1,
615
MOUSEEVENTF_ABSOLUTE = 0x8000,
616
MOUSEEVENTF_RIGHTDOWN = 0x8,
617
MOUSEEVENTF_RIGHTUP = 0x10,
618
}
619
private void Form1_LocationChanged(object sender, EventArgs e)
620
{
621
if(!isMax)
622
location=Location;
623
}
624
protected override void OnControlAdded(ControlEventArgs e)
625
{
626
627
if(e.Control.Top<24)e.Control.Top += 24;
628
if(e.Control.Left<2)e.Control.Left+=2;
629
630
/*
631
if(e.Control.BackColor.ToKnownColor()==KnownColor.Control)
632
{
633
//((MyControl)e.Control).DoPaint();
634
//Color c = Color.FromArgb(255,220,226,240);
635
//e.Control.BackColor=c;
636
int C = (Win32.GDI.GetPixel((IntPtr)Win32.User.GetWindowDC((IntPtr)0),e.Control.Location.X,e.Control.Location.Y));
637
Color c = Color.FromName(C.ToString());
638
e.Control.BackColor = c;
639
}
640
base.OnControlAdded (e);
641
*/
642
}
643
private void WuyinMsnForm_Load(object sender, System.EventArgs e)
644
{
645
this.Height+=22;
646
this.Width+=2;
647
}
648
}
649
public class MyControl:Control
650
{
651
public void DoPaint()
652
{
653
}
654
public void DoPaint(PaintEventArgs e)
655
{
656
if(this.GetType().ToString()=="System.Windows.Forms.Label")
657
{
658
e.Graphics.DrawString(this.Text,this.Font,new SolidBrush(this.ForeColor),this.Location,StringFormat.GenericDefault);
659
return;
660
}
661
662
this.BackColor= Color.Empty;
663
Brush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(4,4),new Size(this.Width-8,20)),Color.FromArgb(248,248,248),Color.FromArgb(220,226,240),3600,true);
664
e.Graphics.FillRectangle(brush,e.ClipRectangle.X,e.ClipRectangle.Y,e.ClipRectangle.Width,e.ClipRectangle.Height);
665
}
666
protected override void OnPaint(PaintEventArgs e)
667
{
668
//base.OnPaint (e);
669
DoPaint(e);
670
}
671
}
672
}
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6
using System.Runtime.InteropServices;7
using HANDLE = System.IntPtr;8
using HWND = System.IntPtr;9
namespace WuyinMuManager10
{11
/// <summary>12
/// Form1 的摘要说明。13
/// </summary>14
public class WuyinMsnForm : System.Windows.Forms.Form15
{16
private System.ComponentModel.IContainer components=null;17
Point location = new Point(0,0);18
Point p= new Point(0,0);19
Size size = new Size(600,400);20
bool isMax = false;21
private System.Windows.Forms.ContextMenu ControlMenu;22
private System.Windows.Forms.MenuItem menuItem6;23
private System.Windows.Forms.MenuItem menuRestore;24
private System.Windows.Forms.MenuItem menuMove;25
private System.Windows.Forms.MenuItem menuSize;26
private System.Windows.Forms.MenuItem menuMin;27
private System.Windows.Forms.MenuItem menuMax;28
private System.Windows.Forms.MenuItem menuClose;29
bool cursor=false;30
Graphics g = null;31
bool IsDiaLog = false;32
public const int WM_NCPAINT = 0x85;33
Color DeskTop = Color.FromArgb(122,150,223);34
Color Caption = Color.FromArgb(216,228,248);35
36
protected override void WndProc(ref Message m)37
{38
39
RectangleF rectf;40
switch (m.Msg)41
{42
case WM_NCPAINT :43
IntPtr hrgn = m.WParam;44
if ( hrgn != (IntPtr)1 )45
{46
Region rgn = Region.FromHrgn(hrgn);47
g = CreateGraphics();48
rectf = rgn.GetBounds(g);49
PaintNonClientArea(g, rectf);50
}51
else52
{53
g = CreateGraphics();54
rectf = new RectangleF(0,-23, Width, 23);55
PaintNonClientArea(g, rectf);56
g.Dispose();57
}58
59
//base.WndProc(ref m);60
break;61
default :62
base.WndProc (ref m);63
break;64
}65
}66
public bool isDialog67
{68
get69
{70
return IsDiaLog ;71
}72
set73
{74
IsDiaLog =value;75
}76
}77
78
void PaintNonClientArea(Graphics g,RectangleF r)79
{80
//this.PainWindow(g);81
}82

83
public WuyinMsnForm()84
{85
//86
// Windows 窗体设计器支持所必需的87
//88
InitializeComponent();89
this.MinimumSize = new Size(160,20);90
//int MyMenu = GetSystemMenu(this.Handle,0);91
this.FormBorderStyle=FormBorderStyle.None;92
//LoadMenu((IntPtr)MyMenu,"");93
//MessageBox.Show(MyMenu.ToString());94
//95
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码96
//97
}98
/// <summary>99
/// 清理所有正在使用的资源。100
/// </summary>101
protected override void Dispose( bool disposing )102
{103
if( disposing )104
{105
if(components != null)106
{107
components.Dispose();108
}109
}110
base.Dispose( disposing );111
}112
protected override void OnPaint(PaintEventArgs e)113
{114
//int handle = Win32.User.GetWindowDC(this.Handle);115
g = this.CreateGraphics();116
//g = Graphics.FromHdc(((IntPtr)handle));117
this.PainWindow(g);118
//Win32.RECT Rect = new Win32.RECT();119
//Win32.User.GetWindowRect((IntPtr)handle,ref Rect);120
//Win32.User.ReleaseDC(this.Handle,(IntPtr)handle);121
}122
public override string Text123
{124
get125
{126
return base.Text;127
}128
set129
{130
DrawText(this.CreateGraphics());131
base.Text = value;132
}133
}134

135
protected override void OnMouseMove(MouseEventArgs e)136
{137
//this.Form1_MouseMove(this,e);138
base.OnMouseMove (e);139
}140

141
void PainWindow(Graphics gx)142
{143
Bitmap bmp = new Bitmap(this.Width,this.Height);144
Graphics g = Graphics.FromImage(bmp);145
Pen pen;146
//左边框1147
pen = new Pen(Color.FromArgb(133,150,202));148
g.DrawLine(pen,new Point(0,0),new Point(0,this.Height));149
//上边框1150
g.DrawLine(pen,new Point(0,0),new Point(this.Width,0));151
pen = new Pen(Color.White);152
g.DrawLine(pen,new Point(1,1),new Point(1,this.Height-1));153
g.DrawLine(pen,new Point(1,1),new Point(this.Width-1,1));154
//右边框1155
pen = new Pen(Color.FromArgb(90,108,162));156
g.DrawLine(pen,new Point(this.Width-1,1),new Point(this.Width-1,this.Height)); 157
//下边框1158
g.DrawLine(pen,new Point(1,this.Height-1),new Point(this.Width,this.Height-1));159
pen = new Pen(Color.FromArgb(191,202,235));160
g.DrawLine(pen,new Point(this.Width-2,2),new Point(this.Width-2,this.Height-2)); 161
g.DrawLine(pen,new Point(2,this.Height-2),new Point(this.Width-2,this.Height-2)); 162
Brush brush = new SolidBrush(Color.FromArgb(206,216,239));163
g.FillRectangle(brush,2,2,this.Width-4,this.Height-4);164
if(this.BackColor.ToKnownColor()!=KnownColor.Control)165
{166
brush = new SolidBrush(this.BackColor);167
g.FillRectangle(brush,4,4,this.Width-8,this.Height-8);168
}169
else170
{171
this.BackColor = Caption;172
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(4,4),new Size(this.Width-8,20)),Color.FromArgb(248,248,248),Color.FromArgb(220,226,240),3600,true);173
g.FillRectangle(brush,4,4,this.Width-8,this.Height-8); 174
}175
//画标题栏176
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(4,4),new Size(this.Width-8,20)),Color.FromArgb(192,208,240),Color.FromArgb(248,248,248),120,true);177
g.FillRectangle(brush,4,4,this.Width-8,20);178
//画图标和标题179
DrawIcon(g);180
DrawText(g);181
//画按钮182
if(!this.isDialog)183
{184
this.DrawMinButton1(g);185
if(this.MaximizeBox)186
this.DrawMaxButton1(g);187
}188
this.DrawCloseButton1(g);189
gx.DrawImage(bmp,0,0);190
}191
void DrawIcon(Graphics g)192
{193
g.DrawIcon(this.Icon,new Rectangle(new Point(6,6),new Size(16,16)));194
}195
void DrawText(Graphics g)196
{197
Brush brush = new SolidBrush(Color.FromArgb(61,77,129));198
g.DrawString(this.Text,new Font("tohoma",9),brush,new Point(26,6),StringFormat.GenericDefault);199
}200
void DrawMinButton1(Graphics g)201
{202
//最小化203
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));204
g.FillRectangle(brush,this.Width-57,6,16,16);205
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.FromArgb(199,207,231),Color.FromArgb(248,248,248),System.Drawing.Drawing2D. LinearGradientMode.Vertical);206
g.FillRectangle(brush,this.Width-56,7,14,14);207
Pen pen;208
if(this.MinimizeBox)209
pen = new Pen(Color.FromArgb(48,64,120),3);210
else211
pen = new Pen(Color.Silver,3);212
g.DrawLine(pen,new Point(this.Width-53,17),new Point(this.Width-46,17));213
}214
void DrawMinButton2(Graphics g)215
{216
//最小化217
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));218
g.FillRectangle(brush,this.Width-57,6,16,16);219
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.White,Color.Snow,System.Drawing.Drawing2D. LinearGradientMode.Vertical);220
g.FillRectangle(brush,this.Width-56,7,14,14);221
Pen pen;222
if(this.MinimizeBox)223
pen = new Pen(Color.FromArgb(48,64,120),3);224
else225
pen = new Pen(Color.Silver,3);226
g.DrawLine(pen,new Point(this.Width-53,17),new Point(this.Width-46,17));227
}228
void DrawMaxButton1(Graphics g)229
{230
//最大化或者恢复231
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));232
g.FillRectangle(brush,this.Width-40,6,16,16);233
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.FromArgb(199,207,231),Color.FromArgb(248,248,248),System.Drawing.Drawing2D. LinearGradientMode.Vertical);234
g.FillRectangle(brush,this.Width-39,7,14,14);235
if(!isMax)236
{237
brush = new SolidBrush(Color.FromArgb(48,64,120));238
g.FillRectangle(brush,this.Width-37,9,10,10);239
Pen pen = new Pen(Color.White,6);240
g.DrawLine(pen,new Point(this.Width-36,15),new Point(this.Width-28,15));241
}242
else243
{244
brush = new SolidBrush(Color.FromArgb(48,64,120));245
g.FillRectangle(brush,this.Width-34,9,7,7);246
Pen pen = new Pen(Color.White,4);247
g.DrawLine(pen,new Point(this.Width-33,13),new Point(this.Width-28,13));248
g.FillRectangle(brush,this.Width-37,12,7,7);249
g.DrawLine(pen,new Point(this.Width-36,16),new Point(this.Width-31,16));250
}251
}252
void DrawMaxButton2(Graphics g)253
{254
//最大化或者恢复255
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));256
g.FillRectangle(brush,this.Width-40,6,16,16);257
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.White,Color.Snow,System.Drawing.Drawing2D. LinearGradientMode.Vertical);258
g.FillRectangle(brush,this.Width-39,7,14,14);259
if(!isMax)260
{261
brush = new SolidBrush(Color.FromArgb(48,64,120));262
g.FillRectangle(brush,this.Width-37,9,10,10);263
Pen pen = new Pen(Color.White,6);264
g.DrawLine(pen,new Point(this.Width-36,15),new Point(this.Width-28,15));265
}266
else267
{268
brush = new SolidBrush(Color.FromArgb(48,64,120));269
g.FillRectangle(brush,this.Width-34,9,7,7);270
Pen pen = new Pen(Color.White,4);271
g.DrawLine(pen,new Point(this.Width-33,13),new Point(this.Width-28,13));272
g.FillRectangle(brush,this.Width-37,12,7,7);273
g.DrawLine(pen,new Point(this.Width-36,16),new Point(this.Width-31,16));274
}275
}276
void DrawCloseButton1(Graphics g)277
{278
//关闭279
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));280
g.FillRectangle(brush,this.Width-23,6,16,16);281
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.FromArgb(199,207,231),Color.FromArgb(248,248,248),System.Drawing.Drawing2D. LinearGradientMode.Vertical);282
g.FillRectangle(brush,this.Width-22,7,14,14);283
Pen pen = new Pen(Color.FromArgb(48,64,120),2);284
g.DrawLine(pen,new Point(this.Width-20,9),new Point(this.Width-11,18));285
g.DrawLine(pen,new Point(this.Width-11,9),new Point(this.Width-20,18));286
}287
void DrawCloseButton2(Graphics g)288
{289
//关闭290
Brush brush = new SolidBrush(Color.FromArgb(128,144,168));291
g.FillRectangle(brush,this.Width-23,6,16,16);292
brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(6,this.Width-63),new Size(17,17)),Color.Red,Color.Snow,System.Drawing.Drawing2D. LinearGradientMode.Vertical);293
g.FillRectangle(brush,this.Width-22,7,14,14);294
Pen pen = new Pen(Color.FromArgb(48,64,120),2);295
g.DrawLine(pen,new Point(this.Width-20,9),new Point(this.Width-11,18));296
g.DrawLine(pen,new Point(this.Width-11,9),new Point(this.Width-20,18));297
}298
Windows 窗体设计器生成的代码385
protected override void OnCreateControl()386
{387
base.OnCreateControl ();388
}389
private void Form1_DoubleClick(object sender, EventArgs e)390
{391
if(!this.isDialog)392
{393
Point p = new Point(0,0);394
GetCursorPos(out p);395
if(p.X - this.Location.X>=this.Width-70 || p.Y - this.Location.Y>=20)396
return;397
if(this.Size.Width >=Screen.AllScreens[0].WorkingArea.Size.Width && this.Size.Height>=Screen.AllScreens[0].WorkingArea.Size.Height)398
Restore();399
else400
{401
Max();402
}403
}404
}405
[DllImport("user32")] public static extern int GetCursorPos(out Point lpPoint);406
[DllImport("user32")] public static extern int GetSystemMenu(HWND hwnd, int bRevert);407
[DllImport("user32")] public static extern int LoadMenu(HANDLE hInstance, string lpString);408
//[DllImport("user32")] public static extern int MoveWindow(HWND hwnd, int x, int y, int nWidth, int nHeight, int bRepaint);409
void Restore()410
{411
//MessageBox.Show(String.Format("W:{0},H:{1}",size.Width,size.Height));412
this.Size=this.size;413
this.Location = this.location;414
this.WindowState=FormWindowState.Normal;415
this.isMax=false;416
this.Invalidate();417
this.menuMax.Enabled=true;418
this.menuRestore.Enabled=false;419
this.menuSize.Enabled=true;420
this.menuMove.Enabled=true;421
//this.PainWindow(g);422
}423
void Max()424
{425
//MessageBox.Show(String.Format("W:{0},H:{1}",size.Width,size.Height));426
//this.location = this.Location;427
//this.size = this.Size;428
this.isMax = true;429
this.Size = Screen.AllScreens[0].WorkingArea.Size;430
this.Location=new Point(0,0);431
this.Invalidate();432
this.menuMax.Enabled=false;433
this.menuRestore.Enabled=true;434
this.menuSize.Enabled=false;435
this.menuMove.Enabled=false;436
//this.PainWindow(g);437
}438
void CgBt(MouseEventArgs e)439
{440
if(e.X>this.Width-23 && e.X<this.Width-7 && e.Y>7 && e.Y<20)441
{442
this.DrawCloseButton2(g);443
return;444
}445
else446
{447
this.DrawCloseButton1(g);448
} 449
if(this.isDialog)450
return; 451
if(e.X>this.Width-57 && e.X<this.Width-41 && e.Y>7 && e.Y<22)452
{453
this.DrawMinButton2(g);454
return;455
}456
else457
{458
this.DrawMinButton1(g);459
}460
if(e.X>this.Width-40 && e.X<this.Width-24 && e.Y>7 && e.Y<22)461
{462
this.DrawMaxButton2(g); 463
return;464
}465
else466
{467
this.DrawMaxButton1(g); 468
}469
}470
private void Form1_MouseMove(object sender, MouseEventArgs e)471
{ 472
if(e.X>this.Width-70 && e.Y<25)473
CgBt(e); 474
if(isMax)475
return;476
CursorResizeForm(e,this);477
if(e.Button==MouseButtons.Left && e.Y>0 && e.Y<30 && e.X<this.Width-70 && e.Y<=20)478
this.Location=new Point(Control.MousePosition.X-p.X,Control.MousePosition.Y-p.Y);479
}480
private void CursorResizeForm(MouseEventArgs e,Control control)481
{ //鼠标控制窗体大小482
if(e.Y>control.Height-10 && e.Y<control.Height && e.X>control.Width-10 && e.X<control.Width)483
control.Cursor=System.Windows.Forms.Cursors.SizeNWSE; 484
else if(e.X>control.Width-6 && e.X<control.Width && control.Cursor!=System.Windows.Forms.Cursors.SizeNWSE)485
control.Cursor=System.Windows.Forms.Cursors.SizeWE;486
else if(e.Y>control.Height-6 && e.Y<control.Height && control.Cursor!=System.Windows.Forms.Cursors.SizeNWSE)487
control.Cursor=System.Windows.Forms.Cursors.SizeNS;488
else if(e.X>0 && e.X<6 && control.Cursor!=System.Windows.Forms.Cursors.SizeNESW)489
control.Cursor=System.Windows.Forms.Cursors.SizeWE;490
else if(!cursor)491
control.Cursor=Cursors.Default;492
if(e.Button==MouseButtons.Left)493
{494
if(control.Cursor==System.Windows.Forms.Cursors.SizeWE && e.X>16)495
control.Width=Control.MousePosition.X-control.Left+3;496
else if(control.Cursor==Cursors.SizeNWSE)497
{498
control.Width=Control.MousePosition.X-control.Left+3;499
control.Height=Control.MousePosition.Y-control.Top+3;500
}501
else if(control.Cursor==System.Windows.Forms.Cursors.SizeNS)502
control.Height=Control.MousePosition.Y-control.Top+3;503
}504
}505
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)506
{507
if(this.isDialog)508
return;509
cursor=true;510
p=new Point(e.X,e.Y);511
//location = this.Location;512
//size = this.Size;513
if(e.Button==MouseButtons.Right && e.X>0 && e.X<this.Width - 70 && e.Y>0 && e.Y<22)514
{515
this.ControlMenu.Show(this,new Point(e.X,e.Y));516
return;517
}518
if(e.Button==MouseButtons.Left && e.X>0 && e.X<26 && e.Y>0 && e.Y<22)519
this.ControlMenu.Show(this,new Point(0,20));520
}521
private void Form1_MouseUp(object sender, MouseEventArgs e)522
{523
if(e.Button!=MouseButtons.Left)524
return;525
cursor=false;526
//三个控制按钮的527
if(e.X>this.Width-24 && e.X<this.Width-6 && e.Y>7 && e.Y<20)528
Close();529
if(this.isDialog)530
return;531
if(e.X>this.Width-57 && e.X<this.Width-48 && e.Y>7 && e.Y<20)532
this.WindowState=FormWindowState.Minimized;533
if(e.X>this.Width-48 && e.X<this.Width-23 && e.Y>7 && e.Y<20)534
{535
if(isMax)536
Restore();537
else538
Max(); 539
}540
}541
private void Form1_SizeChanged(object sender, EventArgs e)542
{543
if(this.Size.Width<=20 || this.Size.Height<=20)544
return;545
if(!isMax)546
{547
this.location = this.Location;548
this.size=this.Size;549
}550
//this.PainWindow(g);551
}552
protected override void DefWndProc(ref Message m)553
{554
//MessageBox.Show(m.ToString());555
base.DefWndProc (ref m);556
}557

558
private void Form1_KeyDown(object sender, KeyEventArgs e)559
{560
if(e.Alt && e.KeyCode==Keys.Space)561
this.ControlMenu.Show(this,new Point(4,22)); 562
}563
private void menuClose_Click(object sender, System.EventArgs e)564
{565
Close();566
}567
private void menuMax_Click(object sender, System.EventArgs e)568
{569
Max();570
}571
private void menuMin_Click(object sender, System.EventArgs e)572
{573
this.WindowState=FormWindowState.Minimized;574
}575
private void menuSize_Click(object sender, System.EventArgs e)576
{577
Pen pen = new Pen(Color.Black,4);578
g.DrawLine(pen,new Point(0,0),new Point(0,this.Height));579
g.DrawLine(pen,new Point(0,0),new Point(this.Width,0));580
g.DrawLine(pen,new Point(this.Width,0),new Point(this.Width,this.Height));581
g.DrawLine(pen,new Point(0,this.Height),new Point(this.Width,this.Height));582
this.Cursor = Cursors.SizeAll;583
}584
private void menuMove_Click(object sender, System.EventArgs e)585
{586
Pen pen = new Pen(Color.Black,4);587
g.DrawLine(pen,new Point(0,0),new Point(0,this.Height));588
g.DrawLine(pen,new Point(0,0),new Point(this.Width,0));589
g.DrawLine(pen,new Point(this.Width,0),new Point(this.Width,this.Height));590
g.DrawLine(pen,new Point(0,this.Height),new Point(this.Width,this.Height));591
mouse_event(MouseEvents.MOUSEEVENTF_MOVE,this.Location.X + this.Width/2,this.Location.Y + this.Height/2,0,0);592
//MoveWindow(this.Handle,0,0,this.Width,this.Height,0);593
this.Cursor = Cursors.SizeAll;594
}595
/// <summary>596
/// 模拟鼠标事件597
/// </summary>598
/// <param name="dwFlags">鼠标事件的Enum</param>599
/// <param name="dx">X座标</param>600
/// <param name="dy">Y座标</param>601
/// <param name="cButtons"></param>602
/// <param name="dwExtraInfo"></param>603
[DllImport("user32.dll")]604
public static extern void mouse_event (MouseEvents dwFlags,long dx, long dy,long cButtons,long dwExtraInfo);605
/// <summary>606
/// 定义模拟鼠标的常数607
/// </summary>608
public enum MouseEvents:uint609
{610
MOUSEEVENTF_LEFTDOWN = 0x2,611
MOUSEEVENTF_LEFTUP = 0x4,612
MOUSEEVENTF_MIDDLEDOWN = 0x20,613
MOUSEEVENTF_MIDDLEUP = 0x40,614
MOUSEEVENTF_MOVE = 0x1,615
MOUSEEVENTF_ABSOLUTE = 0x8000,616
MOUSEEVENTF_RIGHTDOWN = 0x8,617
MOUSEEVENTF_RIGHTUP = 0x10,618
}619
private void Form1_LocationChanged(object sender, EventArgs e)620
{621
if(!isMax)622
location=Location;623
}624
protected override void OnControlAdded(ControlEventArgs e)625
{626
627
if(e.Control.Top<24)e.Control.Top += 24;628
if(e.Control.Left<2)e.Control.Left+=2;629
630
/*631
if(e.Control.BackColor.ToKnownColor()==KnownColor.Control)632
{ 633
//((MyControl)e.Control).DoPaint();634
//Color c = Color.FromArgb(255,220,226,240);635
//e.Control.BackColor=c; 636
int C = (Win32.GDI.GetPixel((IntPtr)Win32.User.GetWindowDC((IntPtr)0),e.Control.Location.X,e.Control.Location.Y));637
Color c = Color.FromName(C.ToString());638
e.Control.BackColor = c;639
}640
base.OnControlAdded (e);641
*/642
}643
private void WuyinMsnForm_Load(object sender, System.EventArgs e)644
{645
this.Height+=22;646
this.Width+=2;647
}648
}649
public class MyControl:Control650
{651
public void DoPaint()652
{653
}654
public void DoPaint(PaintEventArgs e)655
{656
if(this.GetType().ToString()=="System.Windows.Forms.Label")657
{658
e.Graphics.DrawString(this.Text,this.Font,new SolidBrush(this.ForeColor),this.Location,StringFormat.GenericDefault);659
return;660
}661
662
this.BackColor= Color.Empty;663
Brush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(new Point(4,4),new Size(this.Width-8,20)),Color.FromArgb(248,248,248),Color.FromArgb(220,226,240),3600,true);664
e.Graphics.FillRectangle(brush,e.ClipRectangle.X,e.ClipRectangle.Y,e.ClipRectangle.Width,e.ClipRectangle.Height); 665
}666
protected override void OnPaint(PaintEventArgs e)667
{668
//base.OnPaint (e);669
DoPaint(e);670
}671
}672
}


浙公网安备 33010602011771号