Peekmessage and getmessage

Source: Internet
Author: User

BoolPeekmessage(
Lpmsg,
Hwnd,
Uint wmsgfiltermin,
Uint wmsgfiltermax,
Uint wremovemsg
);

When a message exists in the message queue, peekmessage returns true. The last parameter can be:

Pm_remove
Pm_noremove

Indicates whether to delete messages from the message queue when they are received.
Different from getmessage:
Peekmessage returns true if there is a message, even if the message is wm_quit, it will not wait when there is no message,
Instead, return immediately and return false. Getmessage is used to retrieve and delete messages from the message queue. When the message queue is empty, it waits until a message exists in the queue. When a wm_quit message is received, it returns false. This is the only condition in which getmessage returns false. Otherwise, true is returned, indicating that the message is always waiting or processing.
Remember: Neither peekmessage nor getmessage will delete the wm_paint message !!! When you delete the wm_paint message from the queue, it is completed by validaterect, validatergn, beginpaint, and endpaint when the display area becomes valid again. Remember !!!!

Haha, run the followingProgramYou will understand:

#Include <Windows.H>

Lresult callback wndproc ( Hwnd , Uint , Wparam , Lparam ) ;
Int Winapi winmain ( Hinstance,
Hinstance hprevinstance ,
Pstr szcmdline ,
Int Icmdshow ) {
Static Tchar szappname [ ] = Text ( "Rect Demo" ) ;
Hwnd ;
MSG msg ;
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 = Getstockobject ( White_brush ) ;
Wndclass . Lpszmenuname = Null ;
Wndclass . Lpszclassname = Szappname ;

If ( ! Registerclass ( & Wndclass ) ) {
MessageBox ( Null , Text ( "Register failure ..." ) ,
Szappname , Mb_iconerror ) ;
Return 0 ;
}

Hwnd = Createwindow ( Szappname ,
Szappname ,
Ws_overlappedwindow ,
Cw_usedefault ,
Cw_usedefault ,
Cw_usedefault,
Cw_usedefault ,
Null ,
Null ,
Hinstance ,
Null ) ;

Showwindow ( Hwnd , Icmdshow ) ;
Updatewindow ( Hwnd ) ;

While ( Peekmessage ( & MSG, Null , 0 , 0 , Pm_remove ) ) {
If ( MSG . Message = = Wm_quit )
Break ;
Translatemessage ( & MSG ) ;
Dispatchmessage ( & MSG ) ;
}
Return MSG . Wparam ;
}

Lresult callback wndproc ( Hwnd ,
Uint message ,
Wparam ,
Lparam ) {
Static Int Cxclient , Cyclient ;
HDC ;
Paintstruct PS ;
Tchar Buffer [ 5] ;
Static Int I ;
Rect ;

Switch ( Message ) {
Case Wm_size :
Cxclient = Loword ( Lparam ) ;
Cyclient = Hiword ( Lparam ) ;
I = 0 ;
Return 0 ;

Case Wm_paint :
HDC = Getdc ( Hwnd ) ;
Getclientrect ( Hwnd , & Rect ) ;
Drawtext ( HDC , Buffer , Wsprintf ( Buffer , "% 5d" , I + + ) , & Rect ,
Dt_singleline | Dt_center | Dt_vcenter ) ;
Releasedc ( Hwnd , HDC ) ;
Return 0 ;

case wm_destroy :
postquitmessage ( 0 ) ;
return 0 ;
}
return defwindowproc ( hwnd , message , wparam , lparam ) ;
}

both peekmessage and getmessage obtain messages from the system message queue, but they have different properties.
if no message is obtained from the message queue for the first time, the main thread of the program will be suspended by the OS (operating system). When the OS is rescheduled to this thread, in addition, when the message queue is still empty, the nature of the two is different:
If getmessage () is used, the main thread of the program will still be suspended by the OS.
If peekmessage () is used, the program obtains control of the OS and runs for a period of time. This function is mostly used to process the idle time of the system.
getmessage and peekmessage are both get the message from send_message queue.
getmessage supports synchronization mechanism. Peekmessage supports asynchronous mechanism.

