问题
I have a very simple Win32 application that uses CAtlExeModuleT. The module simply creates a class CTestWindow derived from CWindowImpl. It just has a single message handler for WM_PAINT. After I create the window and display it, the OnPaint method (WM_PAINT message) is called infinitely and there by consumes 100% CPU.
The code that creates the window is very simple:
m_pMainWnd = new CTestWindow();
if(NULL == m_pMainWnd->Create(NULL, CWindow::rcDefault, _T("Test Window"), WS_OVERLAPPEDWINDOW, 0, hMenu)){
DWORD dwErr = GetLastError();
return E_FAIL;
}
m_pMainWnd->ShowWindow(nShowCmd);
The OnPaint message handler is very simple as well (it doesn't do anything):
LRESULT CTestWindow::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// TODO: Add your message handler code here and/or call default
return 0;
}
回答1:
My guess is that you are not validating the window in your paint handler.
An application must call BeginPaint and EndPaint in response to WM_PAINT messages, or pass the message to the DefWindowProc function to validate the window. DefWindowProc validates the update region; it can send the WM_ERASEBKGND message if the window background needs to be erased.
This would mean the OS will think the window still needs to be painted, and call you again.
来源:https://stackoverflow.com/questions/1117511/never-ending-wm-paint-loop-with-atl-cwindowimpl