菜单练习

5-1.h //头文件,里面存ID标识

#define IDM_ADDMENU 15
#define IDM_DELMENU 16
#define IDM_EXIT 17
#define IDM_HELP 22
#define IDM_qiuhe 23
#define IDM_fangcha 24
#define IDM_pinjunzhi 25
#define IDM_junfanggen 26

 

5-1.rc //资源文件

#include "5-1.h"
#include "windows.h"
MENU MENU DISCARDABLE
BEGIN
POPUP "文件(&F)"
BEGIN
MENUITEM "创建统计计算菜单项(&P)\t Ctrl+P", IDM_ADDMENU
MENUITEM "删除统计计算菜单项(&D)\t Ctrl+D", IDM_DELMENU, GRAYED
MENUITEM SEPARATOR
MENUITEM "退出(&X)", IDM_EXIT
END
MENUITEM "帮助(&H)", IDM_HELP

END

 

MENU ACCELERATORS MOVEABLE PURE
BEGIN
"^P", IDM_ADDMENU, ASCII
"^D", IDM_DELMENU, ASCII
END

 

 

5-1.cpp //源文件

#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_pinjunzhi,"平均值");
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;
}

posted @ 2020-10-01 08:31  nanfengnan  阅读(143)  评论(0编辑  收藏  举报