消息的产生与处理流程
typedef struct tagMSG {
HWND hwnd; //句柄
UINT message; //消息类型
WPARAM wParam; //消息详细信息
LPARAM lParam; //消息详细信息
DWORD time; //消息产生时间
POINT pt; //消息产生位置
DWORD lPrivate;
} MSG, *PMSG, *NPMSG, *LPMSG;
WINUSER.H
其中存放了消息类型,可以自行查看。
消息什么时候产生?
消息是时刻都在产生的,鼠标、键盘、其他应用程序(通过API)、内核程序都会产生消息。
窗口关闭消息
实现关闭窗口时,完全退出程序。
#include "framework.h"
#include "Project2.h"
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
// TODO: 在此处放置代码。
//1.定义窗口是什么样的
TCHAR className[] = TEXT("MyFirstWindow");
WNDCLASS wndclass = { 0 };
wndclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND; //背景颜色
wndclass.lpszClassName = className; //名称
wndclass.hInstance = hInstance; //窗口属于谁
wndclass.lpfnWndProc = WindowProc; //窗口消息/窗口回调/窗口过程函数
RegisterClass(&wndclass); //注册窗口类
//2.创建并显示窗口
HWND hwnd = CreateWindow(className, TEXT("Win32窗口程序"), WS_OVERLAPPEDWINDOW, 10, 10, 800, 600, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOW); //显示窗口
//3.接收消息并处理
MSG msg;
BOOL bRet;
WCHAR szBuffer[0x80];
//循环取出所有消息消息
while ((bRet = GetMessage(&msg, NULL, NULL, NULL)) != 0)
{
if (bRet == -1)
{
wsprintfW(szBuffer, L"Error:%d\0", GetLastError());
OutputDebugString(szBuffer);
}
else
{
//转换消息 到底按了哪个键,将虚拟码转换为字符。
TranslateMessage(&msg);
//分发消息 回到0环区分是哪个窗口的消息
DispatchMessage(&msg);
}
}
return 0;
}
//窗口消息/窗口回调/窗口过程函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
{
//使当前程序退出
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
键盘按下消息
输出按下了键盘上的哪个按键。
#include "framework.h"
#include "Project2.h"
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
// TODO: 在此处放置代码。
//1.定义窗口是什么样的
TCHAR className[] = TEXT("MyFirstWindow");
WNDCLASS wndclass = { 0 };
wndclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND; //背景颜色
wndclass.lpszClassName = className; //名称
wndclass.hInstance = hInstance; //窗口属于谁
wndclass.lpfnWndProc = WindowProc; //窗口消息/窗口回调/窗口过程函数
RegisterClass(&wndclass); //注册窗口类
//2.创建并显示窗口
HWND hwnd = CreateWindow(className, TEXT("Win32窗口程序"), WS_OVERLAPPEDWINDOW, 10, 10, 800, 600, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOW); //显示窗口
//3.接收消息并处理
MSG msg;
BOOL bRet;
WCHAR szBuffer[0x80];
//循环取出所有消息消息
while ((bRet = GetMessage(&msg, NULL, NULL, NULL)) != 0)
{
if (bRet == -1)
{
wsprintfW(szBuffer, L"Error:%d\0", GetLastError());
OutputDebugString(szBuffer);
}
else
{
//转换消息 到底按了哪个键,将虚拟码转换为字符。
TranslateMessage(&msg);
//分发消息 回到0环区分是哪个窗口的消息
DispatchMessage(&msg);
}
}
return 0;
}
//窗口消息/窗口回调/窗口过程函数
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CHAR:
{
WCHAR szOutBuffer[0x80];
wsprintfW(szOutBuffer, L"Message: %c - %c\n", wParam, lParam);
OutputDebugString(szOutBuffer);
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
TranslateMessage()的作用
到底按了哪个键,将虚拟码转换为字符。如果没有它,只能自己去转换。