win32创建窗口,点击“非模式对话框”该菜单项时,在主菜单条下添加一个弹出式菜单“对话框”,下面有菜单项,分别为“打开”、“关闭”、分隔线和“删除菜单”,初始情况下“删除菜单”为不可用
时间: 2023-11-10 09:05:28 浏览: 178
以下是示例代码:
```c++
#include <windows.h>
// 全局变量
HINSTANCE hInst; // 当前实例句柄
HWND hWndMain; // 主窗口句柄
// 菜单命令 ID
#define IDM_OPEN 101
#define IDM_CLOSE 102
#define IDM_SEPARATOR 103
#define IDM_DELETE 104
// 窗口过程
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_OPEN:
MessageBox(hWnd, TEXT("打开对话框"), TEXT("提示"), MB_OK);
break;
case IDM_CLOSE:
MessageBox(hWnd, TEXT("关闭对话框"), TEXT("提示"), MB_OK);
break;
case IDM_DELETE:
MessageBox(hWnd, TEXT("删除菜单"), TEXT("提示"), MB_OK);
break;
}
break;
case WM_CREATE:
{
// 创建“对话框”弹出式菜单
HMENU hMenuPopup = CreatePopupMenu();
AppendMenu(hMenuPopup, MF_STRING, IDM_OPEN, TEXT("打开"));
AppendMenu(hMenuPopup, MF_STRING, IDM_CLOSE, TEXT("关闭"));
AppendMenu(hMenuPopup, MF_SEPARATOR, IDM_SEPARATOR, NULL);
AppendMenu(hMenuPopup, MF_GRAYED | MF_STRING, IDM_DELETE, TEXT("删除菜单"));
// 添加“非模式对话框”菜单项
HMENU hMenu = GetMenu(hWnd);
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hMenuPopup, TEXT("对话框"));
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// WinMain 入口函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 注册窗口类
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.lpszClassName = TEXT("MainWindowClass");
RegisterClassEx(&wcex);
// 创建主窗口
hInst = hInstance;
hWndMain = CreateWindow(TEXT("MainWindowClass"), TEXT("Win32 Window"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
if (!hWndMain)
{
return FALSE;
}
// 显示主窗口
ShowWindow(hWndMain, nCmdShow);
UpdateWindow(hWndMain);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
```
在主窗口的创建过程中,创建一个弹出式菜单,并添加菜单项。在窗口过程中,处理菜单命令,并显示相应的提示框。在“删除菜单”菜单项上设置 MF_GRAYED 标志,使其不可用。
阅读全文