1. 添加資源,新加一個ToolBar的資源 IDR_TOOLBAR_SEARCH,並在此工具欄上再加上一個項:取ID為:ID_SEARCH
2. 在MainFrm類中加入如下代碼:
變量:
CMFCToolBar m_wndToolBarSearch;
方法:
CMFCToolBarComboBoxButton *m_comboButton; afx_msg LRESULT OnToolbarReset(WPARAM,LPARAM); afx_msg void OnSelChangeClick(); afx_msg void OnClickComboBox();
3. 在MainFrm的消息映射中加入如下代碼:
ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, OnToolbarReset) ON_COMMAND(ID_SEARCH, &CMainFrame::OnClickComboBox) ON_CBN_SELCHANGE(ID_SEARCH,&CMainFrame::OnSelChangeClick)
4. 在MainFrm的構造函數中修改如下:
CMainFrame::CMainFrame() : m_comboButton( NULL )
5. 在MainFrm的析構函數中加入:
if ( NULL != m_comboButton ) { delete m_comboButton; m_comboButton = NULL; } else ;
6. 添加消息響應函數的實現
LRESULT CMainFrame::OnToolbarReset(WPARAM wp,LPARAM lp) { if ( NULL == m_comboButton ) { m_comboButton = new CMFCToolBarComboBoxButton(ID_SEARCH, GetCmdMgr ()->GetCmdImage(ID_SEARCH, FALSE), CBS_DROPDOWN); } else ; m_comboButton->EnableWindow(TRUE); m_comboButton->SetCenterVert(); m_comboButton->SetDropDownHeight(10); m_comboButton->SetFlatMode(); m_comboButton->AddItem(_T("OPTION1")); m_comboButton->AddItem(_T("OPTION2")); m_comboButton->SelectItem(0); m_wndToolBarSearch.ReplaceButton (ID_SEARCH, *m_comboButton); return 0; } void CMainFrame::OnSelChangeClick() { MessageBox( _T("OnSelChangeClick.") ); } void CMainFrame::OnClickComboBox() { CMFCToolBarComboBoxButton* pSrcCombo = CMFCToolBarComboBoxButton::GetByCmd (ID_SEARCH, TRUE); int index = m_comboButton->GetCurSel(); index = pSrcCombo->GetCurSel(); CString str; pSrcCombo->GetEditCtrl()->GetWindowText( str ); pSrcCombo->AddItem(str); MessageBox( _T("OnClickComboBox: ") + str ); }
7. 在MainFrm的OnCreate中的適當地方添加創建的代碼:
if (!m_wndToolBarSearch.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC, CRect(1, 1, 1, 1), IDR_TOOLBAR_SEARCH) || !m_wndToolBarSearch.LoadToolBar(theApp.m_bHiColorIcons ? IDR_TOOLBAR_SEARCH : IDR_TOOLBAR_SEARCH)) { TRACE0("未能創建工具欄\n"); return -1; // 未能創建 } ////////////////////////////////////////////////////////////////////////////////////// m_wndToolBarSearch.SetWindowText( _T("Search") ); ////////////////////////////////////////////////////////////////////////////////////// m_wndToolBarSearch.EnableCustomizeButton(TRUE, ID_VIEW_CUSTOMIZE, strCustomize); ////////////////////////////////////////////////////////////////////////////////////// m_wndToolBarSearch.EnableDocking(CBRS_ALIGN_ANY); ////////////////////////////////////////////////////////////////////////////////////// DockPane(&m_wndToolBarSearch);
