Cc edraws the windows form title bar and border

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

Reply to "Cc edraws the windows form title bar and border"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website