This article explains how to put various controls like combo box or edit box on toolbars.
The simplest way to add controls to the toolbar, is using our Toolbar Editor application: https://bcgsoft.com/Support/DevArea/ToolbarEditor
For example, you want to create a "Find" combo box, which appears on a toolbar and contains the recently used search strings. The user will be able to type the strings in the combo box edit control and then either press the enter key to search a document, or press the escape key to return the focus to the main frame. The document is displayed in a CEditView - derived view.
The following steps is the "setup" work that deals with the creation of the combo box button and handling of its commands:
- Open the application resources, add a new button with the ID_EDIT_FIND command ID to the desired (IDR_MAINFRAME) toolbar and create a new menu item with the same ID. Add a new string "Find the text\nFind" to the string table with ID_EDIT_FIND_COMBO command ID. This ID will be used as the command ID of the find combo box button. Note. The ID_EDIT_FIND is the standard command processed by CEditView, therefore you don't need to implement a special handler for this command, but you need to implement a handler for the ID_EDIT_FIND_COMBO command.
- Derive a class from the CBCGPComboBox class. Let's name it CFindComboBox.
- In the CFindComboBox class override the PreTranslateMessage virtual member function. This will allow you to process WM_KEYDOWN message first and take the appropriate actions. When the user hits the escape key (VK_ESCAPE) return the focus to the main frame window. When the user hits the enter key (VK_ENTER) post the WM_COMMAND message with the ID_EDIT_FIND_COMBO command ID to the main frame window (the command will be routed to the view).
- Derive a class from the CBCGPToolbarComboBoxButton class. Let's name it CFindComboButton.
- Constructor of CBCGPToolbarComboBoxButton takes three parameters: the button's command ID, index of the button's image and the combo box style. You should pass the ID_EDIT_FIND_COMBO as the command ID and you can use CImageHash::GetImageOfCommand with ID_EDIT_FIND to obtain the image index.
- Override the CBCGPToolbarComboBoxButton::CreateCombo member function. Here you should create the CFindComboBox object and return a pointer to it.
- Use IMPLEMENT_SERIAL macro to make the combo button persistent. The workspace manager automatically loads and saves the button's state from/to Windows registry.
- Implement the ID_EDIT_FIND_COMBO handler in your view. Use CBCGPToolBar::GetCommandButtons with the ID_EDIT_FIND_COMBO ID to retrieve all "Find" combo box buttons. It can be several copies of a combo box button with the same command ID because of customization.
- Use CBCGPToolBar::IsLastCommandFromButton to determine whether the "Find" command was sent from the our combo box button. If so, find the text and add the search string to the combo box.
You should perform the following steps to put the find combo box button to the toolbar:
- Implement the BCGM_RESETTOOLBAR message handler (OnToolbarReset) in the main frame window. Note. This message is sent by the framework to the main frame window when a toolbar has been initialized (on the application startup), or a toolbar has been reset during customization. In either case you need to replace the standard toolbar button with the "custom" find combo box button (see step 2).
- In the OnToolbarReset handler analyze the toolbar's ID (it is WPARAM of the BCGM_RESETTOOLBAR message). When you encounter the IDR_MAINFRAME toolbar just call CBCGPToolBar::ReplaceButton with ID_EDIT_FIND and a reference to a CFindComboButton object. Note. You can construct a CFindComboButton object on the stack, because ReplaceButtoncopies the button object and maintains the copy.
- If you enable customization, you implement the "customize" handler (OnViewCustomize) and create CBCGPToolbarCustomize dialog. You must call CBCGPToolbarCustomize::ReplaceButton with ID_EDIT_FIND and a reference to a CFindComboButton object. Note. The customization dialog contains the "Commands" page with the "Commands" list box. The user is able to drag the commands and drop them on the toolbars. By default, the customization dialog processes the application menus and builds lists of the standard toolbar buttons for each category. If you don't replace the standard toolbar button by the CBCGPToolbarButton - derived button when needed, you will lose the extended functionality provided by derived objects.