Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C++

Some Tricks with Peek Message

Rate me:
Please Sign up or sign in to vote.
2.28/5 (15 votes)
12 Oct 2006CPOL1 min read 73.4K   21   7
Some interesting things that you can do with PeekMessage.

Introduction

I started off in VC++ after programming for sometime in VB (a few months at home). One function that I missed from VB was DoEvents. But I have found an equivalent one in VC++ using the function PeekMessage. Most of you might know these tricks, but this is mainly for those who don't know how to do this.

Why I needed a DoEvents function

Well, I am in the midst of developing an interesting personal project, the idea is a secret ;). So what happened was that I had a tight loop kind of a situation, and I didn't want to use a thread because it was un-necessary. That's when I started thinking about a DoEvents equivalent in VC++.

DoEvents code

Now enough of preaching :). Let's get to the code that will help us in doing this.

C++
static void DoEvents( HWND hWnd_i )
{
   MSG stMsg = { 0 };
   while( PeekMessage( &stMsg, hWnd_i, 0, 0, PM_REMOVE ))
   {
       TranslateMessage( &stMsg );
       DispatchMessage( &stMsg );
    }
}

The loop in which this is called is as follows...

C++
for( UINT uFileIndex = 0; uFileIndex < uFileCount; ++uFileIndex )
{
   UpdateProgressBar( uFileIndex + 1 );
   if( !( uFileIndex % LOOP_THRESH_HOLD_FOR_DO_EVENTS ))
   {
      ResUtils::DoEvents( this->GetSafeHwnd() );
   }
}

I defined a LOOP_THRESH_HOLD_FOR_DO_EVENTS for calling this function. This will be useful if your loop is a low priority one. Now my GUI does not hang, painting takes place properly too.

Flushing the keyboard and mouse input buffer

How about flushing the keyboard input buffer and the mouse input buffer selectively...

C++
static void FlushKeyboardInputBuffer( HWND hWnd_i )
{
   MSG stMsg = { 0 };
   while( PeekMessage( &stMsg, hWnd_i , WM_KEYFIRST, WM_KEYLAST, PM_REMOVE ))
      ;
}

static void FlushMouseInputBuffer( HWND hWnd_i )
{
   MSG stMsg = { 0 };
   while( PeekMessage( &stMsg, hWnd_i, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ))
      ;
}

Conclusion

These are a few of the ideas that I got while programming. Hope this helps. There are some other flags that you can pass to PeekMessage, e.g.:

PM_QS_INPUTWindows 98/Me, Windows 2000/XP: Process mouse and keyboard messages.
PM_QS_PAINTWindows 98/Me, Windows 2000/XP: Process paint messages.
PM_QS_POSTMESSAGEWindows 98/Me, Windows 2000/XP: Process all posted messages, including timers and hotkeys.
PM_QS_SENDMESSAGEWindows 98/Me, Windows 2000/XP: Process all sent messages.

For more information on how to do idle time processing using PeekMessage, search MSDN with the OnIdle keyword, and you will bump into pretty interesting topics.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Microsoft
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionClearing Keyboard Buffer with Peek Message on VB.net Pin
Member 1459669118-Sep-19 2:03
Member 1459669118-Sep-19 2:03 
GeneralMy vote of 5 Pin
upasanabhatia29-Nov-10 18:25
upasanabhatia29-Nov-10 18:25 
QuestionMessage 0x00AE in XP Pin
Ajay L D21-Nov-06 3:31
Ajay L D21-Nov-06 3:31 
AnswerRe: Message 0x00AE in XP Pin
Nibu babu thomas23-Nov-06 19:38
Nibu babu thomas23-Nov-06 19:38 
GeneralRe: Message 0x00AE in XP Pin
helloworld222212-May-07 8:36
helloworld222212-May-07 8:36 
GeneralRe: Message 0x00AE in XP Pin
Nibu babu thomas13-May-07 17:22
Nibu babu thomas13-May-07 17:22 
AnswerRe: Message 0x00AE in XP Pin
Romualdas Cukuras24-May-07 2:01
Romualdas Cukuras24-May-07 2:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.