以下代码实现菜单窗口和快捷键的ctrl+o打开的操作
#include <windows.h>
#include<tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <string>
#define IDM_OPEN 102
/*鼠标消息
* 键盘消息
* Onkeydown
* Onkeyup
* //键盘扫描码
* /lParam>>16&0ff
快捷键消息
命令消息WM_COMMAND
1.菜单消息
2.快捷键消息
3.控件消息
自定义消息
窗口消息
客户区域的概念(Client Aera)
非客户区
*/
using namespace std;
string g_Text;
TEXTMETRIC g_tm;//字体信息
VOID showerrormassage()
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |/* 分别为FORMAT_MESSAGE_ALLOCATE_BUFFER由函数分配输出缓冲区,
FORMAT_MESSAGE_FROM_SYSTEM表示程序将会在系统消息表资源中搜索所需消息,FORMAT_MESSAGE_IGNORE_INSERTS程序将会忽略搜索到消息中的插入序列。 */
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION);
LocalFree(lpMsgBuf);
}
LRESULT OnCreate(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
OutputDebugString(_T("[11syy]WM_CREATE\n"));
HDC hdc = GetDC(hwnd);
SelectObject(hdc,
GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc, &g_tm);
ReleaseDC(hwnd, hdc);
return TRUE;
}
LRESULT OnClese(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
OutputDebugString(_T("[11syy]WM_ClOSE\n"));
DestroyWindow(hwnd);
return TRUE;
}
LRESULT OnDestroy(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PostMessage(hwnd, WM_QUIT, 0, NULL);
OutputDebugString(_T("[11syy]WM_DESTROY\n"));
return TRUE;
}
LRESULT OnKeydown(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
OutputDebugString(_T("[11syy]WM_KEYDOWN\n"));
return TRUE;
}
LRESULT OnKeyup(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
OutputDebugString(_T("[11syy]WM_KEYUP\n"));
return TRUE;
}
LRESULT OnCommand(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
OutputDebugString(_T("[11syy]Oncommnd \n"));
WORD WID = LOWORD(wParam);
WORD wNotifyCode = HIWORD(wParam);
switch (WID)
{
case 102://这里定义宏IDM_OPEN
MessageBox(NULL, "打开", "asm", MB_OK);
break;
}
//if (wNotifyCode == 1)//快捷键
//{
//}
//else if (wNotifyCode == 0)//菜单
//{
//
//}
//else
if(lParam!=NULL) {//控件
}
return TRUE;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
LRESULT lResult = FALSE;
switch (uMsg)
{
case WM_CREATE:
lResult = OnCreate(hwnd, uMsg, wParam, lParam);
break;
case WM_CLOSE:
lResult = OnClese(hwnd, uMsg, wParam, lParam);
break;
case WM_DESTROY:
lResult = OnDestroy(hwnd, uMsg, wParam, lParam);
break;
case WM_KEYDOWN:
lResult = OnKeydown(hwnd, uMsg, wParam, lParam);
break;
case WM_KEYUP:
lResult = OnKeyup(hwnd, uMsg, wParam, lParam);
break;
case WM_COMMAND:
lResult = OnCommand(hwnd, uMsg, wParam, lParam);
break;
}
//无效区域
if (!lResult) {
return DefWindowProc(hwnd, uMsg, wParam, lParam);//默认窗口过程处理
}
return lResult;
}
//图形界面,窗口
int WINAPI _tWinMain(HINSTANCE hInstance,//应用程序示例句柄
HINSTANCE hPrevInstance,//保留
TCHAR* lpCmdline, //命令行参数,LPSTR可能会变成Unicode
int nCmdShow) {
//申请局部堆地址地址空间
//HLOCAL hMem = LocalAlloc(LHND,100);//locallock localfree 系统会碎片整理
//LPCVOID lpMemory = LocalLock(hMem);
//LocalFree(hMem);
//申请全局堆地址空间 历史遗留问题
//GlobalAlloc();
//申请堆地址空间
ACCEL*aPFNs =(ACCEL*)HeapAlloc(GetProcessHeap(),0,sizeof(ACCEL)*2);//底层都是调用这个
if (aPFNs == nullptr)
{
showerrormassage();
return 0;
}
//申请堆空间,并且指定可读可写
//VirtualAlloc();
aPFNs[0].fVirt = FCONTROL | FVIRTKEY;
aPFNs[0].key = 'O';
aPFNs[0].cmd = IDM_OPEN;
//创建快捷键表
HACCEL hAccel =CreateAcceleratorTable(aPFNs,2);
if (hAccel == NULL)
{
showerrormassage();
return 0;
}
//1.注册窗口
TCHAR szWndclassName[] = { _T("chongmousyy") };
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;//窗口类型
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDC_HAND);//图标
wc.hCursor = LoadCursor(NULL, IDC_ARROW);//光标FDXX
wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));//窗口背景颜色刷子
wc.lpszClassName = szWndclassName;//窗口类名,窗口名字不可以为空
wc.lpszMenuName = NULL;//窗口菜单
if (RegisterClassEx(&wc) == 0)
{
showerrormassage();
return 0;
};
//2.创建窗口
TCHAR szWndName[] = { _T("翀某人") };
HWND hwnd = CreateWindowEx(0,
szWndclassName,
szWndName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == 0)
{
showerrormassage();
return 0;
}
HMENU hMenu = CreateMenu();
//弹出菜单
AppendMenu(hMenu, MF_STRING|MF_POPUP,(UINT_PTR)hMenu,"文件(&f)");
AppendMenu(hMenu, MF_STRING|MF_POPUP, (UINT_PTR)hMenu, "编辑(&E)");
//添加子菜单
HMENU hSubMenu = GetSubMenu(hMenu, 0);
AppendMenu(hSubMenu, MF_STRING, IDM_OPEN, "打开(&O)");
SetMenu(hwnd, hMenu);
RECT rc;
GetClientRect(hwnd, &rc);
//3.显示跟新窗口
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
//4.消息循环(消息队列)
BOOL bRET;
MSG msg;
while ((bRET = GetMessage(&msg, NULL, 0, 0)) != 0) {
if (bRET == -1) {
break;
}
else
{
//转换快捷键消息
if (!TranslateAccelerator(hwnd,hAccel,&msg))
//转发消息
{
TranslateMessage(&msg);//转换键盘消息为字符消息
DispatchMessage(&msg);
}//派发消息
}
}
//5.消息处理
//删除快捷键表
DestroyAcceleratorTable(hAccel);
HeapFree(GetProcessHeap(),0,aPFNs);
//资源
return 0;
}