Search code examples
c++bitmapbackgroundmfcmdi

Bitmap as background to MDI mainfrm


I am trying to make a bitmap image the background for the mainframe before any other frames are loaded but I'm not having any luck, what am I missing?

BOOL CMainFrame::OnEraseBkgnd(CDC* pDC)
{
    CBitmap m_cbImage;
    COLORREF m_crBack;
    UINT m_uImage;
    m_crBack = RGB(255, 255, 255);
    m_uImage = IDB_SPLASH;

    if ((m_cbImage.m_hObject == NULL) && (m_uImage>0))
    {
        m_cbImage.LoadBitmap(m_uImage);
        m_bFirst = TRUE;
    }
    else
    {
        if (m_bFirst)
        {
            m_bFirst = FALSE;
        }
    }
    CRect rcClient;
    GetClientRect(rcClient);
    pDC->FillSolidRect(rcClient, m_crBack);
    if (m_cbImage.m_hObject != NULL)
    {
        BITMAP sBitmap;
        m_cbImage.GetBitmap(&sBitmap);
        CSize szImage(sBitmap.bmWidth, sBitmap.bmHeight);
        pDC->DrawState(CPoint(0, 0), szImage, &m_cbImage, DST_BITMAP | DSS_NORMAL);
    }
    return TRUE;

}

Solution

  • What looks like the background of the mainframe really isn't. The mainframe mostly just handles exactly that: the frame. The MDI client area is handled by a MDI Client window (which, in turn, manages the MDI child windows).

    To change the background drawing, you create a MDI Client window class (public derivative of CWnd), and handle the drawing in its OnEraseBkgnd. Create an instance of that class in your MainFrame class, and in your MainFrame's OnCreate, tell your MDI client to subclass the stock MDI client window:

    myClient.SubclassWindow(m_hWndMDIClient);