您的位置:首页 > 其它

VC多文档MFC程序的背景色改变

2010-12-06 17:09 441 查看
VC 建立多文档工程,CMainFrame主框架设置了背景色,但是当打开一个新文档非最大化状态时,鼠标点住拖动  主框架的背景色不能时时刷新,请问这个问题怎么解决? 很急很急,十万火急
下面贴出代码:
1./*截获MDI客户窗口WM_PAINT消息,在这个函数中向主框架窗口发送WM_PAINT/消息,
在该消息的处理函数中实现彩色位图的显示或者设置背景色*/
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if ( pMsg->hwnd == m_hWndMDIClient && pMsg->message == WM_PAINT )
{
Invalidate() ;

}
return CMDIFrameWnd::PreTranslateMessage(pMsg);
}

2./*设置主框架背景色或者贴上一张位图*/
void CMainFrame::OnPaint()
{
// CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
CDC dc;
dc.m_hDC = ::GetDC( this->m_hWndMDIClient );
dc.SetBkMode( TRANSPARENT );
CRect clientRect;
GetClientRect( &clientRect );
dc.FillSolidRect( &clientRect, RGB(100,100,255) );
dc.DeleteDC();

CMDIFrameWnd::OnPaint();
// Do not call CMDIFrameWnd::OnPaint() for painting messages
}

————以上为转载

综上可以完成但存在一些问题:当移动子文档窗口的时候会出现背景不及时重绘,解决方法 在CChildFrame类中假如CChildFrame::OnMove(int x, int y) 函数

void CChildFrame::OnMove(int x, int y)
{
CMDIChildWnd::OnMove(x, y);

// TODO: Add your message handler code here


CWnd * pwnd =((CBKGroundApp *)AfxGetApp())->m_pMainWnd;
((CMainFrame*)pwnd)->OnPaint();

}

CMainFrame中的OnPaint()函数需要变为public;

要使得打开时背景色就变成想要的 就得在InitInstance()函数中加上

pMainFrame->Invalidate();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: