Sometimes you will see some forms and their close buttons on the title bar are disabled. If the program is in a critical process and you do not want the user to interrupt it at will, you can consider disabling the close button .. Net Framework has no built-in support for this, but with the help of Win32 API, it can be easily done. The Win32 API we want to call allows us to access the system menu. In this way, you can operate the menu items, such as close, move, and size. Next let's take a look at the specific practices.
1. Create a Windows application, create a form, and enter the code view.
2. Add necessary namespaces:
Using system. runtime. interopservices;
3. Add necessary constants and reference of API functions: Private const int SC _close = 0xf060;
Private const int mf_enabled = 0x00000000;
Private const int mf_grayed = 0x00000001;
Private const int mf_disabled = 0x00000002;
[Dllimport ("user32.dll", entrypoint = "getsystemmenu")]
Private Static extern intptr getsystemmenu (intptr hwnd, int brevert );
[Dllimport ("user32.dll")]
Public static extern bool enablemenuitem (intptr hmenu, int uidenableitem, int uenable );
4. Add code to the load event processing function of the form: Intptr hmenu = getsystemmenu (this. Handle, 0 );
Enablemenuitem (hmenu, SC _close, mf_disabled | mf_grayed );
Below is:
What should I do if the key process ends and the button needs to be available?
Use the following code:
Intptr hmenu = getsystemmenu (this. Handle, 0 );
Enablemenuitem (hmenu, SC _close, mf_enabled );
In.