《编程思想之消息机制》一文中我们讲了消息的相关概念和消息机制的模拟,本文将进一步聊聊C++中的消息机制。
从简单例子探析核心原理
在讲之前,我们先看一个简单例子:创建一个窗口和两个按钮,用来控制窗口的背景颜色。其效果如下:
图 2 :效果图
?
Win32Test.h
#pragma once
#include
#include
#include
//资源ID #define ID_BUTTON_DRAW 1000 #define ID_BUTTON_SWEEP 1001 // 注册窗口类 ATOM AppRegisterClass(HINSTANCE hInstance); // 初始化窗口 BOOL InitInstance(HINSTANCE, int); // 消息处理函数(又叫窗口过程) LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // (白色背景)按钮事件 void OnButtonWhite(); // (灰色背景)按钮事件 void OnButtonGray(); // 绘制事件 void OnDraw(HDC hdc);
Win32Test.cpp
#include stdafx.h
#include Win32Test.h
//字符数组长度
#define MAX_LOADSTRING 100
//全局变量
HINSTANCE hInst; // 当前实例
TCHAR g_szTitle[MAX_LOADSTRING] = TEXT(Message process); // 窗口标题
TCHAR g_szWindowClass[MAX_LOADSTRING] = TEXT(AppTest); // 窗口类的名称
HWND g_hWnd; // 窗口句柄
bool g_bWhite = false; // 是否为白色背景
//WinMain入口函数
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// 注册窗口类
if(!AppRegisterClass(hInstance))
{
return (FALSE);
}
// 初始化应用程序窗口
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
// 注册窗口类
ATOM AppRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = g_szWindowClass;
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
// 保存实例化句柄并创建主窗口
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 保存handle到全局变量
g_hWnd = CreateWindow(g_szWindowClass, g_szTitle, WS_OVERLAPPEDWINDOW, 0, 0, 400, 300, NULL, NULL, hInstance, NULL);
// 创建按钮
HWND hBtWhite = CreateWindowEx(0, LButton, L白色, WS_CHILD | WS_VISIBLE | BS_TEXT, 100, 100, 50, 20, g_hWnd, (HMENU)ID_BUTTON_DRAW, hInst, NULL);
HWND hBtGray = CreateWindowEx(0, LButton, L灰色, WS_CHILD | WS_VISIBLE | BS_CENTER, 250, 100, 50, 20, g_hWnd, (HMENU)ID_BUTTON_SWEEP, hInst, NULL);
if (!g_hWnd)
{
return FALSE;
}
ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);
return TRUE;
}
// (窗口)消息处理
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
//wmEvent = HIWORD(wParam);
switch (wmId)
{
case ID_BUTTON_DRAW:
OnButtonWhite();
break;
case ID_BUTTON_SWEEP:
OnButtonGray();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
OnDraw(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//事件处理
//按下hBtWhite时的事件
void OnButtonWhite()
{
g_bWhi