Put a CComboBox control on CToolBar

来源:互联网 发布:淘宝折扣软件 编辑:程序博客网 时间:2024/06/01 08:30

    This tutiual is about how to put a CComboBox control on CToolBar.look at the next description.

First of all,you should create a CcomboBoxBar class derived from CToolBar like this:

ComboToolBar.h:

     class CComboToolBar : public CToolBar// CToolBar----->you must use CToolBar instead of CToolBarCtrl by yourself.
{
// Construction
public:
 CComboToolBar();


// Attributes
public:
 CComboBox m_comboBox;//define CComboBox object

// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CComboToolBar)
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~CComboToolBar();

 // Generated message map functions
protected:
 //{{AFX_MSG(CComboToolBar)
  // NOTE - the ClassWizard will add and remove member functions here.
 //}}AFX_MSG
 afx_msg void OnSelChange();// Add response function while you select an item in list box.

 DECLARE_MESSAGE_MAP()
};

ComboToolBar.cpp:

  you must contain header file MainFrm.h in order to use main frame window pointer.

 #include "MainFrm.h"

   BEGIN_MESSAGE_MAP(CComboToolBar, CToolBar)
 //{{AFX_MSG_MAP(CComboToolBar)
  // NOTE - the ClassWizard will add and remove mapping macros here.
  ON_CBN_SELCHANGE(IDC_COMBOX,OnSelChange)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComboToolBar message handlers


void CComboToolBar::OnSelChange()
{
 CMainFrame *pFrame = (CMainFrame* )AfxGetMainWnd();//Get the main window for the child window
 int sel = pFrame->m_wndToolBar.m_comboBox.GetCurSel();
 CString text;
 pFrame->m_wndToolBar.m_comboBox.GetLBText(sel,text);
 AfxMessageBox(_T("you have selected ") + text);
}

In the CFrame  head file,you define a CComboToolBar class object like this:

   public:
 CComboToolBar m_wndToolBar;    // define my custom toolbar,use m_wndToolBar instead of default m_wndToolBar.
And then ,renturn CFrame::OnCreate function and add the following code like this:

/***************************************************************************************/
 //Set the ComboBox list information on tool bar as following description:
 CRect rect;
 m_wndToolBar.SetButtonInfo(12,IDC_COMBOX,TBBS_SEPARATOR|TBBS_GROUP,160);
 m_wndToolBar.GetItemRect(12,&rect);
 //Set the height of list box while the list box will be drag down(拉下)
 //this is the key step,you must do it correctly otherwise you will not get what you want.
 rect.bottom += 100;                                 // add 100 pixels as list height
 BOOL IsCreate = m_wndToolBar.m_comboBox.Create(CBS_DROPDOWN|WS_VISIBLE| //create list box control like this
  WS_TABSTOP|CBS_AUTOHSCROLL,rect,&m_wndToolBar,IDC_COMBOX);    
 if (!IsCreate)
 {
  return -1;
 }
 //initialize list box datas
 CString item;
 for (int i=0; i<10 ; i++)
 {
  item.Format("list item:%d",i);
  m_wndToolBar.m_comboBox.AddString(item);
 }
 //set the current item
 m_wndToolBar.m_comboBox.SetCurSel(0);
 /***************************************************************************************/

In the end,you have finished it.

原创粉丝点击