在对话框中添加toolbar和statusbar

主要运用RepositionBars()函数,对对话框中的控件进行布局,代码放在OnInitDialog()中:

BOOL C************::OnInitDialog()
{
	CModelessDialog::OnInitDialog();

	// Create status bar at the bottom of the dialog window
	if (m_statusBar.Create(this))
	{
		m_statusBar.SetIndicators(m_lpaIDStatusBar, m_cIDStatusBar);
		OnSetMessageString(AFX_IDS_IDLEMESSAGE);

		// Make a sunken or recessed border around the first pane
		m_statusBar.SetPaneInfo(0, m_statusBar.GetItemID(0),
			SBPS_STRETCH, NULL );
	}

	// Create toolbar at the top of the dialog window
	if (m_toolBar.Create(this))
	{
		m_toolBar.LoadBitmap(m_nIDBitmap);
		m_toolBar.SetButtons(m_lpaIDToolBar, m_cIDToolBar);
	}

	m_toolBar.SetBarStyle(m_toolBar.GetBarStyle() |
		CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

	// We need to resize the dialog to make room for control bars.
	// First, figure out how big the control bars are.
	CRect rcClientStart;
	CRect rcClientNow;
	GetClientRect(rcClientStart);
	//将重绘工具栏和状态栏后剩余的客户区空间放到rcClientNow中
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,
				   0, reposQuery, rcClientNow);

	// Now move all the controls so they are in the same relative
	// position within the remaining client area as they would be
	// with no control bars.
	CPoint ptOffset(rcClientNow.left - rcClientStart.left,
					rcClientNow.top - rcClientStart.top);

	CRect  rcChild;
	CWnd* pwndChild = GetWindow(GW_CHILD);
	while (pwndChild)
	{
		pwndChild->GetWindowRect(rcChild);
		ScreenToClient(rcChild);
		rcChild.OffsetRect(ptOffset);
		pwndChild->MoveWindow(rcChild, FALSE);
		pwndChild = pwndChild->GetNextWindow();
	}

	// Adjust the dialog window dimensions
	CRect rcWindow;
	GetWindowRect(rcWindow);
	rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
	rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
	MoveWindow(rcWindow, FALSE);

	// And position the control bars
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);         // Set big icon
	SetIcon(m_hIcon, FALSE);        // Set small icon

	// Finally, center the dialog on the screen
	CenterWindow();
	return TRUE;
}
posted @ 2011-05-12 20:16  MagiCube  阅读(1329)  评论(0编辑  收藏  举报