- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Windows API menus
In this part of the Windows API tutorial, we create menus. A menu is a group of commands located in a menubar. A menubar contains a list of menus. Menus can contain either menu items or other menus calls submenus. A menu item that carries out a command is called a command item or a command. On Windows, menubar is sometimes called a toplevel menu; menus and submenus are called popup menus. Menu items are usually grouped into some logical groups. These groups are divided by a separator. The separator is a small horizontal line.
A simple menu
In the following example, we create a menubar and three menu commands. We also create a separator.
simplemenu.c
#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void AddMenus(HWND); #define IDM_FILE_NEW 1 #define IDM_FILE_OPEN 2 #define IDM_FILE_QUIT 3 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Simple menu"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Simple menu", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 350, 250, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: AddMenus(hwnd); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_FILE_NEW: case IDM_FILE_OPEN: MessageBeep(MB_ICONINFORMATION); break; case IDM_FILE_QUIT: SendMessage(hwnd, WM_CLOSE, 0, 0); break; } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } void AddMenus(HWND hwnd) { HMENU hMenubar; HMENU hMenu; 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"); SetMenu(hwnd, hMenubar); }
Two menu items make a short sound. The third terminates the application.
case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_FILE_NEW: case IDM_FILE_OPEN: MessageBeep(MB_ICONINFORMATION); break; case IDM_FILE_QUIT: SendMessage(hwnd, WM_CLOSE, 0, 0); break; } break;
If we select a menu item, the window procedure receives the WM_COMMAND
message. The menu item id is in the low order word of the wParam
value.
hMenubar = CreateMenu(); hMenu = CreateMenu();
Menubar and menus are created using the CreateMenu()
function.
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");
Menu items and submenus are created using the AppendMenuW()
function. What are we going to append, depends on the flag. The MF_STRING
appends a label, the MF_SEPARATOR
appends a separator and the MF_POPUP
appends a menu.
SetMenu(hwnd, hMenubar);
Finally, we set the menubar calling the SetMenu()
function.

A popup menu
A popup menu is also called context menu. It is a list of commands that appears under some context. For example, in a Firefox web browser when we right click on a web page, we get a context menu. Here we can reload a page, go back, or view a page source. If we right click on a toolbar, we get another context menu for managing toolbars.
popupmenu.c
#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); #define IDM_FILE_NEW 1 #define IDM_FILE_OPEN 2 #define IDM_FILE_QUIT 3 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Popup menu"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Popup menu", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 350, 250, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HMENU hMenu; POINT point; switch(msg) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_FILE_NEW: case IDM_FILE_OPEN: MessageBeep(MB_ICONINFORMATION); break; case IDM_FILE_QUIT: SendMessage(hwnd, WM_CLOSE, 0, 0); break; } break; case WM_RBUTTONUP: point.x = LOWORD(lParam); point.y = HIWORD(lParam); hMenu = CreatePopupMenu(); ClientToScreen(hwnd, &point); 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"); TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hwnd, NULL); DestroyMenu(hMenu); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); }
We have an example with a context menu that has three menu items.
case WM_RBUTTONUP: point.x = LOWORD(lParam); point.y = HIWORD(lParam); ...
The WM_RBUTTONUP
message is posted when the user releases the right mouse button while the cursor is in the client area of a window. The low-order word of the lParam
specifies the x coordinate of the cursor. The high-order word specifies the y coordinate of the cursor. The coordinates are relative to the upper-left corner of the client area.
hMenu = CreatePopupMenu();
The CreatePopupMenu()
function creates a popup menu. It returns a handle to the newly created menu. The menu is initially empty.
ClientToScreen(hwnd, &point);
The ClientToScreen()
function converts the client coordinates of a specified point to screen coordinates. We need these coordinates to display the context menu.
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");
Three menu items and one separator are created.
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hwnd, NULL);
The TrackPopupMenu()
function displays a context menu at the specified location and tracks the selection of items on the menu.
DestroyMenu(hMenu);
In the end, the menu object is destroyed with the DestroyMenu()
function. A menu that was not assigned to a window must be explicitly destroyed.

A check menu item
A check menu item is a menu item that has a check mark before its label. A menu item can be checked or unchecked using the CheckMenuItem()
function.
checkmenuitem.c
#include <windows.h> #include <commctrl.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void AddMenus(HWND); #define IDM_VIEW_STB 1 HWND ghSb; HMENU ghMenu; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Check menu item"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Check menu item", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 350, 250, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { UINT state; switch(msg) { case WM_CREATE: AddMenus(hwnd); InitCommonControls(); ghSb = CreateWindowExW(0, STATUSCLASSNAMEW, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU) 1, GetModuleHandle(NULL), NULL); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_VIEW_STB: state = GetMenuState(ghMenu, IDM_VIEW_STB, MF_BYCOMMAND); if (state == MF_CHECKED) { ShowWindow(ghSb, SW_HIDE); CheckMenuItem(ghMenu, IDM_VIEW_STB, MF_UNCHECKED); } else { ShowWindow(ghSb, SW_SHOWNA); CheckMenuItem(ghMenu, IDM_VIEW_STB, MF_CHECKED); } break; } break; case WM_SIZE: SendMessage(ghSb, WM_SIZE, wParam, lParam); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } void AddMenus(HWND hwnd) { HMENU hMenubar; hMenubar = CreateMenu(); ghMenu = CreateMenu(); AppendMenuW(ghMenu, MF_STRING, IDM_VIEW_STB, L"&Statusbar"); CheckMenuItem(ghMenu, IDM_VIEW_STB, MF_CHECKED); AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR) ghMenu, L"&View"); SetMenu(hwnd, hMenubar); }
In the example we have a View menu with one menu item. This menu item will show or hide a statusbar. When the statusbar is visible, the menu item is checked.
#define IDM_VIEW_STB 1
This is an id for the menu item that will show or hide the statusbar.
InitCommonControls();
A statusbar is a common control. It must be initialized with the InitCommonControls()
function.
ghSb = CreateWindowExW(0, STATUSCLASSNAMEW, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU) 1, GetModuleHandle(NULL), NULL);
This code line creates a statusbar control.
state = GetMenuState(ghMenu, IDM_VIEW_STB, MF_BYCOMMAND);
We get the state of the statusbar menu item with the GetMenuState()
function.
if (state == MF_CHECKED) { ShowWindow(ghSb, SW_HIDE); CheckMenuItem(ghMenu, IDM_VIEW_STB, MF_UNCHECKED); } else { ShowWindow(ghSb, SW_SHOWNA); CheckMenuItem(ghMenu, IDM_VIEW_STB, MF_CHECKED); }
Depending on its state, we show or hide the statusbar control using the ShowWindow()
function. The menu item is checked or unchecked accordingly with the CheckMenuItem()
function.
case WM_SIZE: SendMessage(ghSb, WM_SIZE, wParam, lParam); break;
We resize the statusbar to fit the window after the window is being resized.

A radio menu item
Radio menu items enable to select from a mutually exclusive list of options. Radio menu items are managed with the CheckMenuRadioItem()
function.
radiomenuitem.c
#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void AddMenus(HWND); #define IDM_MODE_MAP 1 #define IDM_MODE_SAT 2 #define IDM_MODE_TRA 3 #define IDM_MODE_STR 4 HMENU hMenu; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Radio menu item"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Radio menu item", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 350, 250, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: AddMenus(hwnd); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_MODE_MAP: CheckMenuRadioItem(hMenu, IDM_MODE_MAP, IDM_MODE_STR, IDM_MODE_MAP, MF_BYCOMMAND); MessageBeep(MB_ICONERROR); break; case IDM_MODE_SAT: CheckMenuRadioItem(hMenu, IDM_MODE_MAP, IDM_MODE_STR, IDM_MODE_SAT, MF_BYCOMMAND); MessageBeep(0xFFFFFFFF); break; case IDM_MODE_TRA: CheckMenuRadioItem(hMenu, IDM_MODE_MAP, IDM_MODE_STR, IDM_MODE_TRA, MF_BYCOMMAND); MessageBeep(MB_ICONWARNING); break; case IDM_MODE_STR: CheckMenuRadioItem(hMenu, IDM_MODE_MAP, IDM_MODE_STR, IDM_MODE_STR, MF_BYCOMMAND); MessageBeep(MB_ICONINFORMATION); break; } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } void AddMenus(HWND hwnd) { HMENU hMenubar; hMenubar = CreateMenu(); hMenu = CreateMenu(); AppendMenuW(hMenu, MF_STRING, IDM_MODE_MAP, L"&Map"); AppendMenuW(hMenu, MF_STRING, IDM_MODE_SAT, L"&Satellite"); AppendMenuW(hMenu, MF_STRING, IDM_MODE_TRA, L"&Traffic"); AppendMenuW(hMenu, MF_STRING, IDM_MODE_STR, L"Street &view"); CheckMenuRadioItem(hMenu, IDM_MODE_MAP, IDM_MODE_STR, IDM_MODE_MAP, MF_BYCOMMAND); AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR) hMenu, L"&Map mode"); SetMenu(hwnd, hMenubar); }
In the example, we have four radio menu items; only one of them can be selected at a time. Each radio menu item beeps a different sound.
#define IDM_MODE_MAP 1 #define IDM_MODE_SAT 2 #define IDM_MODE_TRA 3 #define IDM_MODE_STR 4
These are Ids of radio menu items.
case IDM_MODE_MAP: CheckMenuRadioItem(hMenu, IDM_MODE_MAP, IDM_MODE_STR, IDM_MODE_MAP, MF_BYCOMMAND); MessageBeep(MB_ICONERROR); break;
The CheckMenuRadioItem()
checks a specified menu item and makes it a radio item. In addition, it clears all other menu items in the associated group of menu items. The function's first parameter is a handle to the menu that contains the group of menu items. The last parameter indicates the meaning of the previous three parameters; when MF_BYCOMMAND
is specified, these parameters are Ids of menu items. The second parameter is the Id of the first menu item in the group, the third parameter is the Id of the last menu item in the group. The fourth parameter is the menu identifier to be checked.
... AppendMenuW(hMenu, MF_STRING, IDM_MODE_STR, L"Street &view"); CheckMenuRadioItem(hMenu, IDM_MODE_MAP, IDM_MODE_STR, IDM_MODE_MAP, MF_BYCOMMAND); AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR) hMenu, L"&Map mode"); ...
At the beginning, the first radio item is selected.

Submenu
A submenu is a menu located inside another menu.
submenu.c
#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void AddMenus(HWND); #define IDM_FILE_NEW 1 #define IDM_FILE_IMPORT 2 #define IDM_IMPORT_MAIL 11 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSW wc = {0}; wc.lpszClassName = L"Submenu"; wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassW(&wc); CreateWindowW(wc.lpszClassName, L"Submenu", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 350, 250, 0, 0, hInstance, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: AddMenus(hwnd); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_FILE_NEW: MessageBoxW(NULL, L"New file selected", L"Information", MB_OK); break; case IDM_IMPORT_MAIL: MessageBoxW(NULL, L"Import mail selected", L"Information", MB_OK); } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProcW(hwnd, msg, wParam, lParam); } void AddMenus(HWND hwnd) { HMENU hMenubar = CreateMenu(); HMENU hMenu = CreateMenu(); HMENU hSubMenu = CreatePopupMenu(); AppendMenuW(hMenu, MF_STRING, IDM_FILE_NEW, L"&New"); AppendMenuW(hMenu, MF_STRING | MF_POPUP, (UINT_PTR) hSubMenu, L"&Import"); AppendMenuW(hSubMenu, MF_STRING, IDM_IMPORT_MAIL, L"Import &mail"); AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR) hMenu, L"&File"); SetMenu(hwnd, hMenubar); }
In the example, we have two menu items; one is located in the File menu and the other one in the File's Import submenu. Selecting each menu item causes a message box to be displayed.
case IDM_IMPORT_MAIL: MessageBoxW(NULL, L"Import mail selected", L"Information", MB_OK);
When we select the Import mail submenu item, a message box is shown with the "Import mail selected" text.
HMENU hSubMenu = CreatePopupMenu();
A submenu is created with the CreatePopupMenu()
function.
AppendMenuW(hMenu, MF_STRING | MF_POPUP, (UINT_PTR) hSubMenu, L"&Import");
With the AppendMenuW()
function, we add a submenu to the File menu. The MF_POPUP
flag is used both for popup menus and submenus.
AppendMenuW(hSubMenu, MF_STRING, IDM_IMPORT_MAIL, L"Import &mail");
A menu item is normally added to the submenu with the AppendMenuW()
function.

In this part of the Windows API tutorial, we have covered menus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论