首页 > Windows开发 > 详细

C# 重绘 windows 窗体标题栏和边框

时间:2015-12-11 23:52:55      阅读:386      评论:0      收藏:0      [点我收藏+]
  1 using System;  
  2 using System.Collections.Generic;  
  3 using System.Windows.Forms;  
  4 using System.ComponentModel;  
  5 using System.Drawing;  
  6 using System.Drawing.Drawing2D;  
  7 using System.Runtime.InteropServices;  
  8 using System.Diagnostics;  
  9 
 10 namespace CaptionBox {  
 11     public class ThemeForm : Form {  
 12         #region private structs  
 13 
 14         struct _NonClientSizeInfo {  
 15             public Size CaptionButtonSize;  
 16             public Size BorderSize;  
 17             public int CaptionHeight;  
 18             public Rectangle CaptionRect;  
 19             public Rectangle Rect;  
 20             public Rectangle ClientRect;  
 21             public int Width;  
 22             public int Height;  
 23         };  
 24 
 25         #endregion  
 26 
 27         #region constants  
 28 
 29         const int WM_NCACTIVATE = 0x86;  
 30         const int WM_NCPAINT = 0x85;  
 31         const int WM_NCLBUTTONDOWN = 0xA1;  
 32         const int WM_NCRBUTTONDOWN = 0x00A4;  
 33         const int WM_NCRBUTTONUP = 0x00A5;  
 34         const int WM_NCMOUSEMOVE = 0x00A0;  
 35         const int WM_NCLBUTTONUP = 0x00A2;  
 36         const int WM_NCCALCSIZE = 0x0083;  
 37         const int WM_NCMOUSEHOVER = 0x02A0;  
 38         const int WM_NCMOUSELEAVE = 0x02A2;  
 39         const int WM_NCHITTEST = 0x0084;  
 40         const int WM_NCCREATE = 0x0081;  
 41         //const int WM_RBUTTONUP = 0x0205;  
 42 
 43         const int WM_LBUTTONDOWN = 0x0201;  
 44         const int WM_CAPTURECHANGED = 0x0215;  
 45         const int WM_LBUTTONUP = 0x0202;  
 46         const int WM_SETCURSOR = 0x0020;  
 47         const int WM_CLOSE = 0x0010;  
 48         const int WM_SYSCOMMAND = 0x0112;  
 49         const int WM_MOUSEMOVE = 0x0200;  
 50         const int WM_SIZE = 0x0005;  
 51         const int WM_SIZING = 0x0214;  
 52         const int WM_GETMINMAXINFO = 0x0024;  
 53         const int WM_ENTERSIZEMOVE = 0x0231;  
 54         const int WM_WINDOWPOSCHANGING = 0x0046;  
 55 
 56         // FOR WM_SIZING MSG WPARAM  
 57         const int WMSZ_BOTTOM = 6;  
 58         const int WMSZ_BOTTOMLEFT = 7;  
 59         const int WMSZ_BOTTOMRIGHT = 8;  
 60         const int WMSZ_LEFT = 1;  
 61         const int WMSZ_RIGHT = 2;  
 62         const int WMSZ_TOP = 3;  
 63         const int WMSZ_TOPLEFT = 4;  
 64         const int WMSZ_TOPRIGHT = 5;  
 65 
 66         // left mouse button is down.  
 67         const int MK_LBUTTON = 0x0001;  
 68 
 69         const int SC_CLOSE = 0xF060;  
 70         const int SC_MAXIMIZE = 0xF030;  
 71         const int SC_MINIMIZE = 0xF020;  
 72         const int SC_RESTORE = 0xF120;  
 73         const int SC_CONTEXTHELP = 0xF180;  
 74 
 75         const int HTCAPTION = 2;  
 76         const int HTCLOSE = 20;  
 77         const int HTHELP = 21;  
 78         const int HTMAXBUTTON = 9;  
 79         const int HTMINBUTTON = 8;  
 80         const int HTTOP = 12;  
 81 
 82         const int SM_CYBORDER = 6;  
 83         const int SM_CXBORDER = 5;  
 84         const int SM_CYCAPTION = 4;  
 85 
 86         const int CS_DropSHADOW = 0x20000;  
 87         const int GCL_STYLE = (-26);  
 88 
 89         #endregion  
 90 
 91         #region windows api  
 92 
 93         [DllImport("User32.dll")]  
 94         private static extern IntPtr GetWindowDC(IntPtr hwnd);  
 95         [DllImport("User32.dll")]  
 96         [return: MarshalAs(UnmanagedType.Bool)]  
 97         private static extern bool GetWindowRect(IntPtr hwnd, ref _RECT rect);  
 98         [DllImport("User32.dll")]  
 99         private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);  
100 
101         [DllImport("user32.dll", CharSet = CharSet.Auto)]  
102         public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);  
103         [DllImport("user32.dll", CharSet = CharSet.Auto)]  
104         public static extern int GetClassLong(IntPtr hwnd, int nIndex);  
105 
106         #endregion  
107 
108         #region default constructor  
109 
110         public ThemeForm() {  
111             Text = "ThemeForm1";  
112             CloseButtonImage = Properties.Resources.close.ToBitmap();  
113             CloseButtonHoverImage = Properties.Resources.close2.ToBitmap();  
114             CloseButtonPressDownImage = Properties.Resources.close2.ToBitmap();  
115 
116             MaximumButtonImage = Properties.Resources.max.ToBitmap();  
117             MaximumButtonHoverImage = Properties.Resources.max2.ToBitmap();  
118             MaximumButtonPressDownImage = Properties.Resources.max2.ToBitmap();  
119 
120             MaximumNormalButtonImage = Properties.Resources.maxnorm.ToBitmap();  
121             MaximumNormalButtonHoverImage = Properties.Resources.maxnorm2.ToBitmap();  
122             MaximumNormalButtonPressDownImage = Properties.Resources.maxnorm2.ToBitmap();  
123 
124             MinimumButtonImage = Properties.Resources.min.ToBitmap();  
125             MinimumButtonHoverImage = Properties.Resources.min2.ToBitmap();  
126             MinimumButtonPressDownImage = Properties.Resources.min2.ToBitmap();  
127 
128             HelpButtonImage = Properties.Resources.help.ToBitmap();  
129             HelpButtonHoverImage = Properties.Resources.help2.ToBitmap();  
130             HelpButtonPressDownImage = Properties.Resources.help2.ToBitmap();  
131             CaptionColor = Brushes.White;  
132             CaptionBackgroundColor = Color.DimGray;  
133 
134             SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW); //API函数加载,实现窗体边框阴影效果  
135         }  
136 
137         #endregion  
138 
139         [DefaultValue("")]  
140         [Browsable(true)]  
141         [Category("ControlBox")]  
142         public virtual ContextMenuStrip CaptionContextMenu { get; set; }  
143 
144         protected virtual void OnCaptionContextMenu(int x, int y) {  
145             if (this.CaptionContextMenu != null)  
146                 this.CaptionContextMenu.Show(x, y);  
147         }  
148 
149         #region properties  
150 
151         [Category("ControlBox")]  
152         [Description("Close button image in control box.")]  
153         [DisplayName("CloseButtonImage")]  
154         [DesignOnly(true)]  
155         public Image CloseButtonImage { get; set; }  
156 
157         [Category("ControlBox")]  
158         [Description("Close button image pressed down in control box.")]  
159         [DisplayName("CloseButtonPressDownImage")]  
160         [DesignOnly(true)]  
161         public Image CloseButtonPressDownImage { get; set; }  
162 
163         [Category("ControlBox")]  
164         [Description("Close button image hover in control box.")]  
165         [DisplayName("CloseButtonHoverImage")]  
166         [DesignOnly(true)]  
167         public Image CloseButtonHoverImage { get; set; }  
168 
169         [Category("ControlBox")]  
170         [Description("Maximum button image in control box.")]  
171         [DisplayName("MaximumButtonImage")]  
172         [DesignOnly(true)]  
173         public Image MaximumButtonImage { get; set; }  
174 
175         [Category("ControlBox")]  
176         [Description("Maximum button hover image in control box.")]  
177         [DisplayName("MaximumButtonHoverImage")]  
178         [DesignOnly(true)]  
179         public Image MaximumButtonHoverImage { get; set; }  
180 
181         [Category("ControlBox")]  
182         [Description("Maximum button pressed down image in control box.")]  
183         [DisplayName("MaximumButtonPressDownImage")]  
184         [DesignOnly(true)]  
185         public Image MaximumButtonPressDownImage { get; set; }  
186 
187         [Category("ControlBox")]  
188         [Description("Maximum Normal button image in control box.")]  
189         [DisplayName("MaximumNormalButtonImage")]  
190         [DesignOnly(true)]  
191         public Image MaximumNormalButtonImage { get; set; }  
192 
193         [Category("ControlBox")]  
194         [Description("Maximum Normal button hover image in control box.")]  
195         [DisplayName("MaximumNormalButtonHoverImage")]  
196         [DesignOnly(true)]  
197         public Image MaximumNormalButtonHoverImage { get; set; }  
198 
199         [Category("ControlBox")]  
200         [Description("Maximum Normal button pressed down image in control box.")]  
201         [DisplayName("MaximumNormalButtonPressDownImage")]  
202         [DesignOnly(true)]  
203         public Image MaximumNormalButtonPressDownImage { get; set; }  
204 
205         [Category("ControlBox")]  
206         [Description("Minimum button image in control box.")]  
207         [DisplayName("MinimumButtonImage")]  
208         [DesignOnly(true)]  
209         public Image MinimumButtonImage { get; set; }  
210 
211         [Category("ControlBox")]  
212         [Description("Minimum button hover image in control box.")]  
213         [DisplayName("MinimumButtonHoverImage")]  
214         [DesignOnly(true)]  
215         public Image MinimumButtonHoverImage { get; set; }  
216 
217         [Category("ControlBox")]  
218         [Description("Minimum button pressed down image in control box.")]  
219         [DisplayName("MinimumButtonPressDownImage")]  
220         [DesignOnly(true)]  
221         public Image MinimumButtonPressDownImage { get; set; }  
222 
223         [Category("ControlBox")]  
224         [Description("Help button image in control box.")]  
225         [DisplayName("HelpButtonImage")]  
226         [DesignOnly(true)]  
227         public Image HelpButtonImage { get; set; }  
228 
229         [Category("ControlBox")]  
230         [Description("Help button hover image in control box.")]  
231         [DisplayName("HelpButtonHoverImage")]  
232         [DesignOnly(true)]  
233         public Image HelpButtonHoverImage { get; set; }  
234 
235         [Category("ControlBox")]  
236         [Description("Help button pressed down image in control box.")]  
237         [DisplayName("HelpButtonPressDownImage")]  
238         [DesignOnly(true)]  
239         public Image HelpButtonPressDownImage { get; set; }  
240 
241         [Category("CaptionColor")]  
242         [Description("The color of caption.")]  
243         [DisplayName("CaptionColor")]  
244         [DesignOnly(true)]  
245         public Brush CaptionColor { get; set; }  
246 
247         [Category("CaptionColor")]  
248         [Description("The color of caption.")]  
249         [DisplayName("CaptionBackgroundColor")]  
250         [DefaultValue(typeof(Color), "Black")]  
251         [DesignOnly(true)]  
252         public Color CaptionBackgroundColor { get; set; }  
253 
254         #endregion  
255 
256         #region help methods  
257 
258         private _NonClientSizeInfo GetNonClientInfo(IntPtr hwnd) {  
259             _NonClientSizeInfo info = new _NonClientSizeInfo();  
260             info.CaptionButtonSize = SystemInformation.CaptionButtonSize;  
261             info.CaptionHeight = SystemInformation.CaptionHeight;  
262 
263             switch (this.FormBorderStyle) {  
264                 case System.Windows.Forms.FormBorderStyle.Fixed3D:  
265                     info.BorderSize = SystemInformation.FixedFrameBorderSize;  
266                     break;  
267                 case System.Windows.Forms.FormBorderStyle.FixedDialog:  
268                     info.BorderSize = SystemInformation.FixedFrameBorderSize;  
269                     break;  
270                 case System.Windows.Forms.FormBorderStyle.FixedSingle:  
271                     info.BorderSize = SystemInformation.FixedFrameBorderSize;  
272                     break;  
273                 case System.Windows.Forms.FormBorderStyle.FixedToolWindow:  
274                     info.BorderSize = SystemInformation.FixedFrameBorderSize;  
275                     info.CaptionButtonSize = SystemInformation.ToolWindowCaptionButtonSize;  
276                     info.CaptionHeight = SystemInformation.ToolWindowCaptionHeight;  
277                     break;  
278                 case System.Windows.Forms.FormBorderStyle.Sizable:  
279                     info.BorderSize = SystemInformation.FrameBorderSize;  
280                     break;  
281                 case System.Windows.Forms.FormBorderStyle.SizableToolWindow:  
282                     info.CaptionButtonSize = SystemInformation.ToolWindowCaptionButtonSize;  
283                     info.BorderSize = SystemInformation.FrameBorderSize;  
284                     info.CaptionHeight = SystemInformation.ToolWindowCaptionHeight;  
285                     break;  
286                 default:  
287                     info.BorderSize = SystemInformation.BorderSize;  
288                     break;  
289             }  
290 
291             _RECT areatRect = new _RECT();  
292             GetWindowRect(hwnd, ref areatRect);  
293 
294             int width = areatRect.right - areatRect.left;  
295             int height = areatRect.bottom - areatRect.top;  
296 
297             info.Width = width;  
298             info.Height = height;  
299 
300             Point xy = new Point(areatRect.left, areatRect.top);  
301             xy.Offset(-areatRect.left, -areatRect.top);  
302 
303             info.CaptionRect = new Rectangle(xy.X, xy.Y + info.BorderSize.Height, width, info.CaptionHeight);  
304             info.Rect = new Rectangle(xy.X, xy.Y, width, height);  
305             info.ClientRect = new Rectangle(xy.X + info.BorderSize.Width,  
306                 xy.Y + info.CaptionHeight + info.BorderSize.Height,  
307                 width - info.BorderSize.Width * 2,  
308                 height - info.CaptionHeight - info.BorderSize.Height * 2);  
309 
310             return info;  
311         }  
312 
313         private void DrawTitle(Graphics g, _NonClientSizeInfo ncInfo, bool active) {  
314             int titleX;  
315 
316             if (this.ShowIcon &&  
317                 this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&  
318                 this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow) {  
319                 Size iconSize = SystemInformation.SmallIconSize;  
320                 g.DrawIcon(this.Icon, new Rectangle(new Point(ncInfo.BorderSize.Width, ncInfo.BorderSize.Height + (ncInfo.CaptionHeight - iconSize.Height) / 2), iconSize));  
321                 titleX = ncInfo.BorderSize.Width + iconSize.Width + ncInfo.BorderSize.Width;  
322             } else {  
323                 titleX = ncInfo.BorderSize.Width;  
324             }  
325 
326             SizeF captionTitleSize = g.MeasureString(this.Text, SystemFonts.CaptionFont);  
327             g.DrawString(this.Text, SystemFonts.CaptionFont, CaptionColor,  
328                     new RectangleF(titleX,  
329                         (ncInfo.BorderSize.Height + ncInfo.CaptionHeight - captionTitleSize.Height) / 2,  
330                         ncInfo.CaptionRect.Width - ncInfo.BorderSize.Width * 2 - SystemInformation.MinimumWindowSize.Width,  
331                         ncInfo.CaptionRect.Height), StringFormat.GenericTypographic);  
332         }  
333 
334         private void DrawBorder(Graphics g, _NonClientSizeInfo ncInfo, Brush background, bool active) {  
335             Rectangle borderTop = new Rectangle(ncInfo.Rect.Left,  
336                     ncInfo.Rect.Top,  
337                     ncInfo.Rect.Left + ncInfo.Rect.Width,  
338                     ncInfo.Rect.Top + ncInfo.BorderSize.Height);  
339             Rectangle borderLeft = new Rectangle(  
340                     new Point(ncInfo.Rect.Location.X, ncInfo.Rect.Location.Y + ncInfo.BorderSize.Height),  
341                     new Size(ncInfo.BorderSize.Width, ncInfo.ClientRect.Height + ncInfo.CaptionHeight + ncInfo.BorderSize.Height));  
342             Rectangle borderRight = new Rectangle(ncInfo.Rect.Left + ncInfo.Rect.Width - ncInfo.BorderSize.Width,  
343                     ncInfo.Rect.Top + ncInfo.BorderSize.Height,  
344                     ncInfo.BorderSize.Width,  
345                     ncInfo.ClientRect.Height + ncInfo.CaptionHeight + ncInfo.BorderSize.Height);  
346             Rectangle borderBottom = new Rectangle(ncInfo.Rect.Left + ncInfo.BorderSize.Width,  
347                     ncInfo.Rect.Top + ncInfo.Rect.Height - ncInfo.BorderSize.Height,  
348                     ncInfo.Rect.Width - ncInfo.BorderSize.Width * 2,  
349                     ncInfo.Rect.Height);  
350 
351             //Rectangle leftbottom = new Rectangle(new Point(ncInfo.Rect.Location.X, ncInfo.Rect.Height - ncInfo.BorderSize.Width * 2),  
352             //    new Size(ncInfo.BorderSize.Width * 2, ncInfo.BorderSize.Width * 2));  
353 
354             //g.FillPie(Brushes.Red, leftbottom, 90, 180);  
355             //g.FillRectangle(Brushes.Red, leftbottom);  
356             // top border  
357             g.FillRectangle(background, borderTop);  
358             // left border  
359             g.FillRectangle(background, borderLeft);  
360             // right border  
361             g.FillRectangle(background, borderRight);  
362             // bottom border  
363             g.FillRectangle(background, borderBottom);  
364         }  
365 
366         private void DrawCaption(IntPtr hwnd, bool active) {  
367             IntPtr dc;  
368             Graphics g;  
369             Size iconSize;  
370             _NonClientSizeInfo ncInfo;  
371             Brush backgroundColor = new SolidBrush(CaptionBackgroundColor);  
372             Brush foregroundColor = CaptionColor;  
373 
374             iconSize = SystemInformation.SmallIconSize;  
375 
376             dc = GetWindowDC(hwnd);  
377             ncInfo = GetNonClientInfo(hwnd);  
378             g = Graphics.FromHdc(dc);  
379 
380             g.FillRectangle(backgroundColor, ncInfo.CaptionRect);  
381 
382             DrawBorder(g, ncInfo, backgroundColor, active);  
383             DrawTitle(g, ncInfo, active);  
384             DrawControlBox(g, ncInfo, backgroundColor, this.ControlBox, this.MaximizeBox, this.MinimizeBox, this.HelpButton);  
385 
386             g.Dispose();  
387             ReleaseDC(hwnd, dc);  
388         }  
389 
390         private void DrawControlBox(Graphics g, _NonClientSizeInfo info, Brush background, bool closeBtn, bool maxBtn, bool minBtn, bool helpBtn) {  
391             if (this.ControlBox) {  
392                 int closeBtnPosX = info.CaptionRect.Left + info.CaptionRect.Width - info.BorderSize.Width - info.CaptionButtonSize.Width;  
393                 int maxBtnPosX = closeBtnPosX - info.CaptionButtonSize.Width;  
394                 int minBtnPosX = maxBtnPosX - info.CaptionButtonSize.Width;  
395                 int btnPosY = info.BorderSize.Height + (info.CaptionHeight - info.CaptionButtonSize.Height) / 2;  
396 
397                 Rectangle btnRect = new Rectangle(new Point(closeBtnPosX, btnPosY), info.CaptionButtonSize);  
398                 Rectangle maxRect = new Rectangle(new Point(maxBtnPosX, btnPosY), info.CaptionButtonSize);  
399                 Rectangle minRect = new Rectangle(new Point(minBtnPosX, btnPosY), info.CaptionButtonSize);  
400 
401                 Brush backgroundColor = new SolidBrush(CaptionBackgroundColor);  
402 
403                 g.FillRectangle(backgroundColor, btnRect);  
404                 g.FillRectangle(backgroundColor, maxRect);  
405                 g.FillRectangle(backgroundColor, minRect);  
406 
407                 g.DrawImage(CloseButtonImage, btnRect);  
408 
409                 if (this.MaximizeBox || this.MinimizeBox) {  
410                     if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&  
411                         this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow) {  
412                         if (this.WindowState == FormWindowState.Maximized) {  
413                             g.DrawImage(MaximumNormalButtonImage, maxRect);  
414                         } else {  
415                             g.DrawImage(MaximumButtonImage, maxRect);  
416                         }  
417                         g.DrawImage(MinimumButtonImage, minRect);  
418                     }  
419                 } else if (this.HelpButton) {  
420                     if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&  
421                         this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow) {  
422                         g.DrawImage(HelpButtonImage, maxRect);  
423                     }  
424                 }  
425             }  
426         }  
427 
428         #endregion  
429 
430         #region Major method WndProc  
431 
432         private int LOBYTE(long p) { return (int)(p & 0x0000FFFF); }  
433         private int HIBYTE(long p) { return (int)(p >> 16); }  
434 
435         protected override void WndProc(ref Message m) {  
436             if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.None) {  
437                 switch (m.Msg) {  
438                     case WM_NCPAINT:  
439                         DrawCaption(m.HWnd, Form.ActiveForm == this);  
440                         return;  
441                     case WM_NCACTIVATE:  
442                         DrawCaption(m.HWnd, m.WParam.ToInt32() > 0);  
443                         return;  
444                     case WM_NCRBUTTONDOWN: {  
445                             int posX, posY;  
446                             int wp = m.WParam.ToInt32();  
447                             long lp = m.LParam.ToInt64();  
448                             posX = LOBYTE(lp);  
449                             posY = HIBYTE(lp);  
450 
451                             if (wp == HTCAPTION) {  
452                                 Point pt = this.PointToClient(new Point(posX, posY));  
453                                 if (this.CaptionContextMenu != null) {  
454                                     this.CaptionContextMenu.Show(posX, posY);  
455                                     return;  
456                                 }  
457                             }  
458                             break;  
459                         }  
460                     case WM_SETCURSOR:  
461                         if (this.ControlBox) {  
462                             int posX, posY;  
463                             int wp = m.WParam.ToInt32();  
464                             long lp = m.LParam.ToInt64();  
465                             posX = LOBYTE(lp);  
466                             posY = HIBYTE(lp);  
467 
468                             Brush backgroundColor = new SolidBrush(CaptionBackgroundColor);  
469                             _NonClientSizeInfo ncInfo = GetNonClientInfo(m.HWnd);  
470                             IntPtr dc = GetWindowDC(m.HWnd);  
471 
472                             Graphics g = Graphics.FromHdc(dc);  
473                             int closeBtnPosX = ncInfo.CaptionRect.Left + ncInfo.CaptionRect.Width - ncInfo.BorderSize.Width - ncInfo.CaptionButtonSize.Width;  
474                             int maxBtnPosX, minBtnPosX;  
475                             maxBtnPosX = closeBtnPosX - ncInfo.CaptionButtonSize.Width;  
476                             minBtnPosX = maxBtnPosX - ncInfo.CaptionButtonSize.Width;  
477 
478                             int btnPosY = ncInfo.BorderSize.Height + (ncInfo.CaptionHeight - ncInfo.CaptionButtonSize.Height) / 2;  
479 
480                             Rectangle btnRect = new Rectangle(new Point(closeBtnPosX, btnPosY), ncInfo.CaptionButtonSize);  
481                             Rectangle maxRect = new Rectangle(new Point(maxBtnPosX, btnPosY), ncInfo.CaptionButtonSize);  
482                             Rectangle minRect = new Rectangle(new Point(minBtnPosX, btnPosY), ncInfo.CaptionButtonSize);  
483 
484                             g.FillRectangle(backgroundColor, btnRect);  
485                             g.FillRectangle(backgroundColor, maxRect);  
486                             g.FillRectangle(backgroundColor, minRect);  
487 
488                             if (posX != HTCLOSE) {  
489                                 g.DrawImage(CloseButtonImage, btnRect);  
490                             } else if (MouseButtons != System.Windows.Forms.MouseButtons.Left) {  
491                                 g.DrawImage(CloseButtonHoverImage, btnRect);  
492                             } else {  
493                                 g.DrawImage(CloseButtonPressDownImage, btnRect);  
494                             }  
495 
496                             if (this.MaximizeBox || this.MinimizeBox) {  
497                                 if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&  
498                                     this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow) {  
499                                     if (this.WindowState == FormWindowState.Maximized) {  
500                                         if (this.MaximizeBox) {  
501                                             if (posX != HTMAXBUTTON) {  
502                                                 g.DrawImage(MaximumNormalButtonImage, maxRect);  
503                                             } else if (MouseButtons != System.Windows.Forms.MouseButtons.Left) {  
504                                                 g.DrawImage(MaximumNormalButtonHoverImage, maxRect);  
505                                             } else {  
506                                                 g.DrawImage(MaximumNormalButtonPressDownImage, maxRect);  
507                                             }  
508                                         } else {  
509                                             g.DrawImage(MaximumNormalButtonImage, maxRect);  
510                                         }  
511                                     } else {  
512                                         if (this.MaximizeBox) {  
513                                             if (posX != HTMAXBUTTON) {  
514                                                 g.DrawImage(MaximumButtonImage, maxRect);  
515                                             } else if (MouseButtons != System.Windows.Forms.MouseButtons.Left) {  
516                                                 g.DrawImage(MaximumButtonHoverImage, maxRect);  
517                                             } else {  
518                                                 g.DrawImage(MaximumButtonPressDownImage, maxRect);  
519                                             }  
520                                         } else {  
521                                             g.DrawImage(MaximumButtonImage, maxRect);  
522                                         }  
523                                     }  
524 
525                                     if (this.MinimizeBox) {  
526                                         if (posX != HTMINBUTTON) {  
527                                             g.DrawImage(MinimumButtonImage, minRect);  
528                                         } else if (MouseButtons != System.Windows.Forms.MouseButtons.Left) {  
529                                             g.DrawImage(MinimumButtonHoverImage, minRect);  
530                                         } else {  
531                                             g.DrawImage(MinimumButtonPressDownImage, minRect);  
532                                         }  
533                                     } else {  
534                                         g.DrawImage(MinimumButtonImage, minRect);  
535                                     }  
536                                 }  
537                             } else if (this.HelpButton) {  
538                                 if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&  
539                                     this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow) {  
540                                     if (posX != HTHELP) {  
541                                         g.DrawImage(HelpButtonImage, maxRect);  
542                                     } else if (MouseButtons != System.Windows.Forms.MouseButtons.Left) {  
543                                         g.DrawImage(HelpButtonHoverImage, maxRect);  
544                                     } else {  
545                                         g.DrawImage(HelpButtonPressDownImage, maxRect);  
546                                     }  
547                                 }  
548                             }  
549 
550                             g.Dispose();  
551                             ReleaseDC(m.HWnd, dc);  
552                         }  
553                         break;  
554                     case WM_NCLBUTTONUP: {  
555                             int wp = m.WParam.ToInt32();  
556                             switch (wp) {  
557                                 case HTCLOSE:  
558                                     m.Msg = WM_SYSCOMMAND;  
559                                     m.WParam = new IntPtr(SC_CLOSE);  
560                                     break;  
561                                 case HTMAXBUTTON:  
562                                     if (this.MaximizeBox) {  
563                                         m.Msg = WM_SYSCOMMAND;  
564                                         if (this.WindowState == FormWindowState.Maximized) {  
565                                             m.WParam = new IntPtr(SC_RESTORE);  
566                                         } else {  
567                                             m.WParam = new IntPtr(SC_MAXIMIZE);  
568                                         }  
569                                     }  
570                                     break;  
571                                 case HTMINBUTTON:  
572                                     if (this.MinimizeBox) {  
573                                         m.Msg = WM_SYSCOMMAND;  
574                                         m.WParam = new IntPtr(SC_MINIMIZE);  
575                                     }  
576                                     break;  
577                                 case HTHELP:  
578                                     m.Msg = WM_SYSCOMMAND;  
579                                     m.WParam = new IntPtr(SC_CONTEXTHELP);  
580                                     break;  
581                                 default:  
582                                     break;  
583                             }  
584                             break;  
585                         }  
586 
587                     case WM_NCLBUTTONDOWN:  
588                         if (this.ControlBox) {  
589                             bool ret = false;  
590                             int posX, posY;  
591                             int wp = m.WParam.ToInt32();  
592                             long lp = m.LParam.ToInt64();  
593                             posX = LOBYTE(lp);  
594                             posY = HIBYTE(lp);  
595 
596                             _NonClientSizeInfo ncInfo = GetNonClientInfo(m.HWnd);  
597                             IntPtr dc = GetWindowDC(m.HWnd);  
598                             Brush backgroundColor = new SolidBrush(CaptionBackgroundColor);  
599 
600                             Graphics g = Graphics.FromHdc(dc);  
601                             int closeBtnPosX = ncInfo.CaptionRect.Left + ncInfo.CaptionRect.Width - ncInfo.BorderSize.Width - ncInfo.CaptionButtonSize.Width;  
602                             int maxBtnPosX, minBtnPosX;  
603                             int btnPosY = ncInfo.BorderSize.Height + (ncInfo.CaptionHeight - ncInfo.CaptionButtonSize.Height) / 2;  
604                             maxBtnPosX = closeBtnPosX - ncInfo.CaptionButtonSize.Width;  
605                             minBtnPosX = maxBtnPosX - ncInfo.CaptionButtonSize.Width;  
606 
607                             Rectangle btnRect = new Rectangle(new Point(closeBtnPosX, btnPosY), ncInfo.CaptionButtonSize);  
608                             Rectangle maxRect = new Rectangle(new Point(maxBtnPosX, btnPosY), ncInfo.CaptionButtonSize);  
609                             Rectangle minRect = new Rectangle(new Point(minBtnPosX, btnPosY), ncInfo.CaptionButtonSize);  
610 
611                             g.FillRectangle(backgroundColor, btnRect);  
612                             g.FillRectangle(backgroundColor, maxRect);  
613                             g.FillRectangle(backgroundColor, minRect);  
614 
615                             if (wp == HTCLOSE) {  
616                                 g.DrawImage(CloseButtonPressDownImage, btnRect);  
617                                 ret = true;  
618                             } else {  
619                                 g.DrawImage(CloseButtonImage, btnRect);  
620                             }  
621 
622                             if (this.MaximizeBox || this.MinimizeBox) {  
623                                 if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow &&  
624                                     this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow) {  
625                                     if (this.WindowState == FormWindowState.Maximized) {  
626                                         if (wp == HTMAXBUTTON && this.MaximizeBox) {  
627                                             minBtnPosX = maxBtnPosX - ncInfo.CaptionButtonSize.Width;  
628                                             g.DrawImage(MaximumNormalButtonPressDownImage, maxRect);  
629                                             ret = true;  
630                                         } else {  
631                                             g.DrawImage(MaximumNormalButtonImage, maxRect);  
632                                         }  
633                                     } else {  
634                                         if (wp == HTMAXBUTTON && this.MaximizeBox) {  
635                                             minBtnPosX = maxBtnPosX - ncInfo.CaptionButtonSize.Width;  
636                                             g.DrawImage(MaximumButtonPressDownImage, maxRect);  
637                                             ret = true;  
638                                         } else {  
639                                             g.DrawImage(MaximumButtonImage, maxRect);  
640                                         }  
641                                     }  
642                                     if (wp == HTMINBUTTON && this.MinimizeBox) {  
643                                         g.DrawImage(MinimumButtonPressDownImage, minRect);  
644                                         ret = true;  
645                                     } else {  
646                                         g.DrawImage(MinimumButtonImage, minRect);  
647                                     }  
648                                 }  
649                             } else if (this.HelpButton) {  
650                                 if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.FixedToolWindow &&  
651                                     this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.SizableToolWindow) {  
652                                     if (wp == HTHELP) {  
653                                         g.DrawImage(HelpButtonPressDownImage, maxRect);  
654                                         ret = true;  
655                                     } else {  
656                                         g.DrawImage(HelpButtonImage, maxRect);  
657                                     }  
658                                 }  
659                             }  
660 
661                             g.Dispose();  
662                             ReleaseDC(m.HWnd, dc);  
663 
664                             if (ret)  
665                                 return;  
666                         }  
667                         break;  
668                 }  
669             }  
670 
671             base.WndProc(ref m);  
672         }  
673 
674         #endregion  
675     }  
676 }  

 

C# 重绘 windows 窗体标题栏和边框

原文:http://www.cnblogs.com/ssdnif/p/5040530.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!