VC中CMFCToolBar插入组合框

本文详细介绍了在Visual Studio 2008的MFC应用程序中,如何通过CMFCToolBar工具栏类来添加组合框。包括在资源编辑器中预留位置、定义组合框对象、替换虚拟按钮、设置组合框属性及添加事件响应函数等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚在优快云博客上面看到一篇文章,讲解VS2008中的新类CMFCToolBar的使用, 其中有个关于插入组合框的部分居然要修改MFC的源代码,  文章链接 http://blog.youkuaiyun.com/chenjelly/article/details/6718478  


这里贴出正确的插入方法:


1. MainFrm.h, 声明变量

[cpp]  view plain  copy
 print ?
  1. CMFCToolBarComboBoxButton  *m_comboAddress;  


2. MainFrm.cpp, 在OnCreate函数中创建工具条并且插入一个组合框

[cpp]  view plain  copy
 print ?
  1. if (!m_wndToolBar.CreateEx (this, TBSTYLE_FLAT,  
  2.         CBRS_SIZE_FIXED | AFX_DEFAULT_TOOLBAR_STYLE) ||  
  3.         !m_wndToolBar.LoadToolBar (IDR_MAINFRAME, uiToolbarColdID, uiMenuID,   
  4.         FALSE /* Not locked */, uiToolbarDisID, 0, uiToolbarHotID, uiToolbarPresID))  
  5.     {  
  6.         TRACE0("Failed to create toolbar\n");  
  7.         return -1;      // fail to create  
  8.     }  
  9.   
  10.     //插入组合框  
  11.     m_wndToolBar.InsertButton   
  12.         (CMFCToolBarComboBoxButton(ID_GO_ADDRESS_EDIT, 0), 3);  
  13.       
  14.     //设置组合框属性  
  15.     m_comboAddress = (CMFCToolBarComboBoxButton*)m_wndToolBar.GetButton(3);  
  16.     m_comboAddress->EnableWindow(TRUE);  
  17.     m_comboAddress->SetCenterVert();  
  18.     m_comboAddress->SetDropDownHeight(100);//设置下拉列表的高度  
  19.     m_comboAddress->SetFlatMode();  
  20.       
  21.     //添加内容  
  22.     m_comboAddress->AddItem(_T("once"));  
  23.     m_comboAddress->AddItem(_T("if only"));  
  24.     m_comboAddress->AddItem(_T("P.S.I Love You"));  
  25.     m_comboAddress->AddItem(_T("Love Me If You Dare"));  
  26.     m_comboAddress->SelectItem(0);           //默认选中  
  27.   
  28.   
  29.     DockPane (&m_wndToolBar);  


另外的就是网上找的一篇文章了


VC2008在CMFCToolBar工具栏中加入组合框

如何在CMFCToolBar工具栏中加入组合框等控件,且先看在线MSDN上怎么说的:

To add a combo box button to a toolbar, follow these steps:

1. Reserve a dummy resource ID for the button in the parent toolbar resource.

2. Construct a CMFCToolBarComboBoxButton object.

3. In the message handler that processes the AFX_WM_RESETTOOLBAR message, replace the dummy button with the new combo box button by using CMFCToolBar::ReplaceButton.

具体过程如下:

1.在工具栏资源编辑器中加入id为IDR_COM的空白工具栏,

2.在头文件中定义组合框

CMFCToolBarComboBoxButton* m_ComboButton; 

3.BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)中添加消息响应

    ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, OnToolbarReset)

在头文件中声明消息函数:

     afx_msg LRESULT OnToolbarReset(WPARAM,LPARAM);

在CMainFram中定义处理函数:

LRESULT CMainFrame::OnToolbarReset(WPARAM wp,LPARAM lp)
{      
     m_ComboButton = new CMFCToolBarComboBoxButton(IDR_COM,GetCmdMgr ()->GetCmdImage (IDR_COM, FALSE));

     m_ComboButton->EnableWindow(true);
     m_ComboButton->SetCenterVert();
     m_ComboButton->SetDropDownHeight(25);
     m_ComboButton->SetFlatMode();
     m_ComboButton->AddItem(_T("OPTION1"));
     m_ComboButton->AddItem(_T("OPTION2"));
     m_ComboButton->SelectItem(0); 
     m_wndToolBar.ReplaceButton (IDR_COM, *m_ComboButton);
 return 0;
}

2、添加组合框的事件消息响应函数

消息映射:

ON_COMMAND(IDR_COM, &CMainFrame::OnClickComboBox)
 ON_CBN_SELCHANGE(IDR_COM,&CMainFrame::OnSelChangeClick)//别忘了在TOOLBAR中添加IDR_COM资源。

消息声明:

afx_msg void OnSelChangeClick();
 afx_msg void OnClickComboBox();

消息处理函数:

void CMainFrame::OnSelChangeClick()
{
 CMFCToolBarComboBoxButton* pSrcCombo = CMFCToolBarComboBoxButton::GetByCmd (IDR_COM, TRUE);
 int index = m_ComboButton->GetCurSel();
 index = pSrcCombo->GetCurSel();
 CString str = pSrcCombo->GetItem(index);
}

void CMainFrame::OnClickComboBox()
{

}

(注意:一定要GetByCmd;OnClickComboBox没有做任何处理,但是如果去掉的话,组合框将编程灰色,无法使用)。

注:更新工具栏:

1.工具栏的右侧向下的小箭头->添加或删除按钮->标准->重置工具栏

2.运行regedit打开注册表->HKEY_CURRENT_USER->Software->工程名,找到工程名将其从注册表中删之。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值