4创建快捷键
#include<Windows.h>
#include<stdio.h>
//创建主菜单
void MyAppenMenu(HWND hWnd)
{
//创建顶级菜单,并获取菜单句柄
HMENU hTopMenu = CreateMenu();
//创建下拉菜单,并获取下拉菜单句柄
HMENU hPopMenu1 = CreatePopupMenu();
//增加下拉菜单
AppendMenu(hPopMenu1, MF_STRING, 101, "新建");
AppendMenu(hPopMenu1, MF_STRING, 102, "退出");
HMENU hPopMenu2 = CreatePopupMenu();
//简单显示
AppendMenu(hPopMenu2, MF_STRING, 103, "关于");
//增加菜单,并把下拉菜单置于顶级菜单中
//下拉显示 //类型转换
AppendMenu(hTopMenu, MF_POPUP, (UINT_PTR)hPopMenu1, "文件");
AppendMenu(hTopMenu, MF_POPUP, (UINT_PTR)hPopMenu2, "帮助");
//创建菜单,并把菜单置于窗口上
SetMenu(hWnd, hTopMenu);
DestroyMenu(hTopMenu);
}
void MyKey(char *str)
{
MessageBox(NULL, "我是正式学员",str, MB_OKCANCEL);
}
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_SYSCOMMAND:
{
break;
}
//键盘按下
case WM_KEYDOWN:
{
if (GetAsyncKeyState(VK_LCONTROL)&&0x8000)
if (GetAsyncKeyState(VK_F2))
MyKey("ctrl+F2调用关于");
break;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case 103:
MyKey("点击菜单");
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;
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, 800, 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;
}