皮秒祛斑效果好吗:VC2008在CMFCToolBar工具栏中加入组合框

来源:百度文库 编辑:中财网 时间:2024/05/08 02:45:14

如何在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->工程名,找到工程名将其从注册表中删之。