#include <windows.h>
#include "my.h"
#include "resource.h"
HMENU hmenu,haddmenu;
LRESULT CALLBACK wndproc(HWND hwnd,UINT message,UINT wParam,LONG lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASS wndcls;
HWND hwnd;
MSG message;
HACCEL haccel;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=wndproc;
wndcls.lpszClassName="noname";
wndcls.lpszMenuName="Menu";
wndcls.style=0;
if(!RegisterClass(&wndcls))return FALSE;
hwnd=CreateWindow("noname","菜单实例",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
haccel=LoadAccelerators(hInstance,"Menu");
while(GetMessage(&message,NULL,0,0))
{
if(!TranslateAccelerator(hwnd,haccel,&message))
{
TranslateMessage(0);
DispatchMessage(0);
}
}
return message.wParam;
}
LRESULT CALLBACK wndproc(HWND hwnd,UINT message,UINT wParam,LONG 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_PINGJUN,"平均值");
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);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default: return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}