Winform 去掉 最大化 最小化 关闭按钮(不是关闭按钮变灰)终极解决办法

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Drawing.Drawing2D;  
  10. using System.Runtime.InteropServices;  
  11.   
  12. namespace WinDemo  
  13. {  
  14.     public partial class Form5 : Form  
  15.     {  
  16.         public Form5()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         /// <summary>  
  22.         /// Button 按钮重绘事件  
  23.         /// </summary>  
  24.         /// <param name="sender"></param>  
  25.         /// <param name="e"></param>  
  26.         private void button1_Paint(object sender, PaintEventArgs e)  
  27.         {  
  28.             GraphicsPath myPath = new GraphicsPath();  
  29.             Rectangle rect = new Rectangle(0,0,574,362);//后面2个数据调整窗体大小  
  30.             myPath.AddRectangle(rect);  
  31.             this.Region = new Region(myPath);  
  32.         }  
  33.   
  34.         [DllImport("user32.dll")]  
  35.         static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);  
  36.   
  37.         [DllImport("User32.dll")]  
  38.   
  39.         private static extern IntPtr GetWindowDC(IntPtr hWnd);  
  40.   
  41.         protected override void WndProc(ref System.Windows.Forms.Message m)  
  42.         {  
  43.             const int WM_NCPAINT = 0x85;  
  44.             base.WndProc(ref m);  
  45.   
  46.             if (m.Msg == WM_NCPAINT)  
  47.             {  
  48.   
  49.                 IntPtr hdc = GetWindowDC(m.HWnd);  
  50.                 if ((int)hdc != 0)  
  51.                 {  
  52.                     Graphics g = Graphics.FromHdc(hdc);  
  53.                     Pen pen1 = new Pen(Color.FromArgb(64,64,64));  
  54.                     Pen pen2 = new Pen(Color.FromArgb(128, 128, 128));  
  55.                     Pen pen3 = new Pen(Color.FromArgb(212, 208, 200));  
  56.                     g.DrawLine(pen1, 573, 0, 573, 360);//最外边  
  57.                     g.DrawLine(pen2, 572, 1, 572, 359);//最外边第二条白色  
  58.                     g.DrawLine(pen3, 571, 2, 571, 359);  
  59.                     g.DrawLine(pen3, 571, 2, 571, 359);  
  60.                     g.Flush();  
  61.                     ReleaseDC(m.HWnd, hdc);  
  62.                 }  
  63.             }  
  64.         }  
  65.         private void Form5_MouseCaptureChanged(object sender, EventArgs e)  
  66.         {  
  67.             Graphics g = this.CreateGraphics();  
  68.             Pen pen1 = new Pen(Color.FromArgb(64, 64, 64));  
  69.             Pen pen2 = new Pen(Color.FromArgb(128, 128, 128));  
  70.             Pen pen3 = new Pen(Color.FromArgb(212, 208, 200));  
  71.             g.DrawLine(pen1, 573, 0, 573, 360);//最外边  
  72.             g.DrawLine(pen2, 572, 1, 572, 359);//最外边第二条白色  
  73.             g.DrawLine(pen3, 571, 2, 571, 359);  
  74.             g.DrawLine(pen3, 571, 2, 571, 359);  
  75.             g.Flush();  
  76.         }  
  77. }  
  78. }  

using System.Runtime.InteropServices;

        [DllImport("user32.dll")]
        internal static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);

        [DllImport("user32.dll")]
        internal static extern int GetMenuItemCount(IntPtr hMenu);

        [DllImport("user32.dll")]
        internal static extern int RemoveMenu(IntPtr hMenu, int uPosition, int uFlags);

        ///   <summary> 
        ///   窗体的关闭按钮失效 
        ///   </summary> 
        protected void CloseButtonEnable()
        {
            //   默认窗口去除关闭按钮 
            const int MF_BYPOSITION = 0x00000400;

            IntPtr hWindow = this.Handle;
            IntPtr hMenu = GetSystemMenu(hWindow, false);
            int count = GetMenuItemCount(hMenu);
            RemoveMenu(hMenu, count - 1, MF_BYPOSITION);
            RemoveMenu(hMenu, count - 2, MF_BYPOSITION);
        }

        private void confirm_Load(object sender, EventArgs e)
        {
            CloseButtonEnable();

        }

原文地址:https://www.cnblogs.com/chiyueqi/p/5497352.html