Dave's Brain

Browse - programming tips - win32 disable close button

Date: 2008jul3
Platform: win32
Language: C/C++

Q.  How do I disable a Window's close button?
The [X] thing in the top-right corner.

A.  Here's a little function that does the trick:

// Disable or enable the [X] button in top-right corner
BOOL EnableCloseButton(const HWND hwnd, const BOOL bState)
{
	HMENU	hMenu;
	UINT	dwExtra;

	if (hwnd == NULL) return FALSE;
	if ((hMenu = GetSystemMenu(hwnd, FALSE)) == NULL) return FALSE;
	dwExtra = bState ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
	return EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | dwExtra) != -1;
}

// The MFC version...
#ifdef _MFC_VER
// Disable or enable the [X] button in top-right corner
BOOL EnableCloseButton(const CWnd *wnd, const BOOL bState)
{
	CMenu *menu;

	if (wnd == NULL) return FALSE;
	if ((menu = wnd->GetSystemMenu(FALSE)) == NULL) return FALSE;
	return menu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | bState ? MF_ENABLED : MF_GRAYED) != -1;
}
#endif

// Example use:

// To disable:
	EnableCloseButton(hwnd, FALSE);

// To disable in MFC:
	EnableCloseButton(AfxGetMainWnd(), FALSE);
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: