Skip to content

Commit c45cd64

Browse files
authored
Merge pull request #5434 from Rageking8/fix-misplaced-parenthesis-and-update-walkthrough-putting-controls-on-toolbars
Fix misplaced parenthesis and update "Walkthrough: Putting Controls On Toolbars"
2 parents b5b65aa + 1c761e2 commit c45cd64

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed
Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
---
2-
description: "Learn more about: Walkthrough: Putting Controls On Toolbars"
32
title: "Walkthrough: Putting Controls On Toolbars"
4-
ms.date: "04/25/2019"
3+
description: "Learn more about: Walkthrough: Putting Controls On Toolbars"
4+
ms.date: 04/25/2019
55
helpviewer_keywords: ["Customize dialog box, adding controls", "toolbars [MFC], adding controls"]
6-
ms.assetid: 8fc94bdf-0da7-45d9-8bc4-52b7b1edf205
76
---
87
# Walkthrough: Putting Controls On Toolbars
98

10-
This article describes how to add a toolbar button that contains a Windows control to a toolbar. In MFC, a toolbar button must be a [CMFCToolBarButton Class](../mfc/reference/cmfctoolbarbutton-class.md)-derived class, for example [CMFCToolBarComboBoxButton Class](../mfc/reference/cmfctoolbarcomboboxbutton-class.md), [CMFCToolBarEditBoxButton Class](../mfc/reference/cmfctoolbareditboxbutton-class.md), [CMFCDropDownToolbarButton Class](../mfc/reference/cmfcdropdowntoolbarbutton-class.md), or [CMFCToolBarMenuButton Class](../mfc/reference/cmfctoolbarmenubutton-class.md).
9+
This article describes how to add a toolbar button that contains a Windows control to a toolbar. In MFC, a toolbar button must be a [`CMFCToolBarButton` Class](reference/cmfctoolbarbutton-class.md)-derived class, for example [`CMFCToolBarComboBoxButton` Class](reference/cmfctoolbarcomboboxbutton-class.md), [`CMFCToolBarEditBoxButton` Class](reference/cmfctoolbareditboxbutton-class.md), [`CMFCDropDownToolbarButton` Class](reference/cmfcdropdowntoolbarbutton-class.md), or [`CMFCToolBarMenuButton` Class](reference/cmfctoolbarmenubutton-class.md).
1110

1211
## Adding Controls to Toolbars
1312

@@ -21,7 +20,7 @@ To add a control to a toolbar, follow these steps:
2120

2221
1. Construct the button control by using a `CMFCToolbarButton`-derived class.
2322

24-
1. Replace the dummy button with the new control by using [CMFCToolBar::ReplaceButton](../mfc/reference/cmfctoolbar-class.md#replacebutton). You can construct the button object on the stack, because `ReplaceButton` copies the button object and maintains the copy.
23+
1. Replace the dummy button with the new control by using [`CMFCToolBar::ReplaceButton`](reference/cmfctoolbar-class.md#replacebutton). You can construct the button object on the stack, because `ReplaceButton` copies the button object and maintains the copy.
2524

2625
> [!NOTE]
2726
> If you enabled customization in your application, you may have to reset the toolbar by using the **Reset** button on the **Toolbars** tab of the **Customize** dialog box to see the updated control in your application after recompiling. The toolbar state is saved in the Windows registry, and the registry information is loaded and applied after the `ReplaceButton` method is executed during application startup.
@@ -30,11 +29,11 @@ To add a control to a toolbar, follow these steps:
3029

3130
The **Commands** tab of the **Customize** dialog box contains a list of commands that are available in the application. By default, the **Customize** dialog box processes the application menus and builds a list of standard toolbar buttons in each menu category. To keep the extended functionality that the toolbar controls provide, you must replace the standard toolbar button with the custom control in the **Customize** dialog box.
3231

33-
When you enable customization, you create the **Customize** dialog box in the customization handler `OnViewCustomize` by using the [CMFCToolBarsCustomizeDialog Class](../mfc/reference/cmfctoolbarscustomizedialog-class.md) class. Before you display the **Customize** dialog box by calling [CMFCToolBarsCustomizeDialog::Create](../mfc/reference/cmfctoolbarscustomizedialog-class.md#create), call [CMFCToolBarsCustomizeDialog::ReplaceButton](../mfc/reference/cmfctoolbarscustomizedialog-class.md#replacebutton) to replace the standard button with the new control.
32+
When you enable customization, you create the **Customize** dialog box in the customization handler `OnViewCustomize` by using the [`CMFCToolBarsCustomizeDialog` Class](reference/cmfctoolbarscustomizedialog-class.md) class. Before you display the **Customize** dialog box by calling [`CMFCToolBarsCustomizeDialog::Create`](reference/cmfctoolbarscustomizedialog-class.md#create), call [`CMFCToolBarsCustomizeDialog::ReplaceButton`](reference/cmfctoolbarscustomizedialog-class.md#replacebutton) to replace the standard button with the new control.
3433

3534
## Example: Creating a Find Combo Box
3635

37-
This section describes how to create a **Find** combo box control that appears on a toolbar and contains recent-used search strings. The user can type a string in the control and then press the enter key to search a document, or press the escape key to return the focus to the main frame. This example assumes that the document is displayed in a [CEditView Class](../mfc/reference/ceditview-class.md)-derived view.
36+
This section describes how to create a **Find** combo box control that appears on a toolbar and contains recent-used search strings. The user can type a string in the control and then press the enter key to search a document, or press the escape key to return the focus to the main frame. This example assumes that the document is displayed in a [`CEditView` Class](reference/ceditview-class.md)-derived view.
3837

3938
### Creating the Find Control
4039

@@ -46,32 +45,32 @@ First, create the **Find** combo box control:
4645

4746
1. Create a new menu item with the `ID_EDIT_FIND` command ID.
4847

49-
1. Add a new string "Find the text\nFind" to the string table and assign it an `ID_EDIT_FIND_COMBO` command ID. This ID will be used as the command ID of the **Find** combo box button.
48+
1. Add a new string `"Find the text\nFind"` to the string table and assign it an `ID_EDIT_FIND_COMBO` command ID. This ID will be used as the command ID of the **Find** combo box button.
5049

5150
> [!NOTE]
52-
> Because `ID_EDIT_FIND` is a standard command that is processed by `CEditView`, you are not required to implement a special handler for this command. However, you must implement a handler for the new command `ID_EDIT_FIND_COMBO`.
51+
> Because `ID_EDIT_FIND` is a standard command that is processed by `CEditView`, you are not required to implement a special handler for this command. However, you must implement a handler for the new command `ID_EDIT_FIND_COMBO`.
5352
54-
1. Create a new class, `CFindComboBox`, derived from [CComboBox Class](../mfc/reference/ccombobox-class.md).
53+
1. Create a new class, `CFindComboBox`, derived from [`CComboBox` Class](reference/ccombobox-class.md).
5554

56-
1. In the `CFindComboBox` class, override the `PreTranslateMessage` virtual method. This method will enable the combo box to process the [WM_KEYDOWN](/windows/win32/inputdev/wm-keydown) message. If the user hits the escape key (`VK_ESCAPE`), return the focus to the main frame window. If the user hits the Enter key (`VK_ENTER`), post to the main frame window a `WM_COMMAND` message that contains the `ID_EDIT_FIND_COMBO` command ID.
55+
1. In the `CFindComboBox` class, override the `PreTranslateMessage` virtual method. This method will enable the combo box to process the [`WM_KEYDOWN`](/windows/win32/inputdev/wm-keydown) message. If the user hits the escape key (`VK_ESCAPE`), return the focus to the main frame window. If the user hits the Enter key (`VK_ENTER`), post to the main frame window a `WM_COMMAND` message that contains the `ID_EDIT_FIND_COMBO` command ID.
5756

58-
1. Create a class for the **Find** combo box button, derived from [CMFCToolBarComboBoxButton Class](../mfc/reference/cmfctoolbarcomboboxbutton-class.md). In this example, it's named `CFindComboButton`.
57+
1. Create a class for the **Find** combo box button, derived from [`CMFCToolBarComboBoxButton` Class](reference/cmfctoolbarcomboboxbutton-class.md). In this example, it's named `CFindComboButton`.
5958

6059
1. The constructor of `CMFCToolbarComboBoxButton` takes three parameters: the command ID of the button, the button image index, and the style of the combo box. Set these parameters as follows:
6160

6261
1. Pass the `ID_EDIT_FIND_COMBO` as the command ID.
6362

64-
1. Use [CCommandManager::GetCmdImage](reference/internal-classes.md) with `ID_EDIT_FIND` to get the image index.
63+
1. Use [`CCommandManager::GetCmdImage`](reference/internal-classes.md) with `ID_EDIT_FIND` to get the image index.
6564

66-
1. For a list of available combo box styles, see [Combo-Box Styles](../mfc/reference/styles-used-by-mfc.md#combo-box-styles).
65+
1. For a list of available combo box styles, see [Combo-Box Styles](reference/styles-used-by-mfc.md#combo-box-styles).
6766

6867
1. In the `CFindComboButton` class, override the `CMFCToolbarComboBoxButton::CreateCombo` method. Here you should create the `CFindComboButton` object and return a pointer to it.
6968

70-
1. Use the [IMPLEMENT_SERIAL](../mfc/reference/run-time-object-model-services.md#implement_serial) macro to make the combo button persistent. The workspace manager automatically loads and saves the button's state in the Windows registry.
69+
1. Use the [`IMPLEMENT_SERIAL`](reference/run-time-object-model-services.md#implement_serial) macro to make the combo button persistent. The workspace manager automatically loads and saves the button's state in the Windows registry.
7170

72-
1. Implement the `ID_EDIT_FIND_COMBO` handler in your document view. Use [CMFCToolBar::GetCommandButtons](../mfc/reference/cmfctoolbar-class.md#getcommandbuttons) with `ID_EDIT_FIND_COMBO` to retrieve all **Find** combo box buttons. There can be several copies of a button with the same command ID because of customization.
71+
1. Implement the `ID_EDIT_FIND_COMBO` handler in your document view. Use [`CMFCToolBar::GetCommandButtons`](reference/cmfctoolbar-class.md#getcommandbuttons) with `ID_EDIT_FIND_COMBO` to retrieve all **Find** combo box buttons. There can be several copies of a button with the same command ID because of customization.
7372

74-
1. In the `ID_EDIT_FIND` message handler `OnFind`, use [CMFCToolBar::IsLastCommandFromButton](../mfc/reference/cmfctoolbar-class.md#islastcommandfrombutton) to determine whether the find command was sent from the **Find** combo box button. If so, find the text and add the search string to the combo box.
73+
1. In the `ID_EDIT_FIND` message handler `OnFind`, use [`CMFCToolBar::IsLastCommandFromButton`](reference/cmfctoolbar-class.md#islastcommandfrombutton) to determine whether the find command was sent from the **Find** combo box button. If so, find the text and add the search string to the combo box.
7574

7675
### Adding the Find Control to the Main Toolbar
7776

@@ -82,20 +81,20 @@ To add the combo box button to the toolbar, follow these steps:
8281
> [!NOTE]
8382
> The framework sends this message to the main frame window when a toolbar is initialized during application startup, or when a toolbar is reset during customization. In either case, you must replace the standard toolbar button with the custom **Find** combo box button.
8483
85-
1. In the `AFX_WM_RESETTOOLBAR` handler, examine the toolbar ID, that is, the *WPARAM* of the AFX_WM_RESETTOOLBAR message. If the toolbar ID is equal to that of the toolbar that contains the **Find** combo box button, call [CMFCToolBar::ReplaceButton](../mfc/reference/cmfctoolbar-class.md#replacebutton) to replace the **Find** button (that is, the button with the command ID `ID_EDIT_FIND)` with a `CFindComboButton` object.
84+
1. In the `AFX_WM_RESETTOOLBAR` handler, examine the toolbar ID, that is, the *`WPARAM`* of the `AFX_WM_RESETTOOLBAR` message. If the toolbar ID is equal to that of the toolbar that contains the **Find** combo box button, call [`CMFCToolBar::ReplaceButton`](reference/cmfctoolbar-class.md#replacebutton) to replace the **Find** button (that is, the button with the command ID `ID_EDIT_FIND`) with a `CFindComboButton` object.
8685

8786
> [!NOTE]
8887
> You can construct a `CFindComboBox` object on the stack, because `ReplaceButton` copies the button object and maintains the copy.
8988
9089
### Adding the Find Control to the Customize Dialog Box
9190

92-
In the customization handler `OnViewCustomize`, call [CMFCToolBarsCustomizeDialog::ReplaceButton](../mfc/reference/cmfctoolbarscustomizedialog-class.md#replacebutton) to replace the **Find** button (that is, the button with the command ID `ID_EDIT_FIND`) with a `CFindComboButton` object.
91+
In the customization handler `OnViewCustomize`, call [`CMFCToolBarsCustomizeDialog::ReplaceButton`](reference/cmfctoolbarscustomizedialog-class.md#replacebutton) to replace the **Find** button (that is, the button with the command ID `ID_EDIT_FIND`) with a `CFindComboButton` object.
9392

9493
## See also
9594

96-
[Hierarchy Chart](../mfc/hierarchy-chart.md)<br/>
97-
[Classes](../mfc/reference/mfc-classes.md)<br/>
98-
[CMFCToolBar Class](../mfc/reference/cmfctoolbar-class.md)<br/>
99-
[CMFCToolBarButton Class](../mfc/reference/cmfctoolbarbutton-class.md)<br/>
100-
[CMFCToolBarComboBoxButton Class](../mfc/reference/cmfctoolbarcomboboxbutton-class.md)<br/>
101-
[CMFCToolBarsCustomizeDialog Class](../mfc/reference/cmfctoolbarscustomizedialog-class.md)
95+
[Hierarchy Chart](hierarchy-chart.md)\
96+
[Classes](reference/mfc-classes.md)\
97+
[`CMFCToolBar` Class](reference/cmfctoolbar-class.md)\
98+
[`CMFCToolBarButton` Class](reference/cmfctoolbarbutton-class.md)\
99+
[`CMFCToolBarComboBoxButton` Class](reference/cmfctoolbarcomboboxbutton-class.md)\
100+
[`CMFCToolBarsCustomizeDialog` Class](reference/cmfctoolbarscustomizedialog-class.md)

0 commit comments

Comments
 (0)