Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32 API C++ Menu bar

Tags:

c++

winapi

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.

like image 869
ss7 Avatar asked May 09 '15 03:05

ss7


People also ask

How to add menu in Win32 application?

You can insert or append menu items by using the InsertMenuItem function. You can also use the InsertMenu function to insert menu items and the AppendMenu function to append menu items.

Is Win32 API written in C?

The Windows API (Win32) is focused mainly on the programming language C in that its exposed functions and data structures are described in that language in recent versions of its documentation.

What is Win32 API used for?

The Win32 API (also called the Windows API) is the native platform for Windows apps. This API is best for desktop apps that require direct access to system features and hardware. The Windows API can be used in all desktop apps, and the same functions are generally supported on 32-bit and 64-bit Windows.


1 Answers

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
like image 66
Barmak Shemirani Avatar answered Oct 02 '22 06:10

Barmak Shemirani