PeekMessage&GetMessage
来源:互联网 发布:数据恢复吧被卖了 编辑:程序博客网 时间:2024/05/23 00:04
转载自:http://www.cnblogs.com/faceang/archive/2010/05/25/1743757.html
PeekMessage&GetMessage
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;
}
替换后,应用程序产生一个消息(鼠标移动、改变窗体大小等),在窗体内画一个随
机生成的矩形,无消息产生时,窗体无变化。
- peekmessage getmessage
- GetMessage PeekMessage
- peekmessage getmessage
- PeekMessage & GetMessage
- PeekMessage&&GetMessage
- PeekMessage&GetMessage
- PeekMessage & GetMessage
- PeekMessage&GetMessage
- PeekMessage&GetMessage
- PeekMessage&GetMessage
- PeekMessage&GetMessage
- 深入GetMessage和PeekMessage
- 深入GetMessage和PeekMessage
- 深入GetMessage和PeekMessage
- 深入GetMessage和PeekMessage
- PeekMessage和GetMessage函数
- 深入GetMessage和PeekMessage
- GetMessage和PeekMessage
- 在.net(C# or vb.net)中如何取消一个窗体的关闭。
- Flex和Bison
- struts2+jquery+json集成
- POJ 2393
- js检测信任站点,添加信任站点。
- PeekMessage&GetMessage
- Evaluate Reverse Polish Notation
- 继承与派生5:基类与派生类之间的转换
- PAT 1016. Phone Bills
- Mysql JDBC URL 中常用参数说明
- java实现线性表的顺序存储
- 一个关于防止编译器优化特定代码的问题
- HDU1492 The number of divisors(约数) about Humble Numbers
- pat Stack(1057)