Cause: the getmessage function obtains a message from the message queue and deletes the message from the queue.
The peekmessage function does not delete a message in the queue after obtaining a message from the message queue.
In terms of results: getmessage waits for a message (like _ getch) and returns the message only after it is obtained.
This is not the case for peekmessage (like _ kbhit). If a message queue is queried, peekmessage is obtained and returned immediately even if it does not exist.

To implement windows multi-task, getmessage is used, because when getmessage finds that there is no message in its message queue, the control is returned to the system, which can avoid resource waste.

Each call to the getmessage function will definitely retrieve a message. When the message queue is empty, the process will be suspended until there is a message in the message queue.
The peekmessage function returns a message immediately regardless of whether a message exists in the message queue. You can determine whether the message is obtained from the returned value. To be precise, this function queries the message queue, you can specify whether to delete a message from the Message Queue After retrieving the message.
In windows3.1, the message queues of many applications are composed of the getmessage function, while in Win32, the message queues of most applications are composed of the peekmessage function. The messages of VC and BCB are similar, based on Win32's preemptive multi-task processing mechanism, the message loop composed of the peekmessage function does not pose any threat to the system.

1. peekmessage

Function: this function is a message queue of the Message check thread and stores the message (if any) in the specified structure.
Function prototype: bool peekmessage (lpmsg ipmsg, hwnd, uint wmsgfiltermin, uint wmsgfiltermax, uint wremovemsg );
Parameters:
Lpmsg: refers to the MSG structure pointer for receiving message information.
Hwnd: handle of the window in which the message is checked.
Wmsgfiltermin: Specifies the first message in the message range to be checked.
Wmsgfiltermax: Specifies the last message in the message range to be checked.
Wremovemsg: determines how messages are processed. This parameter can have one of the following values:
Pm_noremove: After peekmessage is processed, the message is not removed from the queue.
Pm_remove: After peekmessage is processed, the message is removed from the queue.
Pm_noyield can be freely combined to pm_noremove or pm_remove. This flag prevents the system from releasing threads waiting for idle calling of the program.
By default, messages of all types are processed. To process only some messages, specify one or more of the following values:
Pm_qs_input: Windows nt5.0 and Windows 98: Processes mouse and keyboard messages.
Pm_qs_paint: Windows NT 5.0 and Windows 98: Process Drawing messages.
Pm_qs_postmessage: Windows NT 5.0 and Windows 98: processes all sent messages, including timers and hotkeys.
Pm_qs_sendmessage: Windows NT 5.0 and Windows 98: process all sent messages.
Return Value: If a message is available, a non-zero value is returned. If no message is available, the return value is zero.
Note: Unlike the getmessage function, the peekmesssge function does not wait for the message to be put into the queue before returning the message.
Peekmesssge only obtains messages that are associated with the window marked by the hwnd parameter or messages that are identified by lschild as their subwindows, the message must be in the range determined by the wmsgfitermin and wmsgfihermax parameters. If hwnd is null, peekmessage receives messages from the window of the current calling thread (peekmessage does not receive messages from windows of other threads ). If hwnd is C1, peekmessage only returns a message with a null hwnd value, which is sent by the postthreadmessage function. If wmsgfiltermin and wmsgfiltermax are both zero, getmessage returns all available messages (I .e., out-of-range filtering ).
The constant wm_keyfirst and wmkeylast can be used as the filter value to retrieve all keyboard messages. The constant wm_mousefirst and wm_mouselast can be used to receive all mouse messages.
Peekmessage generally does not clear the wm_paint message from the queue. The message will be retained in the queue until processing is completed. However, if the wm_paint message has an empty update area, the peekmessage will clear the wm_paint message from the queue.

2. Difference Between peekmessage and getmessage
A. getmessage will not be returned until there is a suitable message, and peekmessage is just a glimpse of the message queue.
B. getmessage will delete the message from the queue, while peekmessage can set the last parameter wremovemsg to determine whether to keep the message in the queue.

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.