Popup Menus |
|
Popu Menus |
Introduction |
A popup menu is one that appears when the user right-clicks an area of an application or form. In most applications, when the user right-clicks a title bar, the operating system is configured to display a system menu. Here is an example: |
|
A menu is considered, or qualifies as, popup if, or because, it can appear anywhere on the form as the programmer wishes. Such a menu is also referred to as context-sensitive or contextual because its appearance and behavior depend on where it displays on the form or on a particular control. The person who creates the application decides if or where the contextual menu would appear. Because this characteristic is up to the programmer, the same application can display different types of popup menus depending on where the user right-clicks. Here are examples:
The first difference between a main menu and a popup menu is that a popup menu appears as one category or one list of items and not like a group of categories of menus like a main menu. Secondly, while a main menu by default is positioned on the top section of a form, a popup menu doesn't have a specific location on the form.
To support the creation and management of popup menus, the VCL provides the TPopupMenu class. This class is derived from TMenu. To visually create a contextual menu, in the Standard section of the Tool Palette, click the TPopupMenu button and click the form. Once you have a TPopupMenu object, you can create its menu items. To do this, as mentioned for the TMainMenu, right-click the TPopupMenu object and click Menu Designer... Configure the items using the Object Inspector. To programmatically create a popup menu, declare a variable of type TPopupMenu. Specify its owner in its constructor. Here is an example: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TPopupMenu *mnuContext = new TPopupMenu(this);
}
//---------------------------------------------------------------------------
Unlike a main menu, a popup menu provides a single list of items. If you want different popup menus for your form, you have two options. You can create various popup menus or programmatically change your single popup menu in response to something or some action on your form. Each item of a popup menu is an object of type TMenuItem. Use it as reviewed before to create a menu item. After creating a menu item, to add it to the popup menu, use its Items property that is equipped with an Add Method. Here are examples: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { TPopupMenu *mnuContext = new TPopupMenu(this); TMenuItem *mnuFileNew = new TMenuItem(this); TMenuItem *mnuFileOpen = new TMenuItem(this); mnuFileNew->Caption = L"New"; mnuContext->Items->Add(mnuFileNew); mnuFileOpen->Caption = L"Open"; mnuContext->Items->Add(mnuFileOpen); } //-------------------------------------------------------------------------- |
After creating a popup menu, in order to use it, you must assign it to the control that will use it. To support this, the Window controls are equipped with a property named PopupMenu. Assign the TPopupMenu object to this property. Here is an example: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TPopupMenu *mnuContext = new TPopupMenu(this);
TMenuItem *mnuFileNew = new TMenuItem(this);
TMenuItem *mnuFileOpen = new TMenuItem(this);
mnuFileNew->Caption = L"New";
mnuContext->Items->Add(mnuFileNew);
mnuFileOpen->Caption = L"Open";
mnuContext->Items->Add(mnuFileOpen);
pnlSomething->PopupMenu = mnuContext;
}
//---------------------------------------------------------------------------
This would produce:
In your application, you can create as many popup menus as you want. If you have different controls, each can have its own popup menu or many can share a popup menu. Also, you can use different contextual menus for a control and decide what menu to display when/why. There is nothing particularly specific with writing code for a popup menu item. You approach it exactly as if you were dealing with a menu item of a main menu. You can write code for an item of a popup menu independent of any other item of a main menu. If you want an item of a popup menu to respond to the same request as an item of a main menu, you can write code for one of the menu items (either the item on the main menu or the item on the popup menu) and simply call its OnClick event in the event of the other menu item. |
|
|||||||||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|