Using ComboBox on a Flat Toolbar
This was tricky i spent week and a half on this but I solved it by studying the messages that a combobox gives off. I want to thank joerg for helping me get the combobox on the toolbar this class was written to take advantage of th CToolBarEx as a base class that Joerg had contributated.
There is a message CBN_SELENDOK that is immediatly posted before CBN_SELCHANGE.
Using these two messages and a static variable you can pass the current sel that you selected to class outside of the Combobar class. the nice thing is that the api handles the setting the focus correctly.
Here is some code on how it was down.
combobar.cpp
BEGIN_MESSAGE_MAP(CComboBar, CToolBarEx) //{{AFX_MSG_MAP(CComboBar) ON_CBN_SELENDOK(IDW_TOOLCOMBO, OnSelEndOk) //}}AFX_MSG_MAP END_MESSAGE_MAP() BOOL CComboBar::Create(CFrameWnd * pParent, UINT nID, UINT nComboID, const int nWidth, const int nHeight) { // Create the toolbar as CMainFrame::OnCreate() would do if (!CToolBar::Create(pParent) || !LoadToolBar(nID)) { TRACE0("Falied to create the toolbar.\n"); return FALSE; // fail to create } // set the size of combo-control CRect rect(-nWidth, -nHeight, 0, 0); // make the button, that is selected to be the combo-control, // a separator and resize that separator ASSERT(CommandToIndex(nComboID) >= 0); // make sure the id is valid SetButtonInfo(CommandToIndex(nComboID), nComboID, TBBS_SEPARATOR, nWidth); // create the combo-control itself, reposition it in the // client-area and show it if (!m_pWndBox.Create(WS_CHILD | CBS_DROPDOWN | CBS_AUTOHSCROLL | WS_VSCROLL | CBS_HASSTRINGS, rect, this, nComboID)) { TRACE("Failed to create the combo-box %p .\n", nComboID); return FALSE; } GetItemRect(CommandToIndex(nComboID), &rect); m_pWndBox.SetWindowPos(0, rect.left, rect.top, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS); m_pWndBox.SetFont(&m_GuiFont); m_pWndBox.ShowWindow(SW_SHOW); if (!OnInitialCreate()) { TRACE("Failed to add strings to %p\n", nComboID); return FALSE; } // yup - now we should be able to see a toolbar with a combo-box return TRUE; } BOOL CComboBar::OnInitialCreate() { if(m_pWndBox.m_hWnd != NULL) { //This is where you add your strings m_pWndBox.AddString("Page Width"); m_pWndBox.AddString("150 %"); m_pWndBox.AddString("125 %"); m_pWndBox.AddString("100 %"); m_pWndBox.AddString("75 %"); m_pWndBox.AddString("50 %"); m_pWndBox.AddString("25 %"); //Don't forget the initial position. m_pWndBox.SetCurSel(0); return TRUE; } return FALSE; } int CComboBar::m_cbIndex = 0; void CComboBar::OnSelEndOk() { m_cbIndex = m_pWndBox.GetCurSel(); }
combobar.h
class CComboBar : public CToolBarEx { // Construction public: CComboBar(); CComboBox m_pWndBox; //This is the storage variable for the index value of the combobox static int m_cbIndex; // Attributes public: // Operations public: BOOL OnInitialCreate(); //is the toolbar-resource to load //is the ID of the button, that shall be used as the combo-control BOOL Create(CFrameWnd * pParent, UINT nID, UINT nComboID, const int nWidth, const int nHeight); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CComboBar) public: virtual void OnFinalRelease(); //}}AFX_VIRTUAL // Implementation public: virtual ~CComboBar(); // Generated message map functions protected: //{{AFX_MSG(CComboBar) //}}AFX_MSG DECLARE_MESSAGE_MAP() // Generated OLE dispatch map functions //{{AFX_DISPATCH(CComboBar) // NOTE - the ClassWizard will add and remove member functions here. afx_msg void OnSelEndOk(); //}}AFX_DISPATCH DECLARE_DISPATCH_MAP() DECLARE_INTERFACE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Developer Studio will insert additional declarations immediately before the previous line. #endif //!defined(AFX_COMBOBAR_H__1A26E666_6D60_11D1_81F9_0020781030ED__INCLUDED_)
to use this stuff in another class you need to include
#include "combobar.h"
then using the ON_CBN_SELCHANGE message you retrieve the index like this
void CDSViewView::OnSelChange() { int nCase = m_ComboBox.m_cbIndex; switch(nCase) { case 0: //code for m_cbIndex = 0 break; case 1: //code for m_cbInex = 1 break; default: //default code here } }
In CToolBarEx in the header file.I move the following code around.
private: CFont m_GuiFont;
to
public: CFont m_GuiFont;

Comments
How can I change the bitmap of a toolbar button of MDI application
Posted by Legacy on 10/18/2002 12:00amOriginally posted by: Amitesh Bharti
I want to change the bitmap of a button in toolbar of MDI window. I want to have one bitmap during unpressed state and another bitmap during pressed state.
I have same ID defined in resource for this tool bar button and corresponding menu items. I am able to change the state of toolbar button(Pressed/Unpressed and menu ( toggle between two text )
Please suggest
ReplyHow can I delete the separater line behind the combo box in the tool bar?
Posted by Legacy on 09/26/2002 12:00amOriginally posted by: Chan Park
I made a combo box on tool bar according to your conducts. It was successful! But now, I got one problem.
Because my tool bar buttons are sized with 32*32 while the combo box' heigh is 16, still there is a vertical separater line behind the combo box. I thought it was caused by the style option 'TBBS_SEPARATOR' in the next code that creating extended area for combo box:
SetButtonInfo(CommandToIndex(nComboID), nComboID, TBBS_SEPARATOR,nWidth);
You may think it's not good to see. I don't want to extend the combo box' height(16). Then, how can I delete the separater behind the combo box? I tried it with several methods but failed.
I need your favor.
ReplyNEED HELP
Posted by Legacy on 07/12/2002 12:00amOriginally posted by: LEDHINES
HOW DO YOU DELETE NAMES OFF OF A LOCATION TOOLBAR? ((ASAP)
Replylong winded
Posted by Legacy on 11/08/2001 12:00amOriginally posted by: simon kenee
This is a very long winded way of going about this. There is simple example which I got working in the MFC samples of a combo on a toolbar. Can be used with the flat look toolbar found on this site for a nice outlook type effect. Although it takes a bit of adjustment to get it to fit the bar properly.
Reply
HELP
Posted by Legacy on 08/24/2001 12:00amOriginally posted by: Subrata
Ur sample is OK . But how do i implement it ? Can u send one demo project featuring this ?
ReplySubrata
How to get a pointer to combo
Posted by Legacy on 12/03/2000 12:00amOriginally posted by: novice
How to get a pointer to combo on toolbar in CDocument or somewhere else?
ReplyUsing flat toolbar, but cannot "catch" any CBN message
Posted by Legacy on 06/01/2000 12:00amOriginally posted by: Tim Hodgson
I'm using a flat toolbar and trying to get any message for the combo box, but no such luck. Any suggestions?
ReplyComboboxes are also covered in the docktool MFC sample
Posted by Legacy on 06/29/1999 12:00amOriginally posted by: Will Sargent
There is a combobox in the docktool MFC sample, not that I've been able to get it to work.
Is there an advantage to subclassing CToolbar to CEditbar rather than using a CDialogBar? I'm more used to JFC/Swing than MFC, and some of the resource hassles seem a little odd to me.
Reply