目前理解的windows消息机制是:
消息产生(点击按钮)-> 消息循环(消息队列)-> 消息处理
windows有一个系统消息队列,为每一个应用程序分配一个应用程序消息队列,保存各种窗口消息
这里通过VS自带的win32应用demo来了解windows消息机制:
// rc_test.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "rc_test.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_RC_TEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RC_TEST));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc; //指定该窗口的消息事件处理函数
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
HICON htestIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RC_TEST));
if (htestIcon == NULL)
{
MessageBox(NULL, _T("加载图标失败"), _T("错误"), MB_ICONERROR);
return ATOM();
}
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RC_TEST)); //任务栏图标
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); //鼠标光标(箭头,等待)
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW +1); //窗口背景颜色
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_RC_TEST);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); //标题栏小图标
//注册窗口类
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
//HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
// CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
//创建窗口
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
100, 100, 500, 500, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
//显示窗口,nCmdShow控制窗口如何显示:SW_SHOWMAXIMIZED表示最大化显示
ShowWindow(hWnd, nCmdShow);
//直接发送WM_PAINT消息到指定窗口的窗口处理程序,不经过应用队列
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDS_STRING1001:
MessageBox(NULL, _T("HELLO"), _T("WORLD"), MB_ICONHAND);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
第一步首先是注册主窗口,在MyRegisterClass函数里,指定了该窗口的处理函数。窗口消息的分发处理在WndProc这个函数完成;
wcex.lpfnWndProc = WndProc; //指定该窗口的消息事件处理函数
第二步是窗口的创建显示;
第三步是消息循环:循环获取消息并分发消息;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
第四步是窗口处理程序WndProc;
WndProc:
窗口创建的时候,销毁的时候,resize,move,minimize都会调用WndProc
用户不处理的消息交给DefWindowProc处理
- 队列消息: 键盘,鼠标,定时器,repaint(WM_PAINT),quit(WM_QUIT)
- 非队列消息:非队列消息通常由windows函数调用产生,例如:CreateWindow(WM_CREATE), UpdateWindow(WM_PAINT)
注意:键盘或鼠标消息会生成非队列消息,例如鼠标点击菜单栏某项目时,会产生非队列消息WM_COMMAND。
窗口被挡住后又出现,windows会要求程序重绘
用户不处理的WM_PAINT消息要传给DefWindowProc处理
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
BeginPaint和EndPaint之间没有内容,用来验证之前无效的区域。