菜单
作者:互联网
#include "windows.h"
#include "5-1.h"
HMENU hmenu, haddmenu; //定义菜单句柄
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG Msg;
HACCEL hAccel;
WNDCLASS wndclass;
char lpszMenuName[] = "Menu"; //加的
char lpszClassName[] = "菜单";
char lpszTitle[] = "My_Menu_Example";
wndclass.style = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = lpszMenuName; //加载窗口类菜单
wndclass.lpszClassName = lpszClassName;
if (!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hwnd = CreateWindow(lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
//加载加速键资源
hAccel = LoadAccelerators(hInstance, lpszMenuName);
while (GetMessage(&Msg, NULL, 0, 0))
{
//在消息循环中截获加速键消息
if (!TranslateAccelerator(hwnd, hAccel, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
//处理菜单消息
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_ADDMENU: //在主菜单中添加弹出式统计计算菜单的消息
hmenu = GetMenu(hwnd); //获取主菜单句柄
haddmenu = CreateMenu(); //动态创建菜单
//在创建的菜单中增加菜单项
AppendMenu(haddmenu, MF_ENABLED, IDM_qiuhe, "求和");
AppendMenu(haddmenu, MF_ENABLED, IDM_fangcha, "方差");
AppendMenu(haddmenu, MF_ENABLED, IDM_pingjunzhi, "平均值");
AppendMenu(haddmenu, MF_ENABLED, IDM_junfanggen, "均方根");
//将创建的弹出式菜单插入主菜单中
InsertMenu(hmenu, 2, MF_POPUP | MF_BYPOSITION, (UINT)haddmenu, "统计计算(&C)");
//相应改变菜单中有关绘图统计计算菜单项的属性
EnableMenuItem(hmenu, IDM_ADDMENU, MF_GRAYED);
EnableMenuItem(hmenu, IDM_DELMENU, MF_ENABLED);
DrawMenuBar(hwnd); //重新显示窗口菜单
break;
case IDM_DELMENU: //从主菜单中删除弹出式统计计算菜单
DeleteMenu(hmenu, 2, MF_BYPOSITION); //删除统计计算菜单项
EnableMenuItem(hmenu, IDM_ADDMENU, MF_ENABLED); //相应改变“文件”菜单中有关统计计算菜单项的属性
EnableMenuItem(hmenu, IDM_DELMENU, MF_GRAYED);
DrawMenuBar(hwnd); //重新显示窗口菜单
break;
case IDM_EXIT:
SendMessage(hwnd, WM_DESTROY, 0, 0); //选择“退出”菜单项时,向应用程序发出WM_DESTROY消息
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
标签:菜单,MF,hwnd,hmenu,IDM,wndclass 来源: https://www.cnblogs.com/nanfengnan/p/13747879.html