Article ID: 149407
Article Last Modified on 11/21/2006
#if _MFC_VER < 0x400 ... #endifdirectives.
class CMyStatusBar : public CStatusBar
{
...
#if _MFC_VER < 0x400
virtual void DoPaint(CDC* pDC);
#endif
#if _MFC_VER >= 0x400
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
#endif
...
}
#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
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
...
}
Additional query words: kbinf 1.00 1.50 1.51 1.52 2.00 2.10 2.20 2.50 2.51 2.52 3.00 3.10 3.20 4.00 4.10 alwaysupdate dskbsweep
Keywords: kbhowto kbstatbar kbuidesign kbbitmap kbmfcctrlbar KB149407