应用Wizard生成的MDI程序中Frame的背景是黑色的,本文将讨论如何更改该背景。需要之处的是Frame的客户区并不是由MainFrame维护的,其背景窗口的句柄为m_hWndMDIClient。故而更改背景的思路是将该句柄指向我们自行设计的窗口类。
首先新建一个窗口类,继承于CWnd,然后重写OnEreaseBkgnd()函数。
class CFmBk : public CWnd
{
// Construction
public:
CFmBk();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFmBk)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CFmBk();
// Generated message map functions
protected:
//{{AFX_MSG(CFmBk)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#include "stdafx.h"
#include "ChgFrmBk.h"
#include "FmBk.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CFmBk
CFmBk::CFmBk()
{
}
CFmBk::~CFmBk()
{
}
BEGIN_MESSAGE_MAP(CFmBk, CWnd)
//{{AFX_MSG_MAP(CFmBk)
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CFmBk message handlers
BOOL CFmBk::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CRect rt;
GetClientRect(&rt);
pDC->FillSolidRect(&rt,RGB(255,0,0));//设置背景为红色
return true;
}
准备该类后,在CMainFrame中重写OnCreateClient()函数:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
if(CMDIFrameWnd::OnCreateClient(lpcs,pContext))
{
m_FmClient.SubclassWindow(m_hWndMDIClient);
return TRUE;
}
return FALSE;
}
完成!