Click here to Skip to main content
15,879,096 members
Articles / Desktop Programming / MFC
Article

Modifying the System Menu

Rate me:
Please Sign up or sign in to vote.
3.19/5 (64 votes)
26 Jan 2007CPOL4 min read 113.7K   1.4K   38   28
Add menu items to, and learn how to handle the system menu

Introduction

This is another in my "techniques I really use" series of short articles that don't discuss theory or design patterns, or any of that other hoity-toity stuff. This article is about simply getting something done. So without wasting any more of your time than I already have, let's get started.

Doing the Deed

The system menu is the menu that's displayed when you right-click on the application's icon in the title bar. The act of changing the system menu is fairly simple. Just add the following code to your application's InitInstance function:

HMENU pSysMenu = ::GetSystemMenu(m_pMainWnd->GetSafeHwnd(), FALSE);
if (pSysMenu)
{
    ::InsertMenu(pSysMenu, 0, MF_BYPOSITION | MF_STRING, ID_SHOW_MESSAGE1, <BR>                 "Show Message 1");
    ::InsertMenu(pSysMenu, 1, MF_BYPOSITION | MF_STRING, ID_SHOW_MESSAGE2, <BR>                 "Show Message 2");
    ::InsertMenu(pSysMenu, 2, MF_BYPOSITION | MF_SEPARATOR, 0, "");
}

After you retrieve the menu, simply add your new menu items to it. In the code above, we added our custom menu items to the top of the menu, and separated them from the original menu items with a menu separator. Easy stuff.

Next, we need to add our menu item IDs to the resource.h file. The easiest way to do this is to simply add a new menu to the application's resources, and create the menu items in the new menu. Well, it's kind of easy. When I did this, and then tried to add handlers via the Properties view for the CMainFrame message map, the ID's showed up as 32771 and 32772 instead of the names I gave them. I had to go back and re-display the menu resource and change the ID names in order for them to show up properly in the message map. Sometimes VS2005 just pisses me off. In any case, I then went back and added the handlers in CMainFrame.

The final step is to add a handler for the Windows WM_SYSCOMMAND message. Once you've added the handler, replace the contents of the function with the following code:

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
{
    bool bRecognized = false;
    // We have to pass all system commands to the base handler. In <BR>    // ClinicBoard1, we needed to override to use the following switch <BR>    // condtion:
    //
    //        switch (wParam & 0xFFF0)
    //
    // Starting with VS2005, they're passing the actual message ID instead of 
    // a WPARAM, so we don't have to do that anymore. jms - 25JAN2007
    switch (nID)
    {
        case SC_CLOSE:
        case SC_HOTKEY:
        case SC_HSCROLL:
        case SC_KEYMENU:
        case SC_MAXIMIZE:
        case SC_MINIMIZE:
        case SC_MOUSEMENU:
        case SC_MOVE:
        case SC_NEXTWINDOW:
        case SC_PREVWINDOW:
        case SC_RESTORE:
        case SC_SCREENSAVE:
        case SC_SIZE:
        case SC_TASKLIST:
        case SC_VSCROLL:
        case 61587: // show the system menu
        case 61443: // allow window resizing by click/dragging the main window <BR>                    // borders
/*
        // no longer supported in VS2005/MFC8?
        case SC_ARRANGE:
    #if(WINVER >= 0x0400)
        case SC_DEFAULT:
        case SC_MONITORPOWER:
        case SC_CONTEXTHELP:
        case SC_SEPARATOR:
    #endif
*/
            CFrameWnd::OnSysCommand(nID, lParam);
            return;
    }

    switch (nID)
    {
        case ID_SHOW_MESSAGE1 :
        case ID_SHOW_MESSAGE2 :
            PostMessage(WM_COMMAND, nID, 0);
            break;
    }

}

First, we need to allow the function to process system commands. If none were processed we'll stillbe in the function (after handling a system command, the first switch statement returns to the calling function) and we can process our own commands. Since we have handlers for them in our CMainFrame class, we'll just post the message to our CMainFrame window.

Note About OnSysCommand

In VS2005 (or maybe even VS2003), the OnSysCommand() function was modified as follows:

  • In prior versions of the function, the first parameter was a WPARAM, with the lower word containing information used by Windows, and the upper word being the actual ID of the selected command, and it was up to the programmer to strip the lower word in order to determine the command being sent. In VS2005, MFC strips the lower word for you, thus leading to the next change.
  • The first parameter passed to the function was changed from a WPARAM to a UINT. This made things on our side of the function just a wee bit simpler because in the case of the function above, we no longer had to do this - switch(wParam&0xFFF0) - in order to get to the message ID.
  • The list of handled messages appears to have shrunk. As you can see in the code above, I commented out several message IDs not specifically listed in MSDN.
  • It seems that MSDN does not provide a complete list of system messages that this command can process. While testing the sample app, I discovered the following issues:

    • Left-clicking the application icon will not present the system menu to the user. Right-clicking will, and the menu items will work.

    • The user will be unable to click in the main menu of your application.

    • The user will not be able to resize the window by click/dragging the borders of the main window.

    • The buttons that minimize/maximize/close the window (on the right side of the title bar) appear to work normally.

    To fix all of these issues, you have to add the following case items to the code above:

    case 61587: // show the system menu
    case 61443: // allow window resizing by click/dragging the main window <BR>            // borders

    I did a search in the MFC/SDK source code, and these two message ID's are NOT defined anywhere. C'mon Microsoft - you're sure getting sloppy in your old age.

Disclaimers and Other Notes

As usual, I'm asking that you vote the article and not my politics. Isn't it a dirty rotten shame that I have to add this to every one of my articles? No, it's not because of my politics, but because some people here don't know when to leave their politics out of their voting considerations.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
QuestionJust... Wow Pin
#realJSOP18-Jul-16 4:21
mve#realJSOP18-Jul-16 4:21 
QuestionOverriding Winform's System menu for a user control C# Sample Pin
ekhanlari17-Feb-13 12:40
ekhanlari17-Feb-13 12:40 
Dear John Simmons
Hello
Your article and C++ sample project is very good.
I want a C# Sample project for overriding Winform's System menu for a user control

Tanks
GeneralMy vote of 5 Pin
Nigam Patel2-Jan-12 17:56
Nigam Patel2-Jan-12 17:56 
GeneralLearned something new Pin
Hans Dietrich26-Jan-07 7:00
mentorHans Dietrich26-Jan-07 7:00 
GeneralNo, THIS is the thanks you get Pin
Wes Aday26-Jan-07 4:17
professionalWes Aday26-Jan-07 4:17 
GeneralSo This is The Thanks I Get... Pin
#realJSOP26-Jan-07 4:14
mve#realJSOP26-Jan-07 4:14 
GeneralRe: So This is The Thanks I Get... Pin
Rama Krishna Vavilala26-Jan-07 4:21
Rama Krishna Vavilala26-Jan-07 4:21 
GeneralRe: So This is The Thanks I Get... Pin
Thomas Stockwell26-Jan-07 4:32
professionalThomas Stockwell26-Jan-07 4:32 
GeneralRe: So This is The Thanks I Get... Pin
#realJSOP26-Jan-07 4:35
mve#realJSOP26-Jan-07 4:35 
GeneralRe: So This is The Thanks I Get... Pin
Hans Dietrich26-Jan-07 7:06
mentorHans Dietrich26-Jan-07 7:06 
GeneralNot a proper way to handle the system messages Pin
Rama Krishna Vavilala26-Jan-07 4:13
Rama Krishna Vavilala26-Jan-07 4:13 
GeneralRe: Not a proper way to handle the system messages Pin
#realJSOP26-Jan-07 4:17
mve#realJSOP26-Jan-07 4:17 
GeneralRe: Not a proper way to handle the system messages Pin
Rama Krishna Vavilala26-Jan-07 4:19
Rama Krishna Vavilala26-Jan-07 4:19 
GeneralRe: Not a proper way to handle the system messages Pin
#realJSOP26-Jan-07 4:31
mve#realJSOP26-Jan-07 4:31 
GeneralRe: Not a proper way to handle the system messages Pin
Nish Nishant26-Jan-07 4:40
sitebuilderNish Nishant26-Jan-07 4:40 
GeneralRe: Not a proper way to handle the system messages Pin
#realJSOP26-Jan-07 4:42
mve#realJSOP26-Jan-07 4:42 
GeneralRe: Not a proper way to handle the system messages Pin
Jason Henderson26-Jan-07 5:26
Jason Henderson26-Jan-07 5:26 
GeneralRe: Not a proper way to handle the system messages Pin
Member 85635788-Apr-12 21:18
Member 85635788-Apr-12 21:18 
GeneralRe: Not a proper way to handle the system messages Pin
Paul Conrad24-Oct-08 7:24
professionalPaul Conrad24-Oct-08 7:24 
GeneralRe: Not a proper way to handle the system messages Pin
#realJSOP24-Oct-08 8:48
mve#realJSOP24-Oct-08 8:48 
GeneralRe: Not a proper way to handle the system messages Pin
Paul Conrad24-Oct-08 10:57
professionalPaul Conrad24-Oct-08 10:57 
GeneralRe: Not a proper way to handle the system messages Pin
Rama Krishna Vavilala26-Jan-07 4:22
Rama Krishna Vavilala26-Jan-07 4:22 
GeneralRe: Not a proper way to handle the system messages Pin
Mark Salsbery26-Jan-07 8:08
Mark Salsbery26-Jan-07 8:08 
GeneralRe: Not a proper way to handle the system messages Pin
Nish Nishant26-Jan-07 4:20
sitebuilderNish Nishant26-Jan-07 4:20 
GeneralRe: Not a proper way to handle the system messages Pin
#realJSOP26-Jan-07 4:33
mve#realJSOP26-Jan-07 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.