Disable the form button in Windows Forms

Source: Internet
Author: User
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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.