框架视图中添加背景图片的方法。 在关闭所有视图后能看到背景图片。
1.在MDIClient.h文件中加入
public:
BOOL SetBitmap(UINT nID);
COLORREF SetBackColor(COLORREF nBackColor);
Private:
CBitmap* m_pBmp;
COLORREF m_nBackColor;
添加消息名
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnSize(UINT nType, int cx, int cy);
2.在MDIClient.cpp文件中加入
构造函数中添加
m_pBmp = NULL;
m_nBackColor = CLR_INVALID;
析构函数中添加
if (m_pBmp)
{
m_pBmp->DeleteObject();
delete m_pBmp;
m_pBmp = NULL;
}
添加消息映射
ON_WM_ERASEBKGND()
ON_WM_SIZE()
添加下述函数体
BOOL CMDIClient::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(&rect);
if (m_nBackColor != CLR_INVALID)
pDC->FillSolidRect(&rect, m_nBackColor);
else
if (m_pBmp != NULL)
{
BITMAP bm;
CDC dcMem;
VERIFY(m_pBmp->GetObject(sizeof(BITMAP), (LPVOID)&bm));
dcMem.CreateCompatibleDC(NULL);
CBitmap* pOldBitmap = dcMem.SelectObject(m_pBmp);
for (register int nX = 0; nX < rect.Width(); nX += bm.bmWidth)
for (register int nY = 0; nY < rect.Height(); nY += bm.bmHeight)
pDC->BitBlt(nX, nY, bm.bmWidth, bm.bmHeight, &dcMem, 0, 0, SRCCOPY);
dcMem.SelectObject(pOldBitmap);
}
else
CWnd::OnEraseBkgnd(pDC);
return TRUE;
}
COLORREF CMDIClient::SetBackColor(COLORREF nBackColor)
{
LockWindowUpdate();
COLORREF cr = m_nBackColor;
m_nBackColor = nBackColor;
UnlockWindowUpdate();
return cr;
}
BOOL CMDIClient::SetBitmap(UINT nID)
{
BOOL bResult = true;
LockWindowUpdate();
if (m_pBmp)
{
m_pBmp->DeleteObject();
delete m_pBmp;
m_pBmp = NULL;
}
m_pBmp = new CBitmap();
bResult = m_pBmp->LoadBitmap(nID);
UnlockWindowUpdate();
return bResult;
}
void CMDIClient::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
if (m_pBmp != NULL)
Invalidate();
}
3.在MainFrm.h中添加
public:
CMDIClient m_wndMdiClient;
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
4.在MainFrm.cpp中
OnCreate()函数中注释
// VERIFY(m_MDIClient.SubclassMDIClient(this));
CChildFrame* CMainFrame::SetActiveView(CChildFrame* pChildFrame, CRuntimeClass* pViewClass, CString strItemName )注释
// m_MDIClient.AddChildFrame((CMDIChildWnd*)pChildFrame);
// OverLoad
CChildFrame* CMainFrame::SetActiveView(CChildFrame* pChildFrame, CRuntimeClass* pViewClass )中注释
// m_MDIClient.AddChildFrame((CMDIChildWnd*)pChildFrame);
添加如下函数体
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
if (CMDIFrameWnd::OnCreateClient(lpcs, pContext))
{
m_wndMdiClient.SubclassWindow(m_hWndMDIClient);
m_wndMdiClient.SetBitmap(IDB_STONE);
return TRUE;
}
else
return FALSE;
}
5.在ChildFrm.cpp
void CChildFrame::OnDestroy()中注释
// thisFrame->m_MDIClient.RemoveChildFrame(pChild);
// thisFrame->FreeFramePtr((CChildFrame *)pChild);
6.在工程中新增一位图资源IDB_STONE