Click here to Skip to main content
13,085,525 members (60,900 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 12 Oct 2006

Some Tricks with Peek Message

, 12 Oct 2006
Rate this:
Please Sign up or sign in to vote.
Some interesting things that you can do with PeekMessage.


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


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.


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
My Technical Blog

I am a computer programmer who started his career as a Java programmer in 2000. Wrote a game and several applications like editors, DB apps etc in Java. My fascination for programming went on a high in the following years. I did a post graduate diploma of 2 years because of which I got introduced to a plethora of Languages. My favorite at that time was VB because it was way too easy to program, intelli-sense was too cool.

I joined an NGO in 2002 as part time computer programmer. Primary responsibility was to develop their website and to write apps for them in VB. During this process I got introduced to DHTML, CSS, HTML, JavaScript, wow had hell of a time. Learned and learned and learned during this time. I got into Visual C++ because of my post graduation in computer applications. I was forced into this powerful language but how lucky I am. Initially I was scared of CreateFont API Wink | ;) but now its a piece of cake. Smile | :)

As of now I'm working as Visual C++ engineer with Microsoft. My passion for this language never ends. Its the raw power of the language, the kind of performance and flexibility it provides, that keeps me motivated to continue working in this language. Started working in VC6 and all through till the latest version of Visual C++. Smile | :)

I'm part of the Microsoft Developer Support - Programming Languages Team. Enjoying every day.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
upasanabhatia29-Nov-10 18:25
memberupasanabhatia29-Nov-10 18:25 
QuestionMessage 0x00AE in XP Pin
Ajay L D21-Nov-06 3:31
memberAjay L D21-Nov-06 3:31 
AnswerRe: Message 0x00AE in XP Pin
Nibu babu thomas23-Nov-06 19:38
memberNibu babu thomas23-Nov-06 19:38 
GeneralRe: Message 0x00AE in XP Pin
helloworld222212-May-07 8:36
memberhelloworld222212-May-07 8:36 
GeneralRe: Message 0x00AE in XP Pin
Nibu babu thomas13-May-07 17:22
memberNibu babu thomas13-May-07 17:22 
AnswerRe: Message 0x00AE in XP Pin
Romualdas Cukuras24-May-07 2:01
memberRomualdas 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.

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