【Visual C++】PeekMessage与GetMessage的对比

本文对比了PeekMessage与GetMessage函数在消息队列处理上的不同,通过具体代码展示了两者在应用程序响应性和效率上的差异,并提供了实现随机矩形绘制的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PeekMessage与GetMessage的对比


相同点:
PeekMessage函数与GetMessage函数都用于查看应用程序消息队列,有消息时将队列中

的消息派发出去。

不同点:
无论应用程序消息队列是否有消息,PeekMessage函数都立即返回,程序得以继续执行

后面的语句(无消息则执行其它指令,有消息时一般要将消息派发出去,再执行其它

指令)。
GetMessage函数只有在消息对立中有消息时返回,队列中无消息就会一直等,直至下

一个消息出现时才返回。在等的这段时间,应用程序不能执行任何指令。

(从他们的不同点上来看,PeekMessage函数有点像“乞丐行乞”,有你就施舍点,没

有也不强求。GetMessage函数有点像“强盗打劫”,有你得给,没有我就等你什么时

候有了再给,这段时间我什么都不干,我就等你。)

下面的程序用来在窗体内画随机生成的矩形,分别使用PeekMessage函数和GetMessage

函数实现,读者可以种实际效果看出他们两者的区别。

#include <windows.h>
#include <stdlib.h> // for the rand function

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void DrawRectangle (HWND) ;

int cxClient, cyClient ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("RandRect") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("Random Rectangles"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

//用于替换的部分
while (TRUE)
{
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break ;
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
else
DrawRectangle (hwnd) ;
}
//用于替换的部分
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM

lParam)
{
switch (iMsg)
{
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}

void DrawRectangle (HWND hwnd)
{
HBRUSH hBrush ;
HDC hdc ;
RECT rect ;

if (cxClient == 0 || cyClient == 0)
return ;

SetRect (&rect, rand () % cxClient, rand () % cyClient,
rand () % cxClient, rand () % cyClient) ;

hBrush = CreateSolidBrush (
RGB (rand () % 256, rand () % 256, rand () % 256)) ;
hdc = GetDC (hwnd) ;

FillRect (hdc, &rect, hBrush) ;
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
}
以上程序用PeekMessage函数实现,在应用程序消息队列没有消息时,随机生成的矩形

将坚持不懈地画下去。当有消息产生时,PeekMessage函数获取并派发消息出去,然后

继续画随机生成的矩形。

下面部分代码用于替换WinMain函数中“用于替换的部分”的代码。
while (TRUE)
{
if (GetMessage (&msg, NULL, 0, 0))
{
if (msg.message == WM_QUIT)
break ;
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
DrawRectangle (hwnd) ;
}
else
break;
}
替换后,应用程序产生一个消息(鼠标移动、改变窗体大小等),在窗体内画一个随

机生成的矩形,无消息产生时,窗体无变化。

文章转自http://www.cnblogs.com/faceang/archive/2010/05/25/1743757.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值