我遇到了CFrameWnd的OnPaint方法问题,我似乎无法弄清楚发生了什么。大约每10毫秒调用一次OnPaint,这会导致计算机冻结。检查CPU使用率和这个应用程序占用50%!OnPaint更新太频繁
该应用程序是一个非常简单的MFC应用程序,它写在一个文件中。
// Includes are done here...
class MFC_Tutorial_Window : public CFrameWnd
{
std::string data;
public:
MFC_Tutorial_Window()
{
this->data = "";
Create(NULL, "Data Win"); // Create window
}
void OnPaint()
{
CDC* pDC = GetDC();
CString s = CString(this->data.c_str());
RECT rc;
HWND hwnd = this->m_hWnd;
if(hwnd != NULL) {
::GetWindowRect(hwnd, &rc);
rc.top = rc.bottom/2;
if(pDC != NULL && pDC->m_hDC != NULL) {
pDC->DrawText(s, &rc, DT_CENTER);
}
}
}
void UpdateWithNewData(std::string up) {
this->data = up;
Invalidate();
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(MFC_Tutorial_Window, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
// App class
class MyApp :public CWinApp
{
MFC_Tutorial_Window *wnd;
BOOL InitInstance()
{
wnd = new MFC_Tutorial_Window();
m_pMainWnd = wnd;
m_pMainWnd->ShowWindow(3);
wnd->UpdateWithNewData("Hello world!");
return 1;
}
};
有谁知道为什么OnPaint被系统垃圾邮件?一直盯着这个代码很长一段时间,我无法找到它。
请注意,您几乎总是希望使用View类,在这种情况下,您可以在OnDraw中进行绘制,并且框架负责处理导致当前问题的细节。 –