Click here to Skip to main content
12,829,115 members (43,021 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 15 Oct 2011

C# Close Button Disable Example

, 17 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple application that shows how to change the status of the close (X) button in Windows Forms.

Close button example


The main purpose of this application is to enable and disable the "Close" button of any Windows Form on your application by using Windows APIs.

In some of my applications, I had to prevent users from clicking the close button to exit an application. Of course, there are various ways to exit an application. But in some cases, clicking the close button may not be the nicest option.

The application uses user32.dll Windows API to invoke some methods. Of course, there are other methods that can be invoked in the same library in order to perform different actions.

Using the Code

There are plenty of ways to enable and disable the close button in Windows Forms. This one is a simple and probably the most effective of all.

To manipulate the state of the close button in your form, as mentioned before, a Windows API named user32.dll is needed. By including user32.dll in your project, you will be able to invoke the methods in the "user32.dll" library. There are two main methods we need to use to change the state of the close button, which are: EnableMenuItem() and GetSystemMenu().

As you can see, these methods are located in the user32.dll file.

After adding using System.Runtime.InteropServices; to your using section, you can start importing functions to your project.

After this step, it's time to create the methods and invoke them in your project. Now, you can invoke those methods in any way you want, like using it in a timer, or any kind of other events. In the demo application, you'll notice that the button events call the custom created void functions which contain other methods for enabling and disabling the close button. The snippet below shows invoking the DLL methods.

In order to change the status of the close button, we need to tell the API the current and target addresses of the button's states. These three values represent the states of the button. All of the values can be found on the internet, if you are interested in manipulating other controls.

internal const int SC_CLOSE = 0xF060;           //close button's code in Windows API
internal const int MF_ENABLED = 0x00000000;     //enabled button status
internal const int MF_GRAYED = 0x1;             //disabled button status (enabled = false)
internal const int MF_DISABLED = 0x00000002;    //disabled button status

Now it's time to call our DLL's methods:

private static extern IntPtr GetSystemMenu(IntPtr HWNDValue, bool isRevert);

private static extern int EnableMenuItem(IntPtr tMenu, int targetItem, int targetStatus);

Here are two others that I didn't have a chance to try, but I don't see any reason for them to not work properly...

internal const int SC_MINIMIZE = 0xF020; //for minimize button on forms
internal const int SC_MAXIMIZE = 0xF030; //for maximize button on forms


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Selim Sertaç BALCI
Software Developer (Senior) Intertech Bilgi İşlem AŞ
Turkey Turkey
Software Engineer, has been working on multiple platforms such as Apple, PHP and .NET Framework.

You may also be interested in...

Comments and Discussions

QuestionHe Pin
Member 1034608220-Nov-14 5:08
memberMember 1034608220-Nov-14 5:08 
Generalquery Pin
KD Palekar8-Feb-14 13:27
memberKD Palekar8-Feb-14 13:27 
GeneralMy vote of 5 Pin
kaskavalci18-Sep-13 4:11
memberkaskavalci18-Sep-13 4:11 
GeneralMy vote of 5 Pin
Britteandy12-Jul-13 2:42
memberBritteandy12-Jul-13 2:42 
QuestionWrite this under C++/WinForm Pin
christmars11-Oct-12 23:47
memberchristmars11-Oct-12 23:47 
NewsRe: Write this under C++/WinForm Pin
Selim Sertaç BALCI27-Nov-12 4:39
memberSelim Sertaç BALCI27-Nov-12 4:39 
GeneralRe: Write this under C++/WinForm Pin
christmars27-Nov-12 5:29
memberchristmars27-Nov-12 5:29 
QuestionInteresting code but... Pin
s.kleinschmidt26-Oct-11 0:33
members.kleinschmidt26-Oct-11 0:33 
AnswerRe: Interesting code but... Pin
Selim Sertaç BALCI28-Oct-11 22:35
memberSelim Sertaç BALCI28-Oct-11 22:35 
GeneralNice Pin
kiran dangar17-Oct-11 3:37
memberkiran dangar17-Oct-11 3:37 
QuestionThanks Pin
Barış Bp16-Oct-11 10:39
memberBarış Bp16-Oct-11 10:39 
AnswerRe: Thanks Pin
Selim Sertaç BALCI16-Oct-11 10:40
memberSelim Sertaç BALCI16-Oct-11 10:40 
QuestionIt Seems this doesn't work on Mininum and Maximum button? Pin
Eason_Wu16-Oct-11 4:45
memberEason_Wu16-Oct-11 4:45 
AnswerRe: It Seems this doesn't work on Mininum and Maximum button? Pin
Selim Sertaç BALCI16-Oct-11 10:04
memberSelim Sertaç BALCI16-Oct-11 10:04 
GeneralRe: It Seems this doesn't work on Mininum and Maximum button? Pin
Eason_Wu17-Oct-11 6:31
memberEason_Wu17-Oct-11 6:31 
AnswerThanks! Pin
trofimchyk16-Oct-11 4:04
membertrofimchyk16-Oct-11 4:04 
GeneralRe: Thanks! Pin
Selim Sertaç BALCI16-Oct-11 10:05
memberSelim Sertaç BALCI16-Oct-11 10:05 
QuestionNot an article Pin
PIEBALDconsult15-Oct-11 19:45
memberPIEBALDconsult15-Oct-11 19:45 
AnswerRe: Not an article Pin
Selim Sertaç BALCI16-Oct-11 10:06
memberSelim Sertaç BALCI16-Oct-11 10:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 17 Oct 2011
Article Copyright 2011 by Selim Sertaç BALCI
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid