关于在对话框中增加状态栏的问题。
mazee 2001-07-25 09:44:49 我在对话框中增加了一个状态栏,但是在创建后总是要我重载CStatusBarCtrl::DrawItem函数,我不知道该如何重载它,我的代码大概如下
class CMyDlg : public CDialog
{
// Construction
public:
CMyDlg(CWnd* pParent = NULL); // standard constructor
CStatusBarCtrl m_StatBar;
...................................
......................................
}
// Microsoft Developer Studio generated include file.
#define IDM_ABOUTBOX 0x0010
#define IDC_STATUSBAR 32500
.....................................
...................................
......................................
在CMyDlg::OnInitDialog() 中增加如下代码
int nTotWide; // total width of status bar
CRect rect;
this->GetWindowRect(&rect);
rect.top = rect.bottom- 25;
m_bRvStatOk = m_StatBar.Create(WS_CHILD | WS_BORDER | WS_VISIBLE ,rect,this,
IDC_STATUSBAR);
if (m_bRvStatOk == NULL)
{
AfxMessageBox ("Status Bar not created!", NULL, MB_OK );
}
CRect rWin;
this->GetWindowRect(&rWin);
nTotWide = rWin.right-rWin.left;
m_Widths[0] = nTotWide / 4;
m_Widths[1] = nTotWide / 2;
m_Widths[2] = nTotWide - m_Widths[0];
m_Widths[3] = -1;
m_StatBar.SetMinHeight(25);
m_StatBar.SetParts( 4, m_Widths);
m_StatBar.SetText("WITH BORDER.", 0,0);
m_StatBar.SetText("WITHOUT BORDER.",1,SBT_NOBORDERS);
m_StatBar.SetText("POPUP.",2,SBT_POPOUT);
m_StatBar.SetText(NULL,3, SBT_OWNERDRAW);
hBmp = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1));
并重载下面函数
void CMyDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//
// Draw bitmap in status bar
//
HDC hdcMem; // device context for memory
HGDIOBJ hbmOld; // old bitmap area we're over-writing
BITMAP bm; // bitmap we're using
//
// Create a compatible DC in memory
//
hdcMem = CreateCompatibleDC(lpDrawItemStruct->hDC);
// Select the "logo.bmp" bitmap into the DC.
hbmOld = ::SelectObject(hdcMem, hBmp);
// Get the size of the bitmap
::GetObject(hBmp, sizeof(bm), &bm);
// Blt the bitmap to the part.
BitBlt(lpDrawItemStruct->hDC,lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.top, bm.bmWidth, bm.bmHeight,
hdcMem, 0, 0,SRCCOPY);
//
// Add some text..1st. get bounding rectangle, then position & display text
//
char szText[16];
RECT rText; // text rectangle
rText.left = lpDrawItemStruct->rcItem.left+24;
rText.top = lpDrawItemStruct->rcItem.top;
rText.right = lpDrawItemStruct->rcItem.right-20;
rText.bottom = lpDrawItemStruct->rcItem.bottom;
//
// add some text after the logo bitmap here
//
memset(szText,0,sizeof(szText));
strcpy(szText,"LOGO"); // text to draw
SelectObject( lpDrawItemStruct->hDC, GetStockObject( ANSI_VAR_FONT ) );
::SetBkColor(lpDrawItemStruct->hDC, 0x00c0c0c0); // set background color
ExtTextOut(lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.left+24, lpDrawItemStruct->rcItem.top+4, ETO_OPAQUE, &rText, szText,
strlen(szText),NULL ); // draw the text in the rectangle rText
//
// End adding text. Reselect the original object into the DC.
//
SelectObject(hdcMem, hbmOld);
// Delete the compatible DC.
DeleteDC(hdcMem);
}
编译完全通过,不过执行时到
void CStatusBarCtrl::DrawItem(LPDRAWITEMSTRUCT)
{
ASSERT(FALSE); // must override for self draw status bars
}
时出错,提示需要我重载该函数。