ON_WM_CTLCOLOR给对话框绘图,部分绘图很不好弄。 比如下面
HBRUSH CShortMsgDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CRect tempContrlRectUp=m_controArealRect;
CRect tempContrlRectDown=m_controArealRect;
tempContrlRectUp.bottom=BTN_POS_TOP;
m_pDC->FillSolidRect(&tempContrlRectUp,RGB(0xCA, 0xE1, 0xFF));
tempContrlRectDown.top=BTN_POS_TOP+ m_btnCount*BTN_WIDE;
m_pDC->FillSolidRect(&tempContrlRectDown,RGB(0xCA, 0xE1, 0xFF));
if(CTLCOLOR_EDIT==nCtlColor)
{
//pDC->SetBkMode(TRANSPARENT);
//pDC->SetBkColor(RGB(234,231,11));
}
hbr= ::CreateSolidBrush(RGB(245,245,245));
return hbr;
}
这个会出现 如不过返回画刷的,m_pDC->FillSolidRect(&tempContrlRectUp,RGB(0xCA, 0xE1, 0xFF)); 填充的部分被其dialog返回的画刷暂时的覆盖
的时候,会出现覆盖的现象。被hbr覆盖。 就算不被hbr覆盖也会被window本身的颜色覆盖。在CSDN看到在OnPaint中填充部dialog颜色
不会出现覆盖。
猜测是返回的画刷OnCtlColor消息先收到,然后才是OnPaint,如果单独
在OnPaint冲填充,也没有返回什么画刷。所以不会被暂时覆盖。下面是在OnPaint中填充对话框不同的颜色。不会发生遮盖。
如果你的工程是对话框, 且要画的地方正是主对话框的话, 直接定位到CXXXDlg的OnPaint函数,如下作处理.
C/C++ codevoid CXXXDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// 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 //***************************目前处理的是这里下的
{
CPaintDC dc(this);
CRect rcClient, rc1,rc2, rc3;
GetClientRect(rcClient); //rcClient是对话框客户区大小,将这个rcClient分成三个区域,画这三个区域就行了
rc1 = CRect(rcCLient.left, rcClient.top, rcClient.right, rcClient.top + rcClient.Height()/3 );
rc2 = CRect(rcClient.left, rc1.bottom, rcClient.right, rc1.bottom + rcClient.Height()/3 );
rc3 = CRect(rcClient.left, rc2.bottom, rcClient.right, rc2.bottom + rcClient.Height()/2 );
dc.FillSolidRect(rc1, RGB(255, 255, 255));
dc.FillSolidRect(rc2, RGB(0, 0, 255));
dc.FillSolidRect(rc3, RGB(0, 0, 0));
CDialog::OnPaint();
}
}
添加对话框 CDialog1的WM_PAINT消息。
C/C++ codevoid CDialog1::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rcClient, rc1,rc2, rc3;
GetClientRect(rcClient); //rcClient是对话框客户区大小,将这个rcClient分成三个区域,画这三个区域就行了
rc1 = CRect(rcClient.left, rcClient.top, rcClient.right, rcClient.top + rcClient.Height()/3 );
rc2 = CRect(rcClient.left, rc1.bottom, rcClient.right, rc1.bottom + rcClient.Height()/3 );
rc3 = CRect(rcClient.left, rc2.bottom, rcClient.right, rc2.bottom + rcClient.Height()/2 );
dc.FillSolidRect(rc1, RGB(255, 255, 255));
dc.FillSolidRect(rc2, RGB(0, 0, 255));
dc.FillSolidRect(rc3, RGB(0, 0, 0));
}