c - 如何识别WindowProcedure中的AppendMenu MF_POPUP Menu?
问题描述
我越来越糊涂了。
MF_POPUP
0x00000010L
指定菜单项打开一个下拉菜单或子菜单。该
uIDNewItem
参数指定下拉菜单或子菜单的句柄。此标志用于将菜单名称添加到菜单栏,或打开下拉菜单、子菜单或快捷菜单的子菜单的菜单项。
(https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-appendmenua)
是的,但是如果参数现在是下拉菜单或子菜单的句柄,我们现在如何指定菜单的uID并在 WindowProcedure 中引用它。uIDNewItem
解决方案
这是一个简单的代码示例,您可以参考,
#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define EXIT_ID 1
#define SUB_ID 2
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message)
{
case WM_CREATE:
{
HMENU hMenubar = CreateMenu();
HMENU hFileMenu = CreateMenu(); //file is a regular menu.. Exit is a regular sub-menu..
HMENU hDisplayMenu = CreatePopupMenu(); //display is a popup-menu because it has children.
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hFileMenu, "File");
AppendMenu(hFileMenu, MF_STRING | MF_POPUP, (UINT_PTR)hDisplayMenu, "Display");
AppendMenu(hDisplayMenu, MF_STRING, SUB_ID, "Sub");
AppendMenu(hFileMenu, MF_STRING, EXIT_ID, "Exit");
SetMenu(hwnd, hMenubar);
}
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case SUB_ID:
MessageBox(hwnd, "You click Sub menu", " ", MB_OK);
break;
case EXIT_ID:
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
};
HINSTANCE hinst;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow) {
HWND hwnd;
hinst = GetModuleHandle(NULL);
// create a window class:
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hinst;
wc.lpszClassName = "win32";
// register class with operating system:
RegisterClass(&wc);
// create and show window:
hwnd = CreateWindow("win32", "My program", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, 0, 0, 1000, 800, NULL, NULL, hinst, NULL);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, SW_SHOW);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
调试:
EXIT_ID
并且SUB_ID
是子菜单的 uID。您需要手动定义它们。
#define EXIT_ID 1
#define SUB_ID 2
更新:
您可以使用WM_MENUSELECT获取单击的菜单的句柄并比较它们的 hmenus。
当用户选择菜单项时发送到菜单的所有者窗口。
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HMENU hMenubar;
switch (message)
{
case WM_CREATE:
{
hMenubar = CreateMenu();
HMENU hFileMenu = CreateMenu(); //file is a regular menu.. Exit is a regular sub-menu..
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hFileMenu, L"File");
SetMenu(hWnd, hMenubar);
}
break;
case WM_MENUSELECT:
{
HMENU hmenu = (HMENU)lParam;
if (hmenu == hMenubar)
{
MessageBox(hWnd, L"You click main menu", L" ", MB_OK);
}
}
break;
...
像这样,
推荐阅读
- php - 如何访问 vagrant 中设置的虚拟主机
- python - 有没有办法加快递归加载雅虎财务数据并将其与使用 pandas 的选定数据进行比较,尤其是 100,000 只股票
- javascript - 更改焦点上文本框的输入字体语言
- mysql - “IN/ALL/ANY 子查询”中的未知列
- html - 如何选择除某些元素的子树中的元素之外的所有元素?
- java - 在Java中排序时如何考虑最后的空间
- python - 如何从Python中通过“stdin”输入的字符串中删除子字符串
- asp.net - 如何在另一个模型的控制器操作中初始化一个模型?
- delphi - 使用 indy UDP 客户端 (delphi) 实现 DTLS
- amazon-web-services - 将文件直接从远程 url 传输到 S3 Bucket