Microsoft KB Archive/129224

= How to remove the system menu from an iconized application in Visual C++ =

Article ID: 129224

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2003 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ 2005 Express Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q129224

<div class="notice_section">

Note Microsoft Visual C++ .NET (2002) supports 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.

Note Microsoft Visual C++ 2005 supports both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model.

<div class="summary_section">

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.

<div class="moreinformation_section">

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.

<ol> <li> 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 .     }                        </li> <li> 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 .     }                        </li> <li> 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) || nType == SIZE_MAXIMIZED) && (!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); }        }      } </li></ol>

Keywords: kbcode kbhowto kbmenu kbuidesign KB129224

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.