void CMainFrame::OnPaint() { CMDIFrameWnd::OnPaint(); CRect rc; CDC dc; dc.m_hDC=::GetDC(this->m_hWndMDIClient); CBrush br(RGB(189,213,217)); dc.SelectObject(&br); dc.SetTextAlign(TA_CENTER); dc.SetBkMode(TRANSPARENT); dc.SetTextColor(RGB(0,0,255)); GetClientRect(&rc); dc.PatBlt(rc.left,rc.top,rc.Width(),rc.Height(),PATCOPY); CString str; str="I am songyewen from [email protected]!"; dc.TextOut(rc.CenterPoint().x,rc.CenterPoint().y,str,str.GetLength()); ReleaseDC(&dc); // TODO: Add your message handler code here
// Do not call CMDIFrameWnd::OnPaint() for painting messages } ---------------------------------------- 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) this->PostMessage(WM_PAINT); return CMDIFrameWnd::PreTranslateMessage(pMsg); } --------------------------------------- BOOL CYourApp::InitInstance() { ... // The main window has been initialized, so show and update it. pMainFrame->ShowWindow(SW_SHOW); //---------------------------------------------- pMainFrame->PostMessage(WM_PAINT);//Update the mdi client color! //---------------------------------------------- pMainFrame->UpdateWindow(); }
sorry! 最后应该为: BOOL CYourApp::InitInstance() { ... // The main window has been initialized, so show and update it. pMainFrame->ShowWindow(SW_SHOW); //---------------------------------------------- pMainFrame->PostMessage(WM_PAINT);//Update the mdi client color! //---------------------------------------------- pMainFrame->UpdateWindow(); return true; }
#if !defined(AFX_VNMDICLIENT_H__FC9EC8F2_8175_11D1_A16B_000000000000__INCLUDED_)
#define AFX_VNMDICLIENT_H__FC9EC8F2_8175_11D1_A16B_000000000000__INCLUDED_#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000class CMdiClient : public CWnd
{
DECLARE_DYNCREATE(CMdiClient)// Construction
public:
CMdiClient();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMdiClient)
//}}AFX_VIRTUAL// Implementation
public:
BOOL SetBitmap(LPCTSTR lpszresourcename);
COLORREF SetBackColor(COLORREF nBackColor);
virtual ~CMdiClient(); // Generated message map functions
protected:
//{{AFX_MSG(CMdiClient)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnSize(UINT nType, int cx, int cy);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()private:
HBITMAP m_pBmp;
COLORREF m_nBackColor;
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.#endif // !defined(AFX_VNMDICLIENT_H__FC9EC8F2_8175_11D1_A16B_000000000000__INCLUDED_)
#include "stdafx.h"
#include "MdiClient.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifCMdiClient::CMdiClient()
{
m_pBmp = NULL;
m_nBackColor = CLR_INVALID;
}CMdiClient::~CMdiClient()
{
if (m_pBmp)
{
m_pBmp = NULL;
}
}IMPLEMENT_DYNCREATE(CMdiClient, CWnd)BEGIN_MESSAGE_MAP(CMdiClient, CWnd)
//{{AFX_MSG_MAP(CMdiClient)
ON_WM_ERASEBKGND()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
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;
::GetObject(m_pBmp,sizeof(BITMAP), (LPVOID)&bm);
dcMem.CreateCompatibleDC(NULL);
HBITMAP pOldBitmap =(HBITMAP ) 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->StretchBlt(rect.left ,rect.top , rect.Width() ,rect.Height(), &dcMem, 0, 0,bm.bmWidth-1,bm.bmHeight-1, 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(LPCTSTR lpszresourcename)
{
BOOL bResult = true; LockWindowUpdate(); //if (m_pBmp)
//{
// m_pBmp = NULL;
//} m_pBmp =(HBITMAP)LoadImage(AfxGetInstanceHandle(),lpszresourcename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION); UnlockWindowUpdate(); return true;
}void CMdiClient::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
if (m_pBmp != NULL)
Invalidate();
}在class CMainFrame : public CMDIFrameWnd中加入
private:
CMdiClient m_wndMdiClient;
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
if (CMDIFrameWnd::OnCreateClient(lpcs, pContext))
{
m_wndMdiClient.SubclassWindow(m_hWndMDIClient);
m_wndMdiClient.SetBitmap("1.bmp");
return TRUE;
}
else
return FALSE;
}
{
CMDIFrameWnd::OnPaint();
CRect rc;
CDC dc;
dc.m_hDC=::GetDC(this->m_hWndMDIClient);
CBrush br(RGB(189,213,217));
dc.SelectObject(&br);
dc.SetTextAlign(TA_CENTER);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(0,0,255));
GetClientRect(&rc);
dc.PatBlt(rc.left,rc.top,rc.Width(),rc.Height(),PATCOPY);
CString str;
str="I am songyewen from [email protected]!";
dc.TextOut(rc.CenterPoint().x,rc.CenterPoint().y,str,str.GetLength());
ReleaseDC(&dc); // TODO: Add your message handler code here
// Do not call CMDIFrameWnd::OnPaint() for painting messages
}
----------------------------------------
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)
this->PostMessage(WM_PAINT);
return CMDIFrameWnd::PreTranslateMessage(pMsg);
}
---------------------------------------
BOOL CYourApp::InitInstance()
{
... // The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(SW_SHOW);
//----------------------------------------------
pMainFrame->PostMessage(WM_PAINT);//Update the mdi client color!
//----------------------------------------------
pMainFrame->UpdateWindow();
}
最后应该为:
BOOL CYourApp::InitInstance()
{
... // The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(SW_SHOW);
//----------------------------------------------
pMainFrame->PostMessage(WM_PAINT);//Update the mdi client color!
//----------------------------------------------
pMainFrame->UpdateWindow();
return true;
}
1、在App的InitInstance()函数中
HWND hmain = pMainFrame->GetWindow(GWL_CHILD)->GetSasfeHwnd();
pfnOldWndProc = (WINPROC)GetWindowLong(hmain,GWL_WINPROC);
SetWindowLong(hmain,GWL_WINPROC,pfnNewWinProc);2、定义全局变量和回调函数
WNDPROC pfnOldWinProc; LRESULT CALLBACK pfnNewWinProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg){
case WM_SIZE:
SendMessage(hwnd,WM_EARSEBKGD,(WPARAM)GetDC(hwnd),0);
return 1;
case WM_EARSEBKGD:
创建内存DC;
装入bitmap;
使用BitBlt画图;
删除内存DC;
return 1;
default:
return CallWindowProc(pfnOldWinProc,hwnd,uMsg,wParam,lParam);
}
}