Customizing the Context Menu and Toolbar of a Visual Component

Most of the functionality provided in ABBYY FineReader Engine Visual Components can be accessed via their context menu and toolbar (if exists). The commands available in the context menu and toolbar are useful in most common usage scenarios. However, you may need to customize their elements to fit your task.

Toolbar is available for Image Viewer, Text Editor, Document Viewer. You can add new buttons to the toolbar and remove excess ones. The buttons can be either predefined ones or the ones you've created.

Context menu is available for Image Viewer, Zoom Viewer, Text Editor, Document Viewer. The context menu of each component has a standard part and a custom part:

  • The standard part of the context menu is the context menu which is displayed in the component by default. This part cannot be changed. You can only display or hide from the display this part of the menu.
  • The custom part of the context menu can be edited. By default, it is empty. You can add any of the standard commands available for the component or any user-defined commands to this part of the context menu. The custom part of the context menu is always displayed after the standard part. The custom part of the context menu can be enabled or disabled too.

Managing available commands

You may find the complete list of commands available in Visual Components in the MenuItemEnum enumeration constants. The Commands subobjects of the ImageViewer, ZoomViewer, DocumentViewer, TextEditor objects provide access to the commands available in each component.

The commands can be called by:

  • selecting an item from a context menu,
  • clicking a toolbar button,
  • pressing a hot key,
  • using the DoCommand method of the Commands subobject of a component.

However, not all these methods are available for each command. See the list of available commands and the ways these commands can be used in each component:

To enable or disable a command, use the EnableCommand or DisableCommand method of the Commands object. If you disable a command, none of the calling methods available for the command works.

Adding predefined commands to the toolbar

Some of the predefined commands are available on the toolbar by default, others can be added to the toolbar of a component by the developer. See the lists of available predefined commands for each component.

To add a predefined command to a toolbar or to remove it:

  1. Obtain the Toolbar object via the Toolbar property of the component.
  2. Insert the command into the selected position in the toolbar using the InsertAt method of the Toolbar object or remove it using the Delete or DeleteAt method.

Note: You can group the buttons on the toolbar using separators. Use the InsertAt method of the Toolbar object with the MI_Separator constant as a parameter.

C# code

After the predefined command has been added, it can be used in the application. ABBYY FineReader Engine handles corresponding events. You can complement the handler with some additional functionality by using the OnCommand event of the ICommandEvents interface.

Adding predefined commands to the context menu

Some of the predefined commands are available in the context menu by default — these commands form the standard part of the context menu. All other predefined commands available in the component can be added to the custom part of the context menu.

To add a predefined command to a custom part of a context menu or to remove it:

  1. Obtain the PopupMenu object via the PopupMenu property of the component.
  2. Disable the whole standard part of the context menu using the StandardMenuEnabled property of the PopupMenu object if you do not need to use it. By default, the standard part is enabled.
  3. Enable the custom part of the context menu using the CustomMenuEnabled property of the PopupMenu object.
  4. Insert a command into the selected position in the custom part of the context menu using the InsertAt method of the PopupMenu object or remove it using the Delete or DeleteAt method.

Note: You can group the items in the custom part of the context menu using separators. Use the InsertAt method of the PopupMenu object with the MI_Separator constant as a parameter.

C# code

After the predefined command has been added, it can be used in the application. ABBYY FineReader Engine handles corresponding events. You can complement the handler with some additional functionality by using the OnCommand event of the ICommandEvents interface.

Creating user-defined commands

You can also create your own commands and add them to a toolbar or context menu. The MenuItemEnum enumeration includes the constants (MI_CustomCommand1, ... MI_CustomCommand10), which can be used to define your own commands.

Before you add a custom command, you need to create an icon (16х16 px) for its toolbar button and think of the text for the command. Then you should register the command in the component and add it to the toolbar or custom part of the context menu. See details below.

To add a user command:

  1. Register the command in the component. Use the RegisterUserCommand method of the Commands subobject of the component with any of the MI_CustomCommand1, ... MI_CustomCommand10 constants as a parameter. You should also specify the path to the icon, the text of the command, and hot key in the parameters of this method.

Note: Hot key consists of two parts: the virtual key code and key modifiers (Ctrl, Alt, Shift). When you pass a hot key to a FineReader Engine method, the hot key should have the same format as the returned value (LRESULT) of the Windows SendMessage function for the HKM_GETHOTKEY message sent. To obtain the hot key value in this format, you should pack the virtual key code of the hot key to the low-order byte of the value and the key modifier that specifies the keys that define a hot key combination to the high-order byte. If you do not need to use a hot key for the command, pass 0 as the parameter of the method.

  1. Implement the OnCommand event of the ICommandEvents interface in order that it handles the event for the new command. See Working with Connectable Objects.
  2. Add the command to the toolbar or custom part of the context menu of the component. The procedure is the same as for predefined commands of a toolbar or context menu.

After this, the command can be used in your application.

C# code

To remove previously added user command:

  1. Remove it from the toolbar or custom part of the context menu. The procedure is the same as for predefined commands of a toolbar or context menu.
  2. Unregister the command. Use the UnregisterUserCommand method of the Commands subobject of the component.

See also

Creating an Application with a Graphical User Interface

9/17/2024 3:14:40 PM

Usage of Cookies. In order to optimize the website functionality and improve your online experience ABBYY uses cookies. You agree to the usage of cookies when you continue using this site. Further details can be found in our Privacy Notice.