StatusBar on Dialogs-在VC中为作为主窗口的对话框添加状态栏的几种方法

感谢CSDN论坛上的 Featured(我握着爱情的门票静静排队……)  提供了一个演示程序的下载链接:http://www.bypro.net/PostAttachment.aspx?PostID=21994&AttachmentID=1834

感谢CSDN论坛上的 goodboyws(深夜不眠者) 提供了一个为对话框加状态栏和工具栏的演示程序的下载链接:http://www.codeproject.com/dialog/dlgtoolstatusbar.asp

感谢CSDN论坛上的  DentistryDoctor(My heart will fly,in the sky.)   提供了本文的链接

http://www.codeguru.com/Cpp/W-D/dislog/toolbarsandstatusbars/article.php/c1955/

Girish Pandit (view profile)
July 28, 1999

MFC allows you to easily add status bars to CFrameWnd-derived windows. However, if you want to add a status bar to a dialog, you're going to find the going just a bit more difficult.

Basically, it turned out that I had to dig very deep into the MFC documentation in order to find anything to help me out. One example I found is by ZEKSER Cyril. His techniques works fine, but (IMHO) is not very "clean" since you have to place an invisible static object on the dialog as a kind of placeholder for the status bar. However, I do want to thank him very much for showing me the light at the end of the tunnel.

The technique I came up with works like this: First, you need to develop your dialog (and define its CDialog-based class). Nothing new here so far.

Then, insert the following code into the CDialog::OnInitDialog function (the m_StatBar variable is of type CStatusBarCtrl).


BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();


	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);
	int nTotWide;

	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));

	return TRUE;  

}

The fourth pane of the status bar is owner drawn because it is used to display a bitmap. In order to do this, simply add a message handler for the dialog's WM_DRAWITEM message. Once you've added that function, update it so that when finished it looks like the following.


void CMyDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{

	HDC hdcMem;
    	HGDIOBJ hbmOld; 
    	BITMAP bm;
	hdcMem = CreateCompatibleDC(lpDrawItemStruct->hDC);

    	hbmOld = ::SelectObject(hdcMem, hBmp);

    	::GetObject(hBmp, sizeof(bm), &bm);


    	BitBlt(lpDrawItemStruct->hDC,lpDrawItemStruct->rcItem.left,
    	lpDrawItemStruct->rcItem.top, bm.bmWidth, bm.bmHeight,
        	hdcMem, 0, 0,SRCCOPY);


	char szText[16];
	RECT rText;  
	rText.left = lpDrawItemStruct->rcItem.left+24;
	rText.top  = lpDrawItemStruct->rcItem.top;
	rText.right = lpDrawItemStruct->rcItem.right-20;
	rText.bottom = lpDrawItemStruct->rcItem.bottom;

	memset(szText,0,sizeof(szText));
	strcpy(szText,"LOGO");

	SelectObject( lpDrawItemStruct->hDC, GetStockObject( ANSI_VAR_FONT ) );
	::SetBkColor(lpDrawItemStruct->hDC, 0x00c0c0c0);  
    	ExtTextOut(lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.left+24, lpDrawItemStruct->rcItem.top+4, ETO_OPAQUE, &rText, szText,
    	strlen(szText),NULL );
    	SelectObject(hdcMem, hbmOld);

    	DeleteDC(hdcMem);
}

Make the following changes to the dialog's header file.


class CMyDlg : public CDialog
{

public:
	CMyDlg(CWnd* pParent = NULL);	// standard constructor
	CStatusBarCtrl	m_StatBar;

	.....................................
	...................................
	......................................
}
CStatusBarCtrl m_StatBar; ..................................... ................................... ...................................... }

Finally, make the following changes to the resource.h file.




#define IDM_ABOUTBOX		0x0010
#define IDC_STATUSBAR           32500
	.....................................
	...................................
	......................................

That's it! You now have a status bar at the bottom of your dialog!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值