VS2010在工具栏上创建查找组合框,即:CMFCToolBar中加入CMFCToolBarComboBoxButton


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);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM