C# Winform 窗体美化

摘要:
1使用系统;2使用System.ComponentModel;3使用系统诊断;4使用系统图纸;5使用System.Drawing.Drawing2D;6使用System.Runtime.InteropServices;7使用System.线程;8使用System.Windows.F
  1 using System;
  2 using System.ComponentModel;
  3 using System.Diagnostics;
  4 using System.Drawing;
  5 using System.Drawing.Drawing2D;
  6 using System.Runtime.InteropServices;
  7 using System.Threading;
  8 using System.Windows.Forms;
  9 using XKW_E.Tool;
 10 using Starts2000.WindowsClassName.Core;
 11 namespace Beautify
 12 {
 13     public partial class MyForm : Form
 14     {
 15         #region 基本信息
 16         [Category("ControlBox")]
 17         [Description("设置标题栏图标的菜单")]
 18         [DisplayName("CaptionMenu")]
 19         public ContextMenuStrip menu { get; set; }
 20         public Bitmap TitleImage;
 21         public Bitmap CloseButtonImage;
 22         public Bitmap CloseButtonPressDownImage;
 23         public Bitmap CloseButtonHoverImage;
 24         public Bitmap MaximumButtonImage;
 25         public Bitmap MaximumButtonHoverImage;
 26         public Bitmap MaximumButtonPressDownImage;
 27         public Bitmap MaximumNormalButtonImage;
 28         public Bitmap MaximumNormalButtonHoverImage;
 29         public Bitmap MaximumNormalButtonPressDownImage;
 30         public Bitmap MinimumButtonImage;
 31         public Bitmap MinimumButtonHoverImage;
 32         public Bitmap MinimumButtonPressDownImage;
 33         public Bitmap HelpButtonImage;
 34         public Bitmap HelpButtonHoverImage;
 35         public Bitmap HelpButtonPressDownImage;
 36         struct NonClientSizeInfo
 37         {
 38             public Size CaptionButtonSize;
 39             public Size BorderSize;
 40             public int CaptionHeight;
 41             public Rectangle CaptionRect;
 42             public Rectangle Rect;
 43             public Rectangle ClientRect;
 44             public int Width;
 45             public int Height;
 46         };
 47 
 48         #endregion
 49 
 50         #region 主要
 51         public MyForm() : base()
 52         {
 53             this.ControlBox = false;
 54             TitleImage = Properties.Resources.Main_Title;
 55 
 56             CloseButtonImage = Properties.Resources.Close_Normal;
 57             CloseButtonHoverImage = Properties.Resources.Close_On;
 58             CloseButtonPressDownImage = Properties.Resources.Close_Down;
 59 
 60 
 61             MaximumButtonImage = Properties.Resources.Max_Normal;
 62             MaximumButtonHoverImage = Properties.Resources.Max_On;
 63             MaximumButtonPressDownImage = Properties.Resources.Max_Down;
 64 
 65 
 66             MaximumNormalButtonImage = Properties.Resources.Restore_Normal;
 67             MaximumNormalButtonHoverImage = Properties.Resources.Restore_On;
 68             MaximumNormalButtonPressDownImage = Properties.Resources.Restore_Down;
 69 
 70             MinimumButtonImage = Properties.Resources.Min_Normal;
 71             MinimumButtonHoverImage = Properties.Resources.Min_On;
 72             MinimumButtonPressDownImage = Properties.Resources.Min_Down;
 73         }
 74         protected override void WndProc(ref Message m)
 75         {
 76             switch (m.Msg)
 77             {
 78                 case WindowsMessage.WM_NCPAINT: //绘制
 79                     PaintBorder(m);
 80                     return;
 81                 case WindowsMessage.WM_NCACTIVATE: //标题栏激活
 82                     if (m.WParam == (IntPtr)WindowsMessage.WM_FALSE)
 83                     {
 84                         m.Result = (IntPtr)WindowsMessage.WM_TRUE;
 85                     }
 86                     return;
 87                 case WindowsMessage.WM_PAINT:
 88                     if (this.WindowState == FormWindowState.Normal)
 89                     {
 90                         PaintBorder(m);
 91                     }
 92                     break;
 93                 case WindowsMessage.WM_SIZE:
 94                     PaintBorder(m);
 95                     break;
 96                 case WindowsMessage.WM_ACTIVATE: //窗体激活
 97                     PaintBorder(m);
 98                     break;
 99                 case WindowsMessage.WM_NCCALCSIZE: //边框大小改变
100                     PaintBorder(m);
101                     break;
102                 case WindowsMessage.WM_EXITSIZEMOVE: //结束大小改变
103                     PaintBorder(m);
104                     break;
105                 case WindowsMessage.WM_SETCURSOR: //光标设置
106                     if (SetCursor(m)) return;
107                     break;
108                 case WindowsMessage.WM_NCRBUTTONDOWN:  //标题栏鼠标右键按下
109                     IconClick(m);
110                     break;
111                 case WindowsMessage.WM_NCLBUTTONUP://标题栏左键释放
112                     UpButton(m);
113                     break;
114                 case WindowsMessage.WM_NCMOUSEMOVE://标题栏鼠标移动
115                     MoveButton(m);
116                     break;
117                 case WindowsMessage.WM_NCLBUTTONDOWN://标题栏左键按下
118                     if (DownButton(m)) return;
119                     break;
120             }
121             base.WndProc(ref m);
122         }
123         protected override CreateParams CreateParams
124         {
125             get
126             {
127                 CreateParams cp = base.CreateParams;
128                 return cp;
129             }
130         }
131         public static IntPtr SetClassLong(HandleRef hWnd, int nIndex, IntPtr dwNewLong)
132         {
133             if (IntPtr.Size > 4)
134                 return Win32API.SetClassLongPtr64(hWnd, nIndex, dwNewLong);
135             else
136                 return new IntPtr(Win32API.SetClassLongPtr32(hWnd, nIndex, unchecked((uint)dwNewLong.ToInt32())));
137         }
138         #endregion
139 
140         #region 处理
141         private NonClientSizeInfo GetNonClientInfo(IntPtr hwnd)
142         {
143             NonClientSizeInfo info = new NonClientSizeInfo();
144             info.CaptionButtonSize = SystemInformation.CaptionButtonSize;
145             info.CaptionHeight = SystemInformation.CaptionHeight;
146 
147             switch (this.FormBorderStyle)
148             {
149                 case FormBorderStyle.Fixed3D:
150                     info.BorderSize = SystemInformation.FixedFrameBorderSize;
151                     break;
152                 case FormBorderStyle.FixedDialog:
153                     info.BorderSize = SystemInformation.FixedFrameBorderSize;
154                     break;
155                 case FormBorderStyle.FixedSingle:
156                     info.BorderSize = SystemInformation.FixedFrameBorderSize;
157                     break;
158                 case FormBorderStyle.FixedToolWindow:
159                     info.BorderSize = SystemInformation.FixedFrameBorderSize;
160                     info.CaptionButtonSize = SystemInformation.ToolWindowCaptionButtonSize;
161                     info.CaptionHeight = SystemInformation.ToolWindowCaptionHeight;
162                     break;
163                 case FormBorderStyle.Sizable:
164                     info.BorderSize = SystemInformation.FrameBorderSize;
165                     break;
166                 case FormBorderStyle.SizableToolWindow:
167                     info.CaptionButtonSize = SystemInformation.ToolWindowCaptionButtonSize;
168                     info.BorderSize = SystemInformation.FrameBorderSize;
169                     info.CaptionHeight = SystemInformation.ToolWindowCaptionHeight;
170                     break;
171                 default:
172                     info.BorderSize = SystemInformation.BorderSize;
173                     break;
174             }
175             RECT areatRect = new RECT();
176             Win32API.GetWindowRect(hwnd, ref areatRect);
177 
178             int width = areatRect.right - areatRect.left;
179             int height = areatRect.bottom - areatRect.top;
180 
181             info.Width = width;
182             info.Height = height;
183 
184             Point xy = new Point(areatRect.left, areatRect.top);
185             xy.Offset(-areatRect.left, -areatRect.top);
186 
187             info.CaptionRect = new Rectangle(xy.X, xy.Y + info.BorderSize.Height, width, info.CaptionHeight);
188             info.Rect = new Rectangle(xy.X, xy.Y, width, height);
189             info.ClientRect = new Rectangle(xy.X + info.BorderSize.Width,
190                 xy.Y + info.CaptionHeight + info.BorderSize.Height,
191                 width - info.BorderSize.Width * 2,
192                 height - info.CaptionHeight - info.BorderSize.Height * 2);
193             return info;
194         }
195         #endregion
196 
197         #region 绘制
198         bool painting = true;
199 
200         private void PaintBorder(Message m)
201         {
202             try
203             {
204                 PAINTSTRUCT paint = new PAINTSTRUCT();
205                 paint.rcPaint = new Rectangle(0, 0, this.Width, this.Height);
206                 if (painting)
207                 {
208 
209                     Win32API.BeginPaint(m.HWnd, ref paint);
210                     painting = false;
211                     NonClientSizeInfo info = GetNonClientInfo(m.HWnd);
212                     BufferedGraphicsContext context = BufferedGraphicsManager.Current;
213                     IntPtr hDC = Win32API.GetWindowDC(m.HWnd);
214                     DrawLeft(hDC, context);
215                     DrawRight(hDC, context);
216                     DrawBottom(hDC, context);
217                     DrawTitle(hDC, context, info);
218                     Win32API.EndPaint(m.HWnd, ref paint);
219                     painting = true;
220                     Win32API.ReleaseDC(Handle, hDC);
221                 }
222             }
223             catch (Exception)
224             {
225 
226             }
227         }
228 
229 
230         #region 边框
231         Rectangle title;
232         Rectangle btnRect;
233         Rectangle maxRect;
234         Rectangle minRect;
235         Rectangle helpRect;
236         Rectangle iconRect;
237         RectangleF rectText;
238         private void DrawTitleInfo(Graphics g, NonClientSizeInfo info)
239         {
240             int titleX;
241             int iconW = 1;
242             int iconH = 1;
243 
244             if (info.CaptionHeight < 20)
245             {
246                 iconW = 15;
247                 iconH = 20;
248             }
249             else
250             {
251                 iconW = 20;
252                 iconH = 25;
253             }
254             Size captionTitleSize = TextRenderer.MeasureText(this.Text, SystemFonts.CaptionFont);
255             Size iconSize = new Size(iconW, iconH);
256 
257             if (this.WindowState == FormWindowState.Maximized)
258             {
259                 if (this.ShowIcon &&
260                   this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&
261                   this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
262                 {
263                     iconRect = new Rectangle(new Point(info.BorderSize.Width + 9, info.BorderSize.Height / 2), iconSize);
264                     g.DrawIcon(this.Icon, iconRect);
265                     titleX = info.BorderSize.Width + iconSize.Width + info.BorderSize.Width;
266                 }
267                 else
268                 {
269                     titleX = info.BorderSize.Width;
270                 }
271                 rectText = new RectangleF(titleX + 7,
272                        (info.BorderSize.Height + info.CaptionHeight - captionTitleSize.Height) / 2 + 2 + info.BorderSize.Height / 2,
273                        info.CaptionRect.Width - info.BorderSize.Width * 2 - SystemInformation.MinimumWindowSize.Width,
274                        info.CaptionRect.Height);
275             }
276             else
277             {
278                 if (this.ShowIcon &&
279                     this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&
280                     this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
281                 {
282                     iconRect = new Rectangle(new Point(info.BorderSize.Width + 9, info.BorderSize.Height / 2), iconSize);
283                     g.DrawIcon(this.Icon, iconRect);
284                     titleX = info.BorderSize.Width + iconSize.Width + info.BorderSize.Width;
285                 }
286                 else
287                 {
288                     titleX = info.BorderSize.Width;
289                 }
290                 rectText = new RectangleF(titleX + 7,
291                         (info.BorderSize.Height + info.CaptionHeight - captionTitleSize.Height) / 2 + 2,
292                         info.CaptionRect.Width - info.BorderSize.Width * 2 - SystemInformation.MinimumWindowSize.Width,
293                         info.CaptionRect.Height);
294             }
295 
296             Rectangle titleText = new Rectangle(iconRect.Right, iconRect.Height, captionTitleSize.Width > 0 ? captionTitleSize.Width : 1, captionTitleSize.Height > 0 ? captionTitleSize.Height : 1);
297             LinearGradientBrush brush = new LinearGradientBrush(titleText, Color.White, Color.Gold, LinearGradientMode.Vertical);
298             SolidBrush brushBorder = new SolidBrush(Color.Gray);
299             g.DrawString(this.Text, new Font(SystemFonts.CaptionFont, FontStyle.Bold), brushBorder, rectText, StringFormat.GenericTypographic);
300             rectText.Offset(1, 1);
301             g.DrawString(this.Text, new Font(SystemFonts.CaptionFont, FontStyle.Bold), brush, rectText, StringFormat.GenericTypographic);
302 
303         }
304         private void DrawControlBox(Graphics g, NonClientSizeInfo info, bool closeBtn, bool maxBtn, bool minBtn, bool helpBtn)
305         {
306             Size iconSize = SystemInformation.IconSize;
307 
308             int closeBtnPosX = info.CaptionRect.Width - info.BorderSize.Width - CloseButtonImage.Width;
309             int maxBtnPosX = closeBtnPosX - MaximumButtonImage.Width;
310             int minBtnPosX = maxBtnPosX - MinimumButtonImage.Width;
311             // int helpBtnPosX = minBtnPosX - HelpButtonImage.Width;
312             int btnPosY = 0;
313             if (this.WindowState == FormWindowState.Maximized)
314             {
315                 btnPosY = info.BorderSize.Height;
316             }
317             btnRect = new Rectangle(new Point(closeBtnPosX, btnPosY), CloseButtonImage.Size);
318             maxRect = new Rectangle(new Point(maxBtnPosX, btnPosY), MaximumButtonImage.Size);
319             minRect = new Rectangle(new Point(minBtnPosX, btnPosY), MinimumButtonImage.Size);
320             //  helpRect = new Rectangle(new Point(helpBtnPosX, btnPosY), HelpButtonImage.Size);
321             g.DrawImage(CloseButtonImage, btnRect);
322 
323             if (this.MaximizeBox || this.MinimizeBox)
324             {
325                 if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&
326                     this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
327                 {
328                     if (this.WindowState == FormWindowState.Maximized)
329                     {
330                         g.DrawImage(MaximumNormalButtonImage, maxRect);
331                     }
332                     else
333                     {
334                         g.DrawImage(MaximumButtonImage, maxRect);
335                     }
336                     g.DrawImage(MinimumButtonImage, minRect);
337                 }
338             }
339             //if (this.HelpButton)
340             //{
341             //    if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&
342             //        this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
343             //    {
344             //        g.DrawImage(HelpButtonImage, helpRect);
345             //    }
346             //}
347         }
348         private void DrawTitle(IntPtr hDC, BufferedGraphicsContext context, NonClientSizeInfo info)
349         {
350             int leftAndRightWidth = (this.Width - this.ClientRectangle.Width) / 2;
351             int height = this.Height - this.ClientRectangle.Height - leftAndRightWidth > 0 ? this.Height - this.ClientRectangle.Height - leftAndRightWidth : 1;
352             int width = this.Width > 0 ? this.Width : 1;
353             title = new Rectangle(0, 0, width, height);
354             BufferedGraphics bg = context.Allocate(hDC, title);
355             Graphics g = bg.Graphics;
356             g.CompositingQuality = CompositingQuality.HighSpeed;
357             g.DrawImage(TitleImage, title);
358             DrawTitleInfo(g, info);
359             DrawControlBox(g, info, this.ControlBox, this.MaximizeBox, this.MinimizeBox, this.HelpButton);
360             bg.Render();
361             bg.Dispose();
362         }
363         private void DrawLeft(IntPtr hDC, BufferedGraphicsContext context)
364         {
365             int leftAndRightWidth = (this.Width - this.ClientRectangle.Width) / 2;
366             int height = this.Height;
367             int Width = (this.Width - this.ClientRectangle.Width) / 2;
368             int y = this.Height - this.ClientRectangle.Height - leftAndRightWidth;
369             Rectangle left = new Rectangle(0, y, Width, height);
370             BufferedGraphics bg = context.Allocate(hDC, left);
371             Graphics g = bg.Graphics;
372             g.CompositingQuality = CompositingQuality.HighSpeed;
373             g.DrawImage(Properties.Resources.Main_Left, left);
374             bg.Render();
375             bg.Dispose();
376         }
377         private void DrawRight(IntPtr hDC, BufferedGraphicsContext context)
378         {
379             int leftAndRightWidth = (this.Width - this.ClientRectangle.Width) / 2;
380             int height = this.Height;
381             int Width = (this.Width - this.ClientRectangle.Width) / 2;
382             int x = this.ClientRectangle.Width + (this.Width - this.ClientRectangle.Width) / 2;
383             int y = this.Height - this.ClientRectangle.Height - leftAndRightWidth;
384             Rectangle right = new Rectangle(x, y, Width, height);
385             BufferedGraphics bg = context.Allocate(hDC, right);
386             Graphics g = bg.Graphics;
387             g.CompositingQuality = CompositingQuality.HighSpeed;
388             g.DrawImage(Properties.Resources.Main_Right, right);
389             bg.Render();
390             bg.Dispose();
391         }
392         private void DrawBottom(IntPtr hDC, BufferedGraphicsContext context)
393         {
394             int leftAndRightWidth = (this.Width - this.ClientRectangle.Width) / 2;
395             int width = this.Width - 2;
396             int height = leftAndRightWidth;
397             int x = 1;
398             int y = this.Height - leftAndRightWidth;
399             Rectangle buttom = new Rectangle(x, y, width, height);
400             BufferedGraphics bg = context.Allocate(hDC, buttom);
401             Graphics g = bg.Graphics;
402             g.CompositingQuality = CompositingQuality.HighSpeed;
403             g.FillRectangle(new SolidBrush(Color.FromArgb(133, 199, 247)), buttom);
404             //g.DrawImage(Properties.Resources.Main_Bottom, buttom);
405             bg.Render();
406             bg.Dispose();
407         }
408         #endregion
409 
410         #region 按钮
411         Point pClick;
412         private void IconClick(Message m)
413         {
414             pClick = new Point((int)m.LParam);
415             pClick.Offset(-this.Left, -this.Top);
416             int locationX = pClick.X + this.Location.X;
417             int locationY = pClick.Y + this.Location.Y;
418             if (iconRect.Contains(pClick))
419             {
420                 if (menu != null)
421                 {
422                     menu.Show(locationX, locationY);
423                 }
424 
425             }
426         }
427         private void MoveButton(Message m)
428         {
429             pClick = new Point((int)m.LParam);
430             pClick.Offset(-this.Left, -this.Top);
431             if (title.Contains(pClick))
432             {
433                 IntPtr dc = Win32API.GetWindowDC(m.HWnd);
434                 Graphics g = Graphics.FromHdc(dc);
435 
436                 #region 关闭按钮
437                 if (btnRect.Contains(pClick))
438                 {
439                     g.DrawImage(CloseButtonHoverImage, btnRect);
440                 }
441                 else
442                 {
443                     g.DrawImage(CloseButtonImage, btnRect);
444                 }
445                 #endregion
446 
447                 #region 最大化最小化
448                 if (this.MaximizeBox || this.MinimizeBox)
449                 {
450                     if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&
451                         this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
452                     {
453                         if (this.WindowState == FormWindowState.Maximized)
454                         {
455                             if (this.MaximizeBox)
456                             {
457                                 if (maxRect.Contains(pClick))
458                                 {
459                                     g.DrawImage(MaximumNormalButtonHoverImage, maxRect);
460                                 }
461                                 else
462                                 {
463                                     g.DrawImage(MaximumNormalButtonImage, maxRect);
464                                 }
465                             }
466                             else
467                             {
468                                 g.DrawImage(MaximumNormalButtonImage, maxRect);
469                             }
470                         }
471                         else
472                         {
473                             if (this.MaximizeBox)
474                             {
475                                 if (maxRect.Contains(pClick))
476                                 {
477                                     g.DrawImage(MaximumButtonHoverImage, maxRect);
478                                 }
479                                 else
480                                 {
481                                     g.DrawImage(MaximumButtonImage, maxRect);
482                                 }
483                             }
484                             else
485                             {
486                                 g.DrawImage(MaximumButtonImage, maxRect);
487                             }
488                         }
489 
490                         if (this.MinimizeBox)
491                         {
492                             if (minRect.Contains(pClick))
493                             {
494                                 g.DrawImage(MinimumButtonHoverImage, minRect);
495                             }
496                             else
497                             {
498                                 g.DrawImage(MinimumButtonImage, minRect);
499                             }
500                         }
501                         else
502                         {
503                             g.DrawImage(MinimumButtonImage, minRect);
504                         }
505                     }
506                 }
507                 #endregion
508 
509                 #region 帮助
510                 //if (this.HelpButton)
511                 //{
512                 //    if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&
513                 //        this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
514                 //    {
515                 //        if (helpRect.Contains(pClick))
516                 //        {
517                 //            g.DrawImage(HelpButtonHoverImage, helpRect);
518                 //        }
519                 //        else
520                 //        {
521                 //            g.DrawImage(HelpButtonImage, helpRect);
522                 //        }
523                 //    }
524                 //}
525                 #endregion;
526                 g.Dispose();
527                 Win32API.ReleaseDC(Handle, dc);
528             }
529         }
530         private void UpButton(Message m)
531         {
532             pClick = new Point((int)m.LParam);
533             pClick.Offset(-this.Left, -this.Top);
534 
535             if (btnRect.Contains(pClick))
536             {
537                 this.Close();
538             }
539             if (this.MaximizeBox)
540                 if (maxRect.Contains(pClick))
541                 {
542                     if (this.WindowState == FormWindowState.Maximized)
543                     {
544                         this.WindowState = FormWindowState.Normal;
545                     }
546                     else
547                     {
548                         this.WindowState = FormWindowState.Maximized;
549                     }
550                 }
551             if (this.MinimizeBox)
552                 if (minRect.Contains(pClick))
553                 {
554                     if (this.WindowState == FormWindowState.Minimized)
555                     {
556                         this.WindowState = FormWindowState.Normal;
557                     }
558                     else
559                     {
560                         this.WindowState = FormWindowState.Minimized;
561                     }
562                 }
563             if (this.HelpButton)
564                 if (helpRect.Contains(pClick))
565                 {
566                     if (this.WindowState == FormWindowState.Minimized)
567                     {
568                         this.WindowState = FormWindowState.Normal;
569                     }
570                     else
571                     {
572                         this.WindowState = FormWindowState.Minimized;
573                     }
574                 }
575         }
576         private bool DownButton(Message m)
577         {
578             bool ret = false; //是否触发点击
579             pClick = new Point((int)m.LParam);
580             pClick.Offset(-this.Left, -this.Top);
581             if (title.Contains(pClick))
582             {
583                 IntPtr dc = Win32API.GetWindowDC(m.HWnd);
584                 Graphics g = Graphics.FromHdc(dc);
585                 #region 关闭按钮
586                 if (btnRect.Contains(pClick))
587                 {
588                     g.DrawImage(CloseButtonPressDownImage, btnRect);
589                     ret = true;
590                 }
591                 else
592                 {
593                     g.DrawImage(CloseButtonImage, btnRect);
594                 }
595                 #endregion
596 
597                 #region 最大化最小化
598                 if (this.MaximizeBox || this.MinimizeBox)
599                 {
600                     if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow &&
601                         this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow)
602                     {
603                         if (this.WindowState == FormWindowState.Maximized)
604                         {
605                             if (maxRect.Contains(pClick) && this.MaximizeBox)
606                             {
607                                 g.DrawImage(MaximumNormalButtonPressDownImage, maxRect);
608                                 ret = true;
609                             }
610                             else
611                             {
612                                 g.DrawImage(MaximumNormalButtonImage, maxRect);
613                             }
614                         }
615                         else
616                         {
617                             if (maxRect.Contains(pClick) && this.MaximizeBox)
618                             {
619                                 g.DrawImage(MaximumButtonPressDownImage, maxRect);
620                                 ret = true;
621                             }
622                             else
623                             {
624                                 g.DrawImage(MaximumButtonImage, maxRect);
625                             }
626                         }
627                         if (minRect.Contains(pClick) && this.MinimizeBox)
628                         {
629                             g.DrawImage(MinimumButtonPressDownImage, minRect);
630                             ret = true;
631                         }
632                         else
633                         {
634                             g.DrawImage(MinimumButtonImage, minRect);
635                         }
636                     }
637                 }
638                 #endregion
639 
640                 #region 帮助
641                 if (this.HelpButton)
642                 {
643                     if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&
644                         this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow)
645                     {
646                         if (helpRect.Contains(pClick))
647                         {
648                             g.DrawImage(HelpButtonPressDownImage, helpRect);
649                             ret = true;
650                         }
651                         else
652                         {
653                             g.DrawImage(HelpButtonImage, helpRect);
654                         }
655                     }
656                 }
657                 #endregion;
658                 g.Dispose();
659                 Win32API.ReleaseDC(m.HWnd, dc);
660             }
661             return ret;
662         }
663         #endregion
664 
665         #region  边缘设置
666 
667         private bool SetCursor(Message m)
668         {
669             bool flag = false;
670             if (btnRect.Contains(pClick))
671             {
672                 Win32API.SetCursor(Cursors.Default.Handle);
673                 flag = true;
674             }
675             if (this.MaximizeBox)
676                 if (maxRect.Contains(pClick))
677                 {
678                     Win32API.SetCursor(Cursors.Default.Handle);
679                     flag = true;
680                 }
681             if (this.MinimizeBox)
682                 if (minRect.Contains(pClick))
683                 {
684                     Win32API.SetCursor(Cursors.Default.Handle);
685                     flag = true;
686                 }
687             if (this.HelpButton)
688                 if (helpRect.Contains(pClick))
689                 {
690                     Win32API.SetCursor(Cursors.Default.Handle);
691                     flag = true;
692                 }
693             if (menu != null)
694             {
695                 if (iconRect.Contains(pClick))
696                 {
697                     Win32API.SetCursor(Cursors.Default.Handle);
698                     flag = true;
699                 }
700             }
701             return flag;
702         }
703         #endregion
704 
705         #endregion
706         private void InitializeComponent()
707         {
708             this.SuspendLayout();
709             // 
710             // MyForm
711             // 
712             this.ClientSize = new System.Drawing.Size(438, 395);
713             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
714             this.Name = "MyForm";
715             this.ResumeLayout(false);
716 
717         }
718     }
719 }

免责声明:文章转载自《C# Winform 窗体美化》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Ora-1157 ora-1110错误解决案例一枚vba获取word文档中的标题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Winform开发框架之单据窗体生成(主从表,流水单号)

项目源码下载地址:https://github.com/GarsonZhang/GZFramework.Demo 前言 1.在开始本节前请先重置代码为 chapter-03-start 懒人地址:https://github.com/GarsonZhang/GZFramework.ShareDemo/tree/chapter-03-start 2.创建表...

C# WinForm开发系列 DataGrid/DataGridView

在WinForm开发中,DataGrid/DataGridView被广泛使用于绑定数据库中数据进行呈现.整理一些关于DataGrid /DataGridView使用的文章,涉及DataGrid/DataGridView基本功能,自定义绘制控件,数据导入/导出(Excel),打印 等. 1.新数据网格简介(DataGridView) 2.自定义DataGri...

Winform开发常用控件之TreeView菜单导航和权限用法

TreeView一个很棒的控件,我们在做WEB开发时常常犯困的一个东东。当然这里介绍winform里面的用法唠。 先介绍几个属性吧,CheckBoxes设置为true的话树形节点前面会出现checkbox勾选框、ImageList绑定树形里面的图标(界面上放一个Imgelist控件,绑定上即可)、ShowLines(树形展开时显示线条)等等 哦,这个方法好...

WinForm 异步调用方法

假如要在一个线程中异步执行一个方法,则先创建一个该方法的委托类型,然后CLR会自动为该委托类型定义一个BeginInvoke方法和EndInvoke方法,我们就靠这两个方法异步调用委托类型指向的方法(这句话有点绕口,呵呵)BeginInvoke这个方法用于启动异步调用,该方法具有和要异步执行的方法具有相同的参数列表,只不过又多加了两个参数,多加的那两个参数...

【转】C# WinForm中的Label如何换行

        第一种是把Label的AutoSize属性设为False,手动修改Label的大小.这样的好处是会因内容的长度而自动换行,但是当内容的长度超过所设定的大小时,多出的内容就会无法显示.因此,这种方法适合于基本确定内容长度的时候使用.         第二种是把Label的Dock设为FILL,同时将AutoSize属性设为False,这种方法...

winform程序登陆后关闭登录窗体

用winform做程序的时候,我们一般都是在Program先启动登录窗体,然后登录成功后才创建主窗体,结果这就导致了登录窗体无法关闭 所以如果我们不在Program的程序入口先创建登录窗体的话就能完美实现登录成功后关闭登录窗体的功能了,直接贴代码 usingSystem; usingSystem.Collections.Generic; usingSyst...