Help: How to insert a combo box control in a tool bar
Prakash N -- pran@wings.xko.dec.com Tuesday, August 20, 1996 Environment: MSVC 4.0/Win 95 Hello! I have a toolbar which is created using the resource editor. In that = toolbar, there are options only to insert buttons. Is there any = possibility to insert a combo box ( a drop down listbox) in the toolbar? = What I expect is, instead of deriving the toolbar class from the = CToolBar class, if I derive it from its base class CControlBar, it may = be possible. Any ideas on this?=20 Cheers, Prakash pran@wings.xko.dec.com
Jim Leavitt -- jimll@halcyon.com Friday, August 23, 1996 [Mini-digest: 2 responses] Prakash.. You wrote... I have a toolbar which is created using the resource editor. In that = toolbar, there are options only to insert buttons. Is there any = possibility to insert a combo box ( a drop down listbox) in the toolbar? = What I expect is, instead of deriving the toolbar class from the = CToolBar class, if I derive it from its base class CControlBar, it may = be possible. Any ideas on this?=20 End... There is a sample... CTRLBARS on the vc 4 cd that illustrates exactly = what you're trying to do. >From VC Help - Search ... "combo near toolbar" EXTRACT... "The second toolbar, the Style Bar, illustrates replacing a toolbar = button (or separator) with a control -- a combo box in this example. = CMainFrame::CreateStyleBar creates a 100-pixel-wide toolbar separator. = It then creates the combo box (IDW_COMBO) as a child of the toolbar, and = sets the position of the combo box to take the space it just allocated = for the separator." Sounds like what you want! Jim Leavitt ---------- -----From: "Rommel Songco"Hi there! You may still use the CToolBar class. What you do is insert a separator in your list of buttons where you want the combo box to be placed. Call SetButtonInfo to adjust the separator's width. Note that for separators, the last parameter of this function sets the width in pixels. Create your combo box and position it in the space allocated in the separator. Regards, Rommel rsongco@spectrasoft.com
Andrew Dalgleish -- andrewd@axonet.com.au Monday, August 26, 1996 [Mini-digest: 5 responses] Rather than replace a separator, put a blank button in with the Combo-box's control ID. You can search for it in the code by using CToolBarCtrl::CommandToIndex(), then proceed as per the WordPad example. This allows you to change the button order etc in the resource editor without changing and recompiling the code. Regards, Andrew Dalgleish ---------- From: owner-mfc-l[SMTP:owner-mfc-l@majordomo.netcom.com] Sent: Friday, 23 August 1996 15:58 To: 'Mfc-l' Subject: RE: Help: How to insert a combo box control in a tool bar [Mini-digest: 2 responses] Prakash.. You wrote... I have a toolbar which is created using the resource editor. In that = toolbar, there are options only to insert buttons. Is there any = possibility to insert a combo box ( a drop down listbox) in the toolbar? = What I expect is, instead of deriving the toolbar class from the = CToolBar class, if I derive it from its base class CControlBar, it may = be possible. Any ideas on this?=20 End... There is a sample... CTRLBARS on the vc 4 cd that illustrates exactly = what you're trying to do. >From VC Help - Search ... "combo near toolbar" EXTRACT... "The second toolbar, the Style Bar, illustrates replacing a toolbar = button (or separator) with a control -- a combo box in this example. = CMainFrame::CreateStyleBar creates a 100-pixel-wide toolbar separator. = It then creates the combo box (IDW_COMBO) as a child of the toolbar, and = sets the position of the combo box to take the space it just allocated = for the separator." Sounds like what you want! Jim Leavitt ---------- -----From: "Rommel Songco"Hi there! You may still use the CToolBar class. What you do is insert a separator in your list of buttons where you want the combo box to be placed. Call SetButtonInfo to adjust the separator's width. Note that for separators, the last parameter of this function sets the width in pixels. Create your combo box and position it in the space allocated in the separator. Regards, Rommel rsongco@spectrasoft.com -----From: patwari Create a CToolBar derived class .In that class, handle WM_CREATE. In the = messagehandler of WM_CREATE, create a ComboBox. Call CToolBar's Create in CMainFrame::OnCreate. Here is the outline.... MainFrm.Cpp int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) =3D=3D -1) return -1; =09 if (!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create }=20 } MainFrm.h CMyToolBar m_wndToolBar; MyToolBar.cpp int CMyToolBar::OnCreate(LPCREATESTRUCT lpCreateStruct)=20 { if (CToolBar::OnCreate(lpCreateStruct) =3D=3D -1) return -1; m_CBox.Create(CBS_DROPDOWNLIST|WS_VISIBLE|WS_VSCROLL|WS_CHI = LD,CRect(0,0,80,80), this, ID_COMBO); =09 return 0; } MyToolBar.h #define ID_COMBO 800 class CMyToolBarCtrl : public CToolBar { public: CComboBox m_CBox; public: public: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); } hope this helps... bye, Jaiteerth Patwari -----From: beriksen@cda.com The problem with the CTRLBARS sample is that sometimes even though you specifically tell the combo box to be the height of the underlying button, you still see that the bottom of the combo box is clipped a bit. This doesn't happen under all resolutions, but on several machines i've found this to be the case. I believe that it has something to do with the font of the combo box beeing too tall, and the combo box won't draw itself smaller than the size necessary to display the selected item from the combo's list. If anyone has seen this behaviour, let me know, 'cause I'd love to fix it for myself. Brian Eriksen beriksen@cda.com -----From: "Rommel Songco" Hi there! You may still use the CToolBar class. What you do is insert a separator in your list of buttons where you want the combo box to be placed. Call SetButtonInfo to adjust the separator's width. Note that for separators, the last parameter of this function sets the width in pixels. Create your combo box and position it in the space allocated in the separator. Regards, Rommel rsongco@spectrasoft.com -----From: Mario Contestabile This is very true, except that you can't insert a button (the placeholder) in the leftmost or rightmost position with the separator attribute. If the combo box needs to be in one of those positions, give the button an ID similar to ID_FUTURE_COMBO and then use SetButtonInfo() to turn it into a separator. // Notice how I use a hard coded 0, because I know it is the leftmost button // If it was in any other spot, I'd use ID_FUTURE_COMBO m_wndToolBar.SetButtonInfo(0, IDW_COMBO, TBSTYLE_SEP, size.cx); CRect rect; m_wndToolBar.GetItemRect(0, &rect); rect.bottom = rect.top + 100; // This is the drop down size of combo in pixels // Create the combo box if (!m_Combo.Create(CBS_DROPDOWNLIST|WS_VISIBLE|WS_TABSTOP, rect, &m_wndToolBar, IDW_COMBO)) mcontest@universal.com -----From: Hugh Robinson There is an article on how to do this in Windows Developer's Journal, August 1996, page 45 Basically you put a separator in the toolbar and create the combo box at the point where the separator is in the toolbar. The code is at ftp://ftp.mfi.com/pub/windev/1996 Hugh Robinson hugh@ssihou.ssii.com
Roger Onslow -- Roger_Onslow@compsys.com.au Tuesday, August 27, 1996 Prakash >Environment: MSVC 4.0/Win 95 > >Hello! > >I have a toolbar which is created using the resource editor. In that toolbar, there are options only to insert buttons. Is there any possibility to insert a combo box ( a >drop down listbox) in the toolbar? Yes .. you can replace one of the toolbar buttons in code with a combo box (which you also create in code). I believe there is an example in MFC samples (I'm sure that's where I got the code I based my toolbar with combo code on). Try DOCKTOOL sample perhaps ?? (do a find in files on CComboBox class and you should find it) I could look up further info for you if you get stuck. >What I expect is, instead of deriving the toolbar class from the CToolBar class, if I derive it from its base class CControlBar, it may be possible. No need -- CToolBar is more than capable with a little work (probably less work than if you try to do it all fom CControlBar >Any ideas on this? Roger
Jeff Wishnie -- jwishnie@swellsoft.com Thursday, August 29, 1996 [Mini-digest: 3 responses] See the WordPad example. It includes two toolbars with combo-boxes and is easy to follow. - Jeff At 10:08 AM 8/27/96 EAT, you wrote: >Prakash >>Environment: MSVC 4.0/Win 95 >> >>Hello! >> >>I have a toolbar which is created using the resource editor. In that toolbar, >there are options only to insert buttons. Is there any possibility to insert a >combo box ( a >drop down listbox) in the toolbar? > >Yes .. you can replace one of the toolbar buttons in code with a combo box >(which you also create in code). >I believe there is an example in MFC samples (I'm sure that's where I got the >code I based my toolbar with combo code on). >Try DOCKTOOL sample perhaps ?? (do a find in files on CComboBox class and you >should find it) >I could look up further info for you if you get stuck. > >>What I expect is, instead of deriving the toolbar class from the CToolBar >class, if I derive it from its base class CControlBar, it may be possible. > >No need -- CToolBar is more than capable with a little work (probably less work >than if you try to do it all fom CControlBar > >>Any ideas on this? > >Roger > > > > jwishnie@swellsoft.com 415 243-9900 (w) -----From: "Dean Grimm"Try this: create a toolbar resource with a dummy button & name it ID_MY_COMBO, or something like that. Also create a string resource with the same ID if you want tooltips. Then, add the following to CMainFrame where: nButton is the Nth button on the toolbar ctl is the CComboBox object nControl is the controlID (ID_MY_COMBO) nComboWidth is the desired width of the combobox nDropHeight is the desired height of the drop list parent is the toolbar object BOOL CMainFrame::AddComboToToolbar(UINT nButton, CComboBox& ctl, UINT nControl, const int nComboWidth, const int nDropHeight, CToolBar& parent) { parent.SetButtonInfo(nButton, nControl, TBBS_SEPARATOR, nComboWidth); // Get the dimensions of our new toolbare CRect rectComboPlacement; parent.GetItemRect(nButton, &rectComboPlacement); rectComboPlacement.bottom = rectComboPlacement.top + nDropHeight; // Create the combo on the toolbar if (!ctl.Create(CBS_DROPDOWNLIST | WS_VSCROLL | CBS_AUTOHSCROLL | WS_VISIBLE | WS_TABSTOP, rectComboPlacement, &parent, nControl)) { TRACE("Failed to create Toolbar Combo\n"); return FALSE; } // Set the font for the Combo to the same as the dialog default ctl.SetFont(parent.GetFont()); return TRUE; } Then in CMainFrame::Create() add the following call at the end: if (AddComboToToolbar(5, m_ctlModeBox, ID_MY_COMBO, 90, 75, m_wndToolBar)) { // add strings to combo box } else { return -1; // fail to create } That should do the trick. Dean ========================================================== Dean Grimm Software Engineer / Cortron corp. -----From: "Sorin Jianu" Hi! There is a fine MFC sample about it. The CTRLBARS sample illustrates a wide variety of customization options for control bars including a custom toolbar with a combo box as a child. Sorin
| Вернуться в корень Архива |