Microsoft KB Archive/149407

= HOWTO: How to Display a Bitmap in a CStatusBar Pane =

Article ID: 149407

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q149407

<div class="summary_section">

SUMMARY
The standard status bar in an MFC application easily allows the application to display its status. Sometimes it is a good idea to display the status using bitmaps or other graphic entities. MFC's CStatusBar class directly supports displaying text, but not graphics, in its panes. This article shows by example how to display a bitmap in a CStatusBar pane.

<div class="moreinformation_section">

MORE INFORMATION
It is not very difficult to display a bitmap in a status bar pane. The code at the end of this article will let you display a bitmap in pane 1 of the status bar. Put the code in a Default AppWizard application, and add a new pane to the status bar before the standard panes. Please refer to online documentation for CStatusBar for information on how to add a pane to the status bar.

With Visual C++ 4.0 and later, MFC's CStatusBar uses the status window common control. In earlier versions, CStatusBar was implemented in MFC. Hence the code required for displaying bitmap in a CStatusBar pane is different.

You can add the sample code to an application built with an earlier version of Visual C++ (a version previous to 4.0), and then recompile the application using Visual C++ 4.0 without modifying the code. If you are using Visual C++ 4.0 or later, you need not add code between the #if _MFC_VER < 0x400 ...  #endif directives.

Step-by-Step Example and Sample Code
<ol> Derive a class from CStatusBar (for example, CMyStatusBar), and use this class instead of CStatusBar in CMainFrame.</li>  Add the following member functions to the class definition in the .h file: class CMyStatusBar : public CStatusBar {  ...   #if _MFC_VER < 0x400 virtual void DoPaint(CDC* pDC); #endif

#if _MFC_VER >= 0x400 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); #endif ...  } </li>  Add the following code for the functions in the .cpp file: #if _MFC_VER < 0x400 void CMyStatusBar::DoPaint(CDC* pDC) {      CRect rect; GetItemRect(1, &rect); // get pane rect pDC->ExcludeClipRect(&rect); // exclude pane rect from paint CStatusBar::DoPaint(pDC); // paint remainder of status bar

CRgn paneRgn; paneRgn.CreateRectRgnIndirect(rect); pDC->SelectClipRgn(&paneRgn); // set clip region to pane rect

CBitmap* pBitmap = /* pointer to current CBitmap */; CDC srcDC; // select current bitmap into a compatible CDC srcDC.CreateCompatibleDC(NULL); CBitmap* pOldBitmap = srcDC.SelectObject(pBitmap); pDC->BitBlt(rect.left, rect.top, rect.Width, rect.Height,                &srcDC, 0, 0, SRCCOPY); // BitBlt to pane rect srcDC.SelectObject(pOldBitmap); }  #endif

#if _MFC_VER >= 0x400 void CMyStatusBar::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {      switch(lpDrawItemStruct->itemID) {      case 1: // Attach to a CDC object CDC dc; dc.Attach(lpDrawItemStruct->hDC);

// Get the pane rectangle and calculate text coordinates CRect rect(&lpDrawItemStruct->rcItem);

CBitmap* pBitmap = /* pointer to current CBitmap */; CDC srcDC; // select current bitmap into a compatible CDC srcDC.CreateCompatibleDC(NULL); CBitmap* pOldBitmap = srcDC.SelectObject(pBitmap); dc.BitBlt(rect.left, rect.top, rect.Width, rect.Height,                    &srcDC, 0, 0, SRCCOPY); // BitBlt to pane rect srcDC.SelectObject(pOldBitmap);

// Detach from the CDC object, otherwise the hDC will be          // destroyed when the CDC object goes out of scope dc.Detach;

return; }

CStatusBar::DrawItem(lpDrawItemStruct); }  #endif </li>  Add the following code to CMainFrame::OnCreate. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {  ...       if (!m_wndStatusBar.Create(this) ||           !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)))

TRACE0("Failed to create status bar\n"); return -1;     // fail to create }

// Add the following code #if _MFC_VER >= 0x400 UINT nID, nStyle; int cxWidth;

m_wndStatusBar.GetPaneInfo(1, nID, nStyle, cxWidth); m_wndStatusBar.SetPaneInfo(1, nID, nStyle | SBT_OWNERDRAW,          cxWidth); #endif ...  }                        </li></ol>

<div class="references_section">