8
votes

I'm trying to learn some basic win32 api. I see to add items to the menu bar tutorials have mentioned to use something like:

hMenubar = CreateMenu();
hMenu = CreateMenu();

AppendMenuW(hMenu, MF_STRING, IDM_FILE_NEW, L"&New");
AppendMenuW(hMenu, MF_STRING, IDM_FILE_OPEN, L"&Open");
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenuW(hMenu, MF_STRING, IDM_FILE_QUIT, L"&Quit");
AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR)hMenu, L"&File");

But in the default project for C++ Desktop in VS2013 has a File and Help menubar and inside they have Exit and About. But all they do is have a switch like this in WndProc:

switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;

I have a few questions. How is it they add the file and help menu bars, as well as the about and exit items in the menubars without using the createmenu() and such? WHat does IDM_ABOUT and IDM_EXIT mean? They have those in the menus, but does just putting them in the switch statement add them to the menubars? What is wmId and wmEvent and why is the switch on wmId? What is actually adding those items to the menu bar?

If you need to see more code of my program it's just a non blank C++ Win32 Project in VS2013

Thanks for any answers provided, and it would be appreicated if someone could point me in the direction of a good current as possible win32 api C++ tutorial.

1
and it would be appreicated if someone could point me in the direction of a good current as possible win32 api C++ tutorial Any book by Charles Petzold.PaulMcKenzie
Thanks a ton I will check it outshenk

1 Answers

7
votes

There are two ways to make menus. It can be done programmatically as you have shown, or it can be done with resource editor. In solution explorer, double-click the file with *.rc extension. You should see a window called "Resource view". Open the resource nodes until you see "Menu". You can add/modify/delete menu items. You can also make dialog boxes.

Each menu item or dialog box button has an identification number, this number is passed through WPARAM wParam. In the above example wmEvent is not used, don't worry about it for now.

IDM_ABOUT is a number defined in resource file #define IDM_ABOUT 101 (or it might be a different number). When menu item is clicked, then a message is sent with that number. You can catch the message and respond to it.

This is basic WinApi, it hasn't changed much in the last 10 or 20 years, any tutorial you find on Google is up to date.

Also, when you make a new menu item with resource editor, Visual Studio automatically creates an ID and assigns it to that menu item. You need to know what those ID's are. You can find out by hitting F4 key which brings up "Properties Window".

For example, if you type in a new menu item "&File New", the ID for that menu will be something like ID_FILE_NEWFILE which should be visible in "Properties Window".

This menu item will be just like IDM_ABOUT. It is sent to Windows Procedure function associated with that window.

For menu item messages:

  • message is always set to WM_COMMAND
  • wParam is the menu ID
  • lParam is not used