Comparison of PeekMessage and GetMessage

Source: Internet
Author: User

Comparison of PeekMessage and GetMessage
Same point:
Both the PeekMessage function and the GetMessage function are used to view the application message queue, when there is a message in the queue

The news was distributed.

Different points:
The PeekMessage function returns immediately regardless of whether the application message queue has a message, and the program continues execution

The following statement (no message executes other instructions, the message is generally sent out, and then other

directive).
The GetMessage function returns only if there is a message in the opposite of the message, and no message in the queue waits until the next

Returned when a message appears. During the waiting period, the application cannot execute any instructions.

(from their different points of view, the PeekMessage function is a bit like "beggar begging", you have to give a point, no

There is no forced. GetMessage function A bit like "robber Rob", have you to give, no I just wait for you what when

I'll wait for you when I give you something and I don't do anything this time. )

The following program is used to draw randomly generated rectangles inside the form, using the PeekMessage function and GetMessage

function implementation, the reader can see the difference between the two in practical effect.

#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);

The part to replace
while (TRUE)
{
if (PeekMessage (&msg, NULL, 0, 0, pm_remove))
{
if (msg.message = = wm_quit)
break;
TranslateMessage (&AMP;MSG);
DispatchMessage (&AMP;MSG);
}
Else
DrawRectangle (HWND);
}
The part to replace
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 ()%, rand ()%, rand ()% 256));
HDC = GetDC (hwnd);

FillRect (hdc, &rect, hbrush);
ReleaseDC (hwnd, HDC);
DeleteObject (Hbrush);
}
The above program is implemented with the PeekMessage function and randomly generated rectangles when there is no message in the application message queue

will continue to draw. When a message is generated, the PeekMessage function obtains and dispatches the message out, and then

Continue drawing a randomly generated rectangle.

The following section of code replaces the code for "parts for substitution" in the WinMain function.
while (TRUE)
{
if (GetMessage (&msg, NULL, 0, 0))
{
if (msg.message = = wm_quit)
break;
TranslateMessage (&AMP;MSG);
DispatchMessage (&AMP;MSG);
DrawRectangle (HWND);
}
Else
Break
}
When replaced, the application generates a message (mouse movement, resizing of the form, and so on), in the form with a

Machine-generated rectangles, no messages generated, no changes to the form

Comparison of PeekMessage and GetMessage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.