![]() |
Desktop Development »
Dialogs and Windows »
Message Handling
Intermediate
OnIdle() implementation for CDialog based app´sBy Emilio GuijarroSimple implementation for the OnIdle function in the CDialog derived classes |
VC6Win2K, WinXP, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
A very common and powerful programming tool for MFC applications is the idle time processing,
which allows the programmer to be noticed when there aren't pending messages for the current
thread. I was surprised when coding a dialog based application I found that this wonderful
advantage was not available, due to the synchronous nature of the DoModal() method. Now the target
is implementing the same functionality on a CDialog derived class.
The code I made to solve this problem is really simple, it uses the GetQueueStatus function
to check the message queue state. If there are not pending messages, it posts the
WM_IDLE
message to the current window, allowing it to call OnIdle(). Of course, all this code is
implemented in an overridden version of WindowProc().
Emulating the behaviour of the lCount parameter (used to calculate how many idle time has
passed since the last false return of OnIdle()) is self-explained in the following source code.
LRESULT CMyDialog::WindowProc(UINT message,
WPARAM wParam, LPARAM lParam)
{
DWORD QueueStatus;
LRESULT resValue = 0;
bool OnIdleRetVal = true;
if(message == WM_IDLE) {
OnIdleRetVal = OnIdle((UINT)wParam);
if(!OnIdleRetVal)
wParam = 0;
} else
resValue = CDialog::WindowProc(message,
wParam, lParam);
QueueStatus = GetQueueStatus(QS_ALLINPUT);
if(HIWORD(QueueStatus) == 0)
PostMessage(WM_IDLE,
wParam + (OnIdleRetVal ? 1 : 0), 0);
return resValue;
}
Of course, OnIdle() should be declared and implemented in the class. The working mechanism,
tips and tricks of the OnIdle function are explained on the CWinApp documentation part of the
MSDN Library, so they will be not explained here for saving time and bytes ;).
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Jul 2002 Editor: Chris Maunder |
Copyright 2002 by Emilio Guijarro Everything else Copyright © CodeProject, 1999-2010 Web19 | Advertise on the Code Project |