更改MDI中Frame的背景

来源:互联网 发布:完美解码mac版 编辑:程序博客网 时间:2024/05/16 15:11

应用Wizard生成的MDI程序中Frame的背景是黑色的,本文将讨论如何更改该背景。需要之处的是Frame的客户区并不是由MainFrame维护的,其背景窗口的句柄为m_hWndMDIClient。故而更改背景的思路是将该句柄指向我们自行设计的窗口类。

首先新建一个窗口类,继承于CWnd,然后重写OnEreaseBkgnd()函数。

class CFmBk : public CWnd{// Constructionpublic:CFmBk();// Attributespublic:// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CFmBk)//}}AFX_VIRTUAL// Implementationpublic:virtual ~CFmBk();// Generated message map functionsprotected://{{AFX_MSG(CFmBk)afx_msg BOOL OnEraseBkgnd(CDC* pDC);//}}AFX_MSGDECLARE_MESSAGE_MAP()};


#include "stdafx.h"#include "ChgFrmBk.h"#include "FmBk.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CFmBkCFmBk::CFmBk(){}CFmBk::~CFmBk(){}BEGIN_MESSAGE_MAP(CFmBk, CWnd)//{{AFX_MSG_MAP(CFmBk)ON_WM_ERASEBKGND()//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CFmBk message handlersBOOL CFmBk::OnEraseBkgnd(CDC* pDC) {// TODO: Add your message handler code here and/or call defaultCRect 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 classif(CMDIFrameWnd::OnCreateClient(lpcs,pContext)){m_FmClient.SubclassWindow(m_hWndMDIClient);return TRUE;}return FALSE;}

完成!


原创粉丝点击