5,545,925 members and growing! (16,428 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Some tricks with Peek Message

By Nibu babu thomas

Some interesting things that you can do with Peek Message
C++, Windows, Visual Studio, Dev

Posted: 12 Oct 2006
Updated: 12 Oct 2006
Views: 11,888
Bookmarked: 7 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 2.57 Rating: 2.31 out of 5
3 votes, 23.1%
1
5 votes, 38.5%
2
1 vote, 7.7%
3
2 votes, 15.4%
4
2 votes, 15.4%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Well 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 DoEvent function.

Well I am in the midst of developing an interesting personal project,l 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 the time when I did start thinking about DoEvent equivalent in VC++.

DoEvent code

Well now enough of preaching :). Let's get back 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 );
    }
}

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 Keyboard and Mouse Input buffer

Well this was about DoEvents. Now how about flushing keyboard input buffer and 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 ))
      ;
}


Doing Idle time processing using PeekMessage

Conclusion

These are in fact a few of the ideas that I got while programming. Hope this helps. There are some other flags that you can pass to PeekMessage for eg:

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.


On how to do Idle time processing using PeekMessage search MSDN with OnIdle keyword, you will bump into pretty interesting topics.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nibu babu thomas


Microsoft MVP for Visual C++.
Nibu has been programming for 5 years now. He loves programming. Not a geek but trying to be one.

Nibu loves playing guitar and piano in his spare time (Right now terribly short on practice Frown).

Nibu loves his mom a lot. In fact has been spoiled by her as his father says Wink. He loves to spent time with his family.

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 Tips and Tricks
Occupation: Software Developer (Senior)
Location: India India

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionMessage 0x00AE in XPmemberAjay L D4:31 21 Nov '06  
AnswerRe: Message 0x00AE in XPmemberNibu babu thomas20:38 23 Nov '06  
GeneralRe: Message 0x00AE in XPmemberhelloworld22229:36 12 May '07  
GeneralRe: Message 0x00AE in XPmemberNibu babu thomas18:22 13 May '07  
AnswerRe: Message 0x00AE in XPmemberRomualdas Cukuras3:01 24 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Oct 2006
Editor:
Copyright 2006 by Nibu babu thomas
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project