Click here to Skip to main content
Licence CPOL
First Posted 12 Oct 2006
Views 35,850
Bookmarked 17 times

Some Tricks with Peek Message

By Nibu babu thomas | 12 Oct 2006
Some interesting things that you can do with PeekMessage.
2 votes, 14.3%
1
5 votes, 35.7%
2
1 vote, 7.1%
3
2 votes, 14.3%
4
4 votes, 28.6%
5
2.48/5 - 14 votes
μ 2.53, σa 2.62 [?]

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.

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...

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...

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_INPUT Windows 98/Me, Windows 2000/XP: Process mouse and keyboard messages.
PM_QS_PAINT Windows 98/Me, Windows 2000/XP: Process paint messages.
PM_QS_POSTMESSAGE Windows 98/Me, Windows 2000/XP: Process all posted messages, including timers and hotkeys.
PM_QS_SENDMESSAGE Windows 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)

About the Author

Nibu babu thomas

Software Developer
Microsoft
India India

Member

Follow on Twitter Follow on Twitter
Nibu has been programming for 6 years now. He loves programming. Not a geek but trying to be one.
 
Nibu loves playing guitar and piano in his spare time.
Nibu loves his family a lot and loves to spent time with them. He is married to a wonderful woman. Smile | :)
 
Nibu is passionate about leading a good christian life. He dedicates all his success to Jesus. In fact this is what he says "I am whatever I am by the grace of Jesus" Smile | :) .
 
He loves CodeProject.
 
He has got a programming tips and tricks site. You could help him improve with your suggestions.
 
Please visit bits and bytes. Your feedback is very important.
 
I brag here -> Random thoughts.
 
What I post on CodeProject or other forums has nothing to do with Microsoft or any of the companies that I work/worked for. These are my own ideas/thoughts unless otherwise explicitly mentioned.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberupasanabhatia19:25 29 Nov '10  
QuestionMessage 0x00AE in XP PinmemberAjay L D4:31 21 Nov '06  
AnswerRe: Message 0x00AE in XP PinmemberNibu babu thomas20:38 23 Nov '06  
GeneralRe: Message 0x00AE in XP Pinmemberhelloworld22229:36 12 May '07  
GeneralRe: Message 0x00AE in XP PinmemberNibu babu thomas18:22 13 May '07  
AnswerRe: Message 0x00AE in XP PinmemberRomualdas Cukuras3:01 24 May '07  
0xAE WM_NCUAHDRAWCAPTION
0xAF WM_NCUAHDRAWFRAME
 
Thats the secret shield of windows xp when you are using themes Smile | :) Good luck. Romas

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 12 Oct 2006
Article Copyright 2006 by Nibu babu thomas
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid