EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

System menu/Control menu

Every top level window contains a small menu which can be seen by right clicking on title bar or clicking on the icon visible at the left side on any application. This is called system menu or control menu. It contains menu items like maximize minimize close, move, size, restore etc. These menu items are to control system operations and these are provided by operating system. However we can modify these or can append some additional menus if needed.

GetSystemMenu

GetSystemMenu() gives access to the application to access this system menu. It returns handle to system menu which can be used to add additional menus. This can also be used to revert to original state.

Syntax

HMENU WINAPI GetSystemMenu(
    HWND hWnd,
    BOOL bRevert
);

Parameters

  • hWnd (HWND) A handle to the window that will own a copy of the window menu.
  • bRevert (BOOL) The action to be taken. If this parameter is FALSE, GetSystemMenu returns a handle to the copy of the window menu currently in use. The copy is initially identical to the window menu, but it can be modified. If this parameter is TRUE, GetSystemMenu resets the window menu back to the default state. The previous window menu, if any, is destroyed.

Return value

If the bRevert parameter is FALSE, the return value is a handle to a copy of the window menu (HMENU). If the bRevert parameter is TRUE, the return value is NULL.

AppendMenu

Appends a new item to the end of the specified menu bar, drop-down menu, submenu, or shortcut menu. You can use this function to specify the content, appearance, and behavior of the menu item.

Syntax

BOOL WINAPI AppendMenu(
        HMENU hMenu,
        UINT uFlags,
        UINT_PTR uIDNewItem,
        LPCTSTR lpNewItem
);

Parameters

  • hMenu (HMENU) A handle to the menu bar, drop-down menu, submenu, or shortcut menu to be changed.
  • uFlags (UINT) Controls the appearance and behavior of the new menu item. This parameter can be a combination of the following values. Value Meaning MF_BITMAP, MF_CHECKED, MF_DISABLED, MF_ENABLED, MF_GRAYED, MF_MENUBARBREAK, MF_MENUBREAK, MF_OWNERDRAW, MF_POPUP, MF_SEPARATOR, MF_STRING,MF_UNCHECKED
  • uIDNewItem (UINT_PTR) The identifier of the new menu item or, if the uFlags parameter is set to MF_POPUP, a handle to the drop-down menu or submenu.
  • lpNewItem (LPCTSTR) The content of the new menu item. The interpretation of lpNewItem depends on whether the uFlags parameter includes the following values. MF_BITMAP, MF_OWNERDRAW, MF_STRING

Return value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero/FALSE. To get extended error information, call GetLastError.

Source Code

Here is a source code to demonstrate GetSystemMenu() function. It creates an additional entry in system menu. It also installs menu handler for this. It prompts a message box to the user and user can further revert this system menu calling GetSystemMenu() with argument as TRUE.

GetSystemMenu Demo Application screen shot

#include <windows.h>
#define IDM_MYSYSTEM 1001
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT message, 
                         WPARAM wParam,
                         LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
10                       LPSTR     lpCmdLine,
11                       int       nCmdShow)
12  {
13    WNDCLASS wc;
14    MSG msg;
15    HWND hWnd;
16    HMENU hMenu;
17 
18    ZeroMemory(&wc, sizeof(WNDCLASS));
19 
20    wc.style           = CS_HREDRAW | CS_VREDRAW;
21    wc.lpfnWndProc     = (WNDPROC)WndProc;
22    wc.hInstance       = hInstance;
23    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
24    wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
25    wc.lpszClassName   = (LPCTSTR)"MyWin32Class";
26 
27 
28    RegisterClass(&wc);
29 
30     hWnd = CreateWindow("MyWin32Class", "GetSystemMenu Demo", WS_OVERLAPPEDWINDOW,
31        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
32 
33    ShowWindow(hWnd, nCmdShow);
34    UpdateWindow(hWnd);
35    /* Get Menu Handle to top window */
36    hMenu = GetSystemMenu(hWnd, FALSE);
37    if(hMenu != INVALID_HANDLE_VALUE) {
38      /* Append one separator menu item */
39      AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
40      /* Append string type menu */
41      AppendMenu(hMenu, MF_STRING, IDM_MYSYSTEM, "My System Menu");
42    }
43    while (GetMessage(&msg, NULL, 0, 0)) 
44    {
45      DispatchMessage(&msg);
46    }
47    return 0;
48  }
49 
50  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
51  {
52    int nSel;
53    switch (message) 
54    {
55      /* System command handler */
56      case WM_SYSCOMMAND:
57        /* Handling our menu item */
58        if(wParam == IDM_MYSYSTEM) {
59          nSel = MessageBox (hWnd, "My System Menu. Want to disable?", "System Menu", MB_YESNO);
60          if (nSel == IDYES){
61            /* If yes we revert to original menu */
62            GetSystemMenu(hWnd, TRUE);
63          }
64        }
65        DefWindowProc(hWnd, message, wParam, lParam);
66        break;
67      case WM_DESTROY:
68        PostQuitMessage(0);
69        break;
70      default:
71        return DefWindowProc(hWnd, message, wParam, lParam);
72     }
73     return 0;
74  }

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 22:48:06(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.