【框架-MFC】MFC MainFrame主客戶區新增背景圖片或顏色
阿新 • • 發佈:2019-01-07
解決方案
1、使用 virtual BOOL PreTranslateMessage(MSG* pMsg);截獲WM_PAINT訊息,並推送WM_PAINT訊息
2、在OnPaint事件中新增背景圖片或顏色
demo:
MainFrame.h
afx_msg void OnPaint();
virtual BOOL PreTranslateMessage(MSG* pMsg);
MainFrame.cpp BEGIN_MESSAGE_MAP(CMainFrame, CBCGPMDIFrameWnd) ON_WM_PAINT() END_MESSAGE_MAP() BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) { // TODO: 在此新增專用程式碼和/或呼叫基類 if(pMsg->hwnd==m_hWndMDIClient && pMsg->message==WM_PAINT) PostMessage(WM_PAINT); return CBCGPMDIFrameWnd::PreTranslateMessage(pMsg); } void CMainFrame::OnPaint() { // CPaintDC dc(this); // device context for painting CDC dc,memdc; dc.m_hDC=::GetDC(this->m_hWndMDIClient); CRect rect; CBitmap bitmap; BITMAP szbitmap; bitmap.LoadBitmap(IDB_BMP_START); bitmap.GetObject(sizeof(BITMAP),&szbitmap); CSize size(szbitmap.bmWidth,szbitmap.bmHeight); memdc.CreateCompatibleDC(&dc); CBitmap *oldbitmap=memdc.SelectObject(&bitmap); GetClientRect(&rect); StretchBlt(dc.m_hDC,0,0,rect.Width(),rect.Height(),memdc.m_hDC,0,0,size.cx,size.cy,SRCCOPY); memdc.SelectObject(oldbitmap); memdc.DeleteDC(); dc.DeleteDC(); CBCGPMDIFrameWnd::OnPaint(); }
resource.h
#define IDB_BMP_START 147
*.rc
/////////////////////////////////////////////////////////////////////////////
//
// Bitmap
//
IDB_BMP_START BITMAP "res\\圖片5.BMP"