CMFCToolBarComboBoxButton的API可以参考CMFCToolBarComboBoxButton的介绍,
在其中的Remark栏,介绍了如何向工具栏中中增加ComboBox,更具体的做法在Walkthrough: Putting Controls On Toolbars
通过上面的做法确实可以成功添加,但是有几个坑需要注意:
1,有可能你的代码跟它一模一样,但是你的工具栏就是不能显示ComboBox
这有可能在你还没有成功增加combo就运行了你程序的缘故,导致注册表中已经存在toolbar相关按钮的信息,反序列化时,仍然采用原来的控件按钮,解决办法是,把应用的注册信息删掉,或者在App的InitInstance中,调用SetRegistryKey的地方,入参改为另外一个值,再试试。
2,按照MSDN上的介绍,你还处理不了ComboBox的VK_RETURN消息。
也就是MSDN中的步骤<3. In the CFindComboBox class, override the PreTranslateMessage virtual method. This method will enable the combo box to process the WM_KEYDOWN message. >,实际中发现,PreTranslateMessage 根本不会触发,我们需要从CMFCToolBarComboBoxEdit,派生出自己的Edit,并重载PreTranslateMessage,在Edit的PreTranslateMessage中才能正确捕获到回车事件。
//FilterComboBox.h
#pragma once
class CFilterComboBox : public CComboBox
{
DECLARE_DYNAMIC(CFilterComboBox)
public:
CFilterComboBox();
virtual ~CFilterComboBox();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnCbnSelchange();
};
#include "FilterComboBox.h"
// FilterComboBox.cpp : 实现文件
//
#include "stdafx.h"
#include "MFCNewStyle.h"
#include "FilterComboBox.h"
// CFilterComboBox
IMPLEMENT_DYNAMIC(CFilterComboBox, CComboBox)
CFilterComboBox::CFilterComboBox()
{
}
CFilterComboBox::~CFilterComboBox()
{
}
BEGIN_MESSAGE_MAP(CFilterComboBox, CComboBox)
ON_CONTROL_REFLECT(CBN_SELCHANGE, &CFilterComboBox::OnCbnSelchange)
END_MESSAGE_MAP()
void CFilterComboBox::OnCbnSelchange()
{
// TODO: 在此添加控件通知处理程序代码
AfxMessageBox(_T("ss"));
}
#pragma once
// MyToolBarComboBoxEdit.h
class CMyToolBarComboBoxEdit : public CMFCToolBarComboBoxEdit
{
public:
CMyToolBarComboBoxEdit(CMFCToolBarComboBoxButton& combo);
virtual ~CMyToolBarComboBoxEdit();
virtual BOOL PreTranslateMessage(MSG* pMsg);
};
// MyToolBarComboBoxEdit.cpp : 实现文件
//
#include "stdafx.h"
#include "MFCNewStyle.h"
#include "MyToolBarComboBoxEdit.h"
// CMyToolBarComboBoxEdit
CMyToolBarComboBoxEdit::CMyToolBarComboBoxEdit(CMFCToolBarComboBoxButton& combo)
: CMFCToolBarComboBoxEdit(combo)
{
}
CMyToolBarComboBoxEdit::~CMyToolBarComboBoxEdit()
{
}
BOOL CMyToolBarComboBoxEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if(pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_RETURN)
{
AfxMessageBox(_T(" CMFCToolBarComboBoxEdit enter"));
}
}
return CMFCToolBarComboBoxEdit::PreTranslateMessage(pMsg);
}
#pragma once
// FilterComboButton.h
class CFilterComboButton : public CMFCToolBarComboBoxButton
{
DECLARE_SERIAL(CFilterComboButton)
public:
CFilterComboButton();
virtual ~CFilterComboButton();
virtual CComboBox* CreateCombo(CWnd* pWndParent, const CRect& rect);
virtual CMFCToolBarComboBoxEdit* CreateEdit(CWnd* pWndParent, const CRect& rect, DWORD dwEditStyle);
};
// FilterComboButton.cpp : 实现文件
#include "stdafx.h"
#include "MFCNewStyle.h"
#include "FilterComboButton.h"
#include "FilterComboBox.h"
#include "MyToolBarComboBoxEdit.h"
static const WORD wSchema = 1;
// CFilterComboButton
IMPLEMENT_SERIAL(CFilterComboButton, CMFCToolBarComboBoxButton, wSchema )
CFilterComboButton::CFilterComboButton()
: CMFCToolBarComboBoxButton(ID_FILTER_COMBO, GetCmdMgr()->GetCmdImage(ID_FILTER_COMBO), CBS_DROPDOWN)
{
}
CFilterComboButton::~CFilterComboButton()
{
}
// CFilterComboButton 成员函数
CComboBox* CFilterComboButton::CreateCombo(CWnd* pWndParent, const CRect& rect)
{
CFilterComboBox* box = new CFilterComboBox();
if (!box->Create(m_dwStyle, rect, pWndParent, m_nID))
{
delete box;
return NULL;
}
box->AddString(_T("ts"));
return box;
}
CMFCToolBarComboBoxEdit* CFilterComboButton::CreateEdit(CWnd* pWndParent, const CRect& rect, DWORD dwEditStyle)
{
CMyToolBarComboBoxEdit* edit = new CMyToolBarComboBoxEdit(*this);
if (!edit->Create(dwEditStyle, rect, pWndParent, m_nID))
{
delete edit;
return NULL;
}
return edit;
}
//CMainFrame中AFX_WM_RESETTOOLBAR的处理函数
//ON_REGISTERED_MESSAGE(AFX_WM_RESETTOOLBAR, &CMainFrame::OnAfxWmResettoolbar)
afx_msg LRESULT CMainFrame::OnAfxWmResettoolbar(WPARAM wParam, LPARAM lParam)
{
switch(wParam)
{
case IDR_MAINFRAME:
TRACE(_T("IDR_MAINFRAME\n"));
m_wndToolBar.ReplaceButton(ID_FILTER_COMBO, CFilterComboButton());
break;
default:
break;
}
return 0;
}