Reputation: 41
How to gray out the close button in C++? Is it possible? I tried that;
#define WINVER 0x0500
#include<windows.h>
DeleteMenu(SystemMenu, SC_CLOSE, MF_BYCOMMAND);
But didn't worked.
Upvotes: 0
Views: 1788
Reputation: 6583
See here http://www.davekb.com/browse_programming_tips:win32_disable_close_button:txt for how to do it using pure Win32 or using MFCs. All the credit goes to them. I've just written the explanations.
This function will enable or disable (and gray out) the menu option (and, as a side effect, the close button) on Win32:
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;
}
It takes the HWND of the window and the desired state for the close button (TRUE enabled, FALSE disabled).
For MFC you can use this version, which accepts a CWnd instead of an HWND:
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;
}
Upvotes: 1