MFC MDI: How to change the background of the MDI main frame?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: MFC MDI: How to change the background of the MDI main frame?

Threaded View

  1. #1
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    MFC MDI: How to change the background of the MDI main frame?

    Q: How can I change the background of the MDI main frame?

    A: In fact, on the main frame background lies another window of 'MDIClient' class. So do the following:


    • Using ClassWizard, add a generic CWnd-derived class, let's say it CMDIClientWnd


    • Add a member of type CMDIClientWnd to CMainFrame class

      Code:
      class CMainFrame : public CMDIFrameWnd
      {
      // ...
      // Attributes
      protected:
         CMDIClientWnd m_wndMDIClient;
      // ...
      };
    • In CMainFrame::OnCreate subclass the MDI client window

      Code:
      int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
      {
         if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
            return -1;
         // NOTE: m_hWndMDIClient is a public member of CMDIFrameWnd
         m_wndMDIClient.SubclassWindow(m_hWndMDIClient);
      // ...
      }
    • Add a member of type CBrush to class CMDIClientWnd

      Code:
      class CMDIClientWnd : public CWnd
      {
      // Attributes
      protected:
         CBrush  m_brush; // will be used to fill the backgound
      };
    • Create the brush in CMDIClientWnd constructor

      Code:
      CMDIClientWnd::CMDIClientWnd()
      {
         // this example uses a pattern brush filled from a bitmap resource
         CBitmap bitmap;
         bitmap.LoadBitmap(IDB_FISHING);
         m_brush.CreatePatternBrush(&bitmap);
      }
    • Finally, handle 'WM_ERASEBKGND'

      Code:
      BOOL CMDIClientWnd::OnEraseBkgnd(CDC* pDC) 
      {
         CBrush* pbrushOld = pDC->SelectObject(&m_brush);
         CRect rect;
         pDC->GetClipBox(&rect);
         pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
         pDC->SelectObject(pbrushOld);
      
         return TRUE;
      }


    Last edited by Andreas Masur; July 25th, 2005 at 04:07 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center