目前正在做一个项目,窗口被分成若干个区域,不同的区域有不同的背景色,在OnPaint()中实现,代码如下:
m_brush1.CreateSolidBrush(RGB(195, 176, 146));
m_brush2.CreateSolidBrush(RGB(29, 15, 14));
m_brush3.CreateSolidBrush(RGB(29, 15, 4));
m_brush4.CreateSolidBrush(RGB(118, 94, 82));
m_brush5.CreateSolidBrush(RGB(255, 255, 82));
以上画刷在OnInitialDialg中创建
void CMyDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CDialog::OnPaint() for painting messages
CRect clientRect;
GetClientRect(&clientRect);
CRect temp;
temp.left = clientRect.left;
temp.top = clientRect.top;
temp.right = clientRect.right / 3 *2;
temp.bottom = clientRect.Height() / 14 * 2;
dc.FillRect(&temp, &m_brush1);
temp.top = temp.bottom;
temp.bottom = temp.top + clientRect.Height() / 14 * 3;
dc.FillRect(&temp, &m_brush2);
temp.top = temp.bottom;
temp.bottom = g_pstuInterIO->rcButton.top;
dc.FillRect(&temp, &m_brush3);
temp.left = temp.right;
temp.top = clientRect.top;
temp.right = clientRect.right;
dc.FillRect(&temp, &m_brush4);
CRect wndrect;
if (m_handwheel_sliderctrl.m_hWnd)
{
m_handwheel_sliderctrl.GetWindowRect(&wndrect);
ScreenToClient(&wndrect);
temp.top = wndrect.bottom + GAP_V;
temp.bottom = temp.top + 1;
dc.FillRect(&temp, &m_brush5);
}
if (m_feedoverride_sliderctrl.m_hWnd)
{
m_feedoverride_sliderctrl.GetWindowRect(&wndrect);
ScreenToClient(&wndrect);
temp.top = wndrect.bottom + GAP_V;
temp.bottom = temp.top + 1;
dc.FillRect(&temp, &m_brush5);
}
if (m_static_fprog.m_hWnd)
{
m_static_fprog.GetWindowRect(&wndrect);
ScreenToClient(&wndrect);
temp.top = wndrect.bottom + GAP_V;
temp.bottom = temp.top + 1;
dc.FillRect(&temp, &m_brush5);
}
dc.FillSolidRect(g_pstuInterIO->rcButton, COLOR_BACKBUTTON);
}
但是当界面中的其他static_text控件需要更新文本内容时,采用如下方式
m_static.setwindowtext(_T("OK"));m_static.invalidate();
原先的内容不能被擦除,当采用Invalidate对整个窗口重新绘制时,窗口闪烁比较明显。
后来采用如下方式只对该控件进行重画,避免了窗口的闪烁:
CRect rectUpdate;
m_static.GetWindowRect(&rectUpdate);//获取该控件对应的屏幕坐标
ScreenToClient(&rectUpdate);//将屏幕坐标转换成窗口的客户坐标
RedrawWindow(&rectUpdate);//只标记该区域更行,避免整个客户区刷新。