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

后端 未结 2 638
后悔当初
后悔当初 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

    0 讨论(0)
  • 2021-01-17 03:19
    private const int dis_close_button = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams ObjCP = base.CreateParams;
                ObjCP.ClassStyle = ObjCP.ClassStyle | dis_close_button;
                return ObjCP;
            }
        }
    

    Try, this...

    0 讨论(0)
提交回复
热议问题