How to make the Close button disabled in a windows Form using C# coding

后端 未结 2 650
后悔当初
后悔当初 2021-01-17 02:44

every one. I just want to make the Close button disabled on a button click event using C#.net. I am trying for this but not correctly sure it\'s correct.

  p         


        
2条回答
  •  情歌与酒
    2021-01-17 03:16

    Add the following library

    using System.Runtime.InteropServices;
    

    Declare the following as class level variable

    const int MF_BYPOSITION = 0x400;
    
    [DllImport("User32")]
    private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
    
    [DllImport("User32")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    
    [DllImport("User32")]
    private static extern int GetMenuItemCount(IntPtr hWnd);
    

    In the Form_Load() event, write the following code

    private void Form1_Load(object sender, EventArgs e)
    {
            IntPtr hMenu = GetSystemMenu(this.Handle, false);
            int menuItemCount = GetMenuItemCount(hMenu);
            RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
    

    }

    Disable the Close 'X' button

    Just modify it for your ClickEvent

提交回复
热议问题