65.9K
CodeProject 正在变化。 阅读更多。
Home

向停靠工具栏添加组合框

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.12/5 (13投票s)

2001年5月13日

1分钟阅读

viewsIcon

234076

downloadIcon

5584

本文演示了一种将组合框轻松添加到停靠工具栏的方法。

引言

本文演示了一种将组合框轻松添加到停靠工具栏的方法。我需要在我的 dBase Explorer 项目的工具栏中添加一个编辑控件(有关详细信息,请访问我的网站:http://www.codearchive.com/~dbase/)。虽然我在 Code Project 网站上找到了一些相关的文章,但大多数实现起来都很困难,并且需要添加大量的代码甚至新的类。本文展示了您只需添加几行代码就可以将组合框添加到工具栏的方法。

为了将组合框添加到工具栏,您需要在 CMainFrame 类中声明一个类型为 CComboBox 的成员变量,如下所示

//
// Any source code blocks look like this
class CMainFrame : public CFrameWnd
{
	
protected: // create from serialization only
	CMainFrame();
	DECLARE_DYNCREATE(CMainFrame)

protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CComboBox m_comboBox;

...
};

您还需要在工具栏中为组合框创建一个占位符图标。通过双击它,应为占位符图标分配一个 ID,例如,在我的例子中,ID_COMBO 被分配给占位符。然后,通过调用 Create 函数,您可以在工具栏中创建组合框,如下所示

//
if(!m_comboBox.Create(CBS_DROPDOWNLIST | CBS_SORT | WS_VISIBLE |
		WS_TABSTOP | WS_VSCROLL, rect, &m_wndToolBar, ID_COMBO))
	{
		TRACE(_T("Failed to create combo-box\n"));
		return FALSE;
	}

 

完整的 OnCreate 函数列表如下

//
//
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	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
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	CRect rect;
	int nIndex = m_wndToolBar.GetToolBarCtrl().CommandToIndex(ID_COMBO);
	m_wndToolBar.SetButtonInfo(nIndex, ID_COMBO, TBBS_SEPARATOR, 205);
	m_wndToolBar.GetToolBarCtrl().GetItemRect(nIndex, &rect);
	rect.top = 1;
	rect.bottom = rect.top + 250 /*drop height*/;
	if(!m_comboBox.Create(CBS_DROPDOWNLIST | CBS_SORT | WS_VISIBLE |
		WS_TABSTOP | WS_VSCROLL, rect, &m_wndToolBar, ID_COMBO))
	{
		TRACE(_T("Failed to create combo-box\n"));
		return FALSE;
	}
	m_comboBox.AddString("Toolbar Combobox item one");
	m_comboBox.AddString("Toolbar Combobox item two");
	m_comboBox.AddString("Toolbar Combobox item three");
	m_comboBox.AddString("Toolbar Combobox item four");
	m_comboBox.AddString("Toolbar Combobox item five");
	m_comboBox.AddString("Toolbar Combobox item six");

	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);
	return 0;
}
© . All rights reserved.