CWinThread::Run()的代码如下:
// main running routine until threadexits
intCWinThread::Run()
{
}
BOOLCWinThread:: PumpMessage()
{
ASSERT_VALID(this);
if(!::GetMessage(&m_msgCur, NULL, NULL,NULL))
returnFALSE;
// process thismessage
if (m_msgCur.message != WM_KICKIDLE&&!PreTranslateMessage(&m_msgCur))
{
::TranslateMessage(&m_msgCur);
::DispatchMessage(&m_msgCur);
}
returnTRUE;
}
看了上面的代码,你应该已经明白OnIdle的工作流程。
这里需要注意lIdleCount 这个变量,每当有消息被处理后, lIdleCount的值就会恢复为0.
CWinApp::OnIdle(0); 也就是 当 lIdleCount 的值为0 的时候,会导致 CDocument::OnIdle() 函数被调用;当时当 lIdleCount 值不为0的时候,就不会调用CDocument::OnIdle()。具体的调用可以看调用堆栈,这里只给出结果。
方法一:
对于MFC程序来讲,很多是采用MFC的文档视图类的框架。比如如果你要让视图不断刷新,在这个不断刷新的视图中可以完成场景渲洒更新等操作。你当然可以在 YourMethod()中获取视图的pView的指针,然后调用其内的接口函数, 就像这样:
CMainFrame *parent = (CMainFrame *)AfxGetMainWnd();
if ( parent && parent->GetSafeHwnd() )
{
CFrameWnd* pFrame = parent->GetActiveFrame();
CView *pView = pFrame->GetActiveView();
if ( pView )
{
pView->Invalidate();
}
}
方法二:
可以直接调用 CWinApp::OnIdle(0); 最后会调用到CDocument::OnIdle() 函数,代码可以参照下面:
POSITION pos = GetFirstViewPosition();
while ( pos != NULL )
{
CView* pView = GetNextView( pos );
pView->Invalidate();
pView->UpdateWindow();
}