PeekMessage與GetMessage的對比
相同點:
PeekMessage函數與GetMessage函數都用於查看應用程序消息隊列,有消息時將隊列中windows
的消息派發出去。dom
不一樣點:
不管應用程序消息隊列是否有消息,PeekMessage函數都當即返回,程序得以繼續執行函數
後面的語句(無消息則執行其它指令,有消息時通常要將消息派發出去,再執行其它ui
指令)。
GetMessage函數只有在消息對立中有消息時返回,隊列中無消息就會一直等,直至下spa
一個消息出現時才返回。在等的這段時間,應用程序不能執行任何指令。htm
(從他們的不一樣點上來看,PeekMessage函數有點像「乞丐行乞」,有你就施捨點,沒blog
有也不強求。GetMessage函數有點像「強盜打劫」,有你得給,沒有我就等你什麼時隊列
候有了再給,這段時間我什麼都不幹,我就等你。)get
下面的程序用來在窗體內畫隨機生成的矩形,分別使用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;
}
替換後,應用程序產生一個消息(鼠標移動、改變窗體大小等),在窗體內畫一個隨
機生成的矩形,無消息產生時,窗體無變化。