How to disable the window close button in an MFC Multiple Document Interface (MDI) application (201553)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ .NET (2002)
    • Microsoft Visual C++ .NET (2003)

This article was previously published under Q201553
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

SUMMARY

This article describes how to disable the close button in the title bar of the main frame and child frame windows of an MFC Multiple Document Interface (MDI) application.

MORE INFORMATION

In some circumstances you may want to prevent the user from clicking the close button in the window's title bar to close a frame window in an MFC application. The close button can be removed by removing the WS_SYSMENU style from the frame window. However, the minimize, maximize, and restore buttons are also removed and cannot be added. This is in accordance to the design of Windows.

To work around this limitation, you can simulate the functionality of a window without a close button by disabling the close button. In the WM_CREATE message handler for the MDI child frame window (CMDIChildWnd-derived class) disable the close button with the following code:
CMenu *pSysMenu = GetSystemMenu(FALSE);
ASSERT(pSysMenu != NULL);
VERIFY(pSysMenu->RemoveMenu(SC_CLOSE, MF_BYCOMMAND));
				
When the child frame window is not maximized, the above code prevents the user from closing the child frame window by clicking the close button. When the child frame window is maximized, the above code makes the close button appear disabled. However, a user can still close the child frame window by clicking this window's close button. You can prevent this by trapping the SC_CLOSE command when it is sent to the child frame window and preventing further processing of this command. To do so, use the WM_SYSCOMMAND message handler for the child frame's class, as in the following example:
// CChildFrame is a CMDIChildWnd-derived class.
void CChildFrame::OnSysCommand(UINT nID, LPARAM lParam)
{
    if(nID == SC_CLOSE)
        return;
    CMDIChildWnd::OnSysCommand(nID, lParam);
}
				
(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Isaac L. Varon, Microsoft Corporation.

Modification Type:MajorLast Reviewed:9/2/2005
Keywords:kbhowto kbMDI kbMenu KbUIDesign KB201553 kbAudDeveloper