3创建菜单
#include<Windows.h>
#include<stdio.h>
//#define WM_MYDESTROY 2
HINSTANCE g_hInstance;
HANDLE g_hOut;
/*顶级菜单 窗口 下拉菜单的步骤
1.创建好窗口
2.创建顶级菜单
3.创建下拉菜单
4.把下拉菜单置于顶级菜单上
5.再把顶级菜单置于窗口上
*/
//创建主菜单
void MyAppenMenu(HWND hWnd)
{
//创建顶级菜单,并获取菜单句柄
HMENU hTopMenu=CreateMenu();
//创建下拉菜单,并获取下拉菜单句柄
HMENU hPopMenu1=CreatePopupMenu();
//增加下拉菜单
AppendMenu(hPopMenu1,MF_STRING,103,"下拉菜单一一");
AppendMenu(hPopMenu1,MF_STRING,104,"下拉菜单一二");
HMENU hPopMenu2=CreatePopupMenu();
//简单显示
AppendMenu(hPopMenu2,MF_STRING,101,"下拉菜单二一");
AppendMenu(hPopMenu2,MF_STRING,102,"下拉菜单二二");
//增加菜单,并把下拉菜单置于顶级菜单中
//下拉显示 //类型转换
AppendMenu(hTopMenu,MF_POPUP,(UINT_PTR)hPopMenu1,"菜单一");
AppendMenu(hTopMenu,MF_POPUP,(UINT_PTR)hPopMenu2,"菜单二");
//创建菜单,并把菜单置于窗口上
SetMenu(hWnd,hTopMenu);
//获取系统菜单句柄
HMENU hSystemMenu=GetSystemMenu(hWnd,false);
//创建新菜单选项
AppendMenu(hSystemMenu,MF_STRING,105,"酷酷");
//删除菜单
DeleteMenu(hSystemMenu,0,MF_BYPOSITION);
//要知道ID
//DeleteMenu(hPopMenu1,101,MF_BYCOMMAND);
DestroyMenu(hTopMenu);
}
//创建右键菜单
void OnRButtonUp(HWND hWnd,LPARAM lParam)
{
HMENU hPop3=CreatePopupMenu();
POINT pt={0};
pt.x=LOWORD(lParam);
pt.y=HIWORD(lParam);
//把客户区(窗口)坐标转换成屏幕坐标
ClientToScreen(hWnd,&pt);
AppendMenu(hPop3,MF_STRING,108,"我的右键菜单");
//TrackPopupMenu(hPop3,TPM_VCENTERALIGN,LOWORD(lParam),HIWORD(lParam)
// ,0,hWnd,NULL);
该函数在指定位置显示快捷菜单,并跟踪菜单项的选择
TrackPopupMenu(hPop3,TPM_VCENTERALIGN,pt.x,pt.y
,0,hWnd,NULL);
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
/*MessageBox(hWnd,TEXT("窗口已创建"),TEXT("窗口"),MB_OKCANCEL)*/
MyAppenMenu(hWnd);break;
case WM_DESTROY:
PostQuitMessage(0);break; //进入系统消息队列
//右击鼠标消息
case WM_RBUTTONUP:
OnRButtonUp(hWnd,lParam);break;
case WM_COMMAND:
// 点击下拉菜单后 出提示
switch(LOWORD(wParam))
{
case 103:
MessageBox(NULL,NULL,"!",MB_OKCANCEL);break;
case 104:
MessageBox(NULL,NULL,"!",MB_OKCANCEL);break;
case 101:
MessageBox(NULL,NULL,"!",MB_OKCANCEL);break;
case 102:
MessageBox(NULL,NULL,"!",MB_OKCANCEL);break;
}
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
void MyRegisterWindowClass(HINSTANCE hInstance,LPCSTR lpClassName,WNDPROC wndproc)
{
WNDCLASS wc={0};
wc.lpszClassName=lpClassName;
wc.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
wc.cbClsExtra=NULL;
wc.cbWndExtra=NULL;
wc.hbrBackground=(HBRUSH)COLOR_WINDOW;
wc.hCursor=NULL;
wc.hIcon=NULL;
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;
wc.lpszMenuName=NULL;
RegisterClass(&wc);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//注册主窗口
MyRegisterWindowClass(hInstance,TEXT("main"),WndProc);
//创建主窗口
HWND hWnd=CreateWindow(TEXT("main"),TEXT("TEXT"),WS_OVERLAPPEDWINDOW,200,200,1000,600,NULL,NULL,hInstance,NULL);
//显示窗口
ShowWindow(hWnd,SW_SHOW); //必须显示主窗口
UpdateWindow(hWnd);
//消息循环
MSG msg={0};
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}