在WinForm中,如何屏蔽关闭按钮?同时需要保留标题栏、最小化按钮、最大化按钮。不可以在Closing进行拦截,因为这样的话会导致计算机无法重新启动。

解决方案 »

  1.   

    不如自己做个带  标题栏、最小化按钮、最大化按钮的panel,替换系统默认的
      

  2.   

    #region 攔截Windows消息 
    [DllImport("user32.dll",EntryPoint="GetSystemMenu")] 
    extern static System.IntPtr GetSystemMenu(System.IntPtr hWnd , System.IntPtr bRevert); 
    static int MF_BYPOSITION = 0x400; 
    static int MF_REMOVE = 0x1000; [DllImport("user32.dll",EntryPoint="RemoveMenu")] 
    extern static int RemoveMenu (IntPtr hMenu, int nPos, int flags); 
    private const int WS_SYSMENU = 0x00080000;  protected override CreateParams CreateParams 

    get 

    CreateParams cp =  base.CreateParams; 
    cp.Style = cp.Style & ~WS_SYSMENU; 
    return cp; 

    }
    #endregion
    這樣就OK了
      

  3.   

    加上這段屏蔽關閉信息
    #region 攔截Windows消息
    protected override void WndProc(ref Message m)
    {
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_CLOSE = 0xF060;
    if (m.Msg == WM_SYSCOMMAND && (int) m.WParam == SC_CLOSE)
    {//捕捉關閉窗體消息
    // User clicked close button
    this.WindowState = FormWindowState.Minimized;
    return;
    }
    base.WndProc(ref m);
    }
    #endregion
      

  4.   

    [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);
            }
      

  5.   

    dragonfly001(我思考,我生存!)   的方法是把整个的标题栏按钮都去掉了,
    yf1025(小桥,流水,人家) 的方法可行