MFC工具栏添加ComboBox控件

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);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值