void CTestHfDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CClientDC dc(this);
if(!FilePathName.IsEmpty())
{
m_Pic.UpdateSizeOnDC(&dc);
CRect rect;
GetDlgItem(IDC_SHOWPIC)->GetWindowRect(&rect);
ScreenToClient(&rect);
m_Pic.Load(FilePathName);
m_Pic.Show(&dc, &rect);
}
// CDialog::OnPaint();
UpdateWindow();
}
}
最后的UpdateWindow();是我自己写的,但是运行程序以后,程序窗口很卡,是什么原因?
------解决方案--------------------
如果窗口更新的区域不为空,UpdateWindow函数通过发送一个WM_PAINT消息来更新指定窗口的客户区。函数绕过应用程序的消息队列,直接发送WM_PAINT消息给指定窗口的窗口过程,如果更新区域为空,则不发送消息。
可能一直调用了OnPaint()
------解决方案--------------------
UpdateWindow(); 为什么要用UpdateWindow();这个,用默认的不行吗
------解决方案--------------------
- C/C++ code
CClientDC dc(this); // 用CPaintDC代替CClientDC,否则永远无法清除无效区域,下面的UpdateWindow()函数就造成不断刷新形成界面卡顿 UpdateWindow(); // 这一句在使用CPaintDC替换之后,就不会再起作用了,可以删除,而且在OnPaint函数内调用UpdateWindow是错误的做法。