HOWTO: Remove the System Menu from an Iconized Application

Last reviewed: July 2, 1997
Article ID: Q129224
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52
        - Microsoft Visual C++ 32-bit Edition, 2.0, 2.1, 4.0, 5.0
    

SUMMARY

There are two ways to remove the system menu of an application when a user clicks an iconized application:

  1. Override the main frame window's PreCreateWindow function to remove the WS_SYSMENU from the window's style field. However, this method removes the system menu altogether from the application.

  2. Override the main frame window's OnSize method, and change the window style of the mainframe to either include or exclude the WS_SYSMENU style bit depending on user action. Use this technique if the user wants to remove the system menu when the application is iconized and add the system menu back to the application when the application is restored.

MORE INFORMATION

Method One

To remove the system menu completely from the application, override the main frame window's PreCreateWindow method as follows:

   BOOL CMainFrame::PreCreateWindow(CREATESTRUCT & cs)
   {
           // Call the base class version of PreCreateWindow, replace
          //  CMDIFrameWnd with CFrameWnd in the following line
          //  for an SDI application

           if (!CMDIFrameWnd::PreCreateWindow(cs))
                return FALSE;

          // Remove the system menu style bit from the window

           cs.style &= ~WS_SYSMENU;
           return TRUE;
   }

Method Two

The following steps and code fragments show how to remove the system menu when a user clicks an iconized application. The system menu is restored when the application is not iconized.

  1. Declare a BOOLEAN public data member in the class declaration of CMainFrame. This data member determines if the system menu is enabled or not:

          // In an SDI application CMainFrame will be derived from CFrameWnd
    

          class CMainFrame : public CMDIFrameWnd
          {
          public:
    
             BOOL sys_menu_enabled;
                 .
                 .  // Existing class declarations
                 .
          }
    
    

  2. Modify the CMainFrame constructor to initialize the sys_menu_enabled data member function to TRUE:

          CMainFrame::CMainFrame()
          {
    
            //default the system menu to be enabled
            sys_menu_enabled = TRUE;
                 .
                 .  // Continue with normal constructor code, if any
                 .
          }
    
    

  3. Use the Class Wizard to create a message handler for the WM_SIZE message for the CMainFrame class, and add the following code to the CMainFrame::OnSize message handler:

          void CMainFrame::OnSize(UINT nType, int cx, int cy)
          {
    
            // declare a local variable to hold the window style
    
               long window_style;
    
            //call base class's OnSize function,
            //If SDI application call CFrameWnd::OnSize()
    
            CMDIFrameWnd::OnSize(nType, cx, cy);
    
            //if user is minimizing or iconizing the application
    
            if (nType == SIZE_MINIMIZED)
            {
                 // Get the main frame window's style
                  window_style = GetWindowLong(m_hWnd, GWL_STYLE);
    
                 //Remove the system menu from the window's style
                  window_style &= ~WS_SYSMENU;
    
                 //toggle the boolean data member to show sys menu disabled
                  sys_menu_enabled = FALSE;
    
                 //set the style attribute of the main frame window
                 SetWindowLong(m_hWnd, GWL_STYLE, window_style);
            }
            else
            {
                 //if user is restoring the application and his system menu
                 //is disabled,
                 if ((nType == SIZE_RESTORED) && (!sys_menu_enabled))
                 {
                     window_style = GetWindowLong(m_hWnd, GWL_STYLE);
    
                   //Add the system menu to the window's style
                     window_style |= WS_SYSMENU;
    
                  //toggle the boolean data member to show sys menu enabled
                    sys_menu_enabled = TRUE;
    
                    SetWindowLong(m_hWnd, GWL_STYLE, window_style);
                    SendMessage(WM_NCACTIVATE,TRUE);
                 }
             }
          }
    
     
    
    	


Keywords : kbprg MfcUI kbcode kbprg
Technology : kbMfc
Version : 1.0 1.5 2.0 2.1 4.0 5.0
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.