Managing Menu Items |
|
Grouping Menu Items |
Menu Separators |
As we will see in later sections, there are various ways you can make a menu look good and you have many options to configure menu items. One of the ways you can manage menu items is to group them. A menu separator is a horizontal line among some menu items to visually divide them. Here is an example: |
|
There are two reasons you would use a separator. You can use a separator just for aesthetic reasons, to make your menu look good. Another, more valuable reason, is to create groups of menu items and show their belonging together by showing a line separating one group from another. To create a separator, set the caption of its menu item to a simple -. To do this visually, insert a new item in the Menu Designer and, in the Object Inspector, set its Caption to -. To do this programmatically, create a TMenuItem object and specify its caption as -. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { TMainMenu *mnuMain = new TMainMenu(this); TMenuItem *mnuFile = new TMenuItem(mnuMain); TMenuItem *mnuFileNew = new TMenuItem(mnuFile); TMenuItem *mnuSeparator = new TMenuItem(mnuMain); TMenuItem *mnuFileOpen = new TMenuItem(mnuFile); TMenuItem *mnuTools = new TMenuItem(mnuMain); TMenuItem *mnuToolsOptions = new TMenuItem(mnuTools); mnuFile->Caption = L"&File"; mnuFileNew->Caption = L"&New"; mnuFileNew->ShortCut = ShortCut(Word(L'N'), TShiftState() << ssCtrl); mnuFile->Add(mnuFileNew); mnuSeparator->Caption = L"-"; mnuFile->Add(mnuSeparator); mnuFileOpen->Caption = L"&Open"; mnuFileOpen->ShortCut = ShortCut(Word(L'O'), TShiftState() << ssCtrl); mnuFile->Add(mnuFileOpen); mnuMain->Items->Add(mnuFile); mnuTools->Caption = L"&Tools"; mnuToolsOptions->Caption = L"&Options..."; mnuTools->Add(mnuToolsOptions); mnuMain->Items->Add(mnuTools); } //--------------------------------------------------------------------------- This would produce:
If you have menu items that perform similar tasks, you can put them in a group, which you can do using line separators. Another option is to create the menu items in their own group. The group of menu items that are created as children of a parent menu is referred to as a sub-menu. Here is an example of a sub-menu in Microsoft Paint:
To visually create a sub-menu, in the Menu Designer:
You can also create a sub-menu for a menu item that itself is a sub-menu. To programmatically add a sub-menu to a menu item, call the Add() method on that item and pass the TMenuItem object of the sub-menu to it. Here are examples: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { TMainMenu *mnuMain = new TMainMenu(this); TMenuItem *mnuFile = new TMenuItem(mnuMain); TMenuItem *mnuFileNew = new TMenuItem(mnuFile); TMenuItem *mnuFileNewForm = new TMenuItem(mnuFile); TMenuItem *mnuFileNewUnit = new TMenuItem(mnuFile); TMenuItem *mnuFileNewFrame = new TMenuItem(mnuFile); mnuFile->Caption = L"&File"; mnuFileNew->Caption = L"&New"; mnuFile->Add(mnuFileNew); mnuFileNewForm->Caption = L"Fo&rm"; mnuFileNew->Add(mnuFileNewForm); mnuFileNewUnit->Caption = L"&Unit"; mnuFileNew->Add(mnuFileNewUnit); mnuFileNewFrame->Caption = L"Fr&ame"; mnuFileNew->Add(mnuFileNewFrame); mnuMain->Items->Add(mnuFile); } //--------------------------------------------------------------------------- This would produce:
Some applications are meant to display more than one form at the same time, or to optionally display and dismiss some forms some time to time. With this type of application, you may need a menu "witness" that can indicate whether the optional form is currently displaying or not. Some other applications may change their view some time to time. For these reasons and others, you can use the menu to assist the user with identifying an option that is currently available or not. You can do this through a check box or a radio button on a menu item. To assist you with displaying a check box on a menu item, the TMenuItem class is equipped with a property named Checked. If you are visually creating a menu, to show a check mark on a menu item, in the Object Inspector, use its Checked field. The default value of this property is False, which means the menu item is not meant to display a check box. To show a check box, you can set this property to True. When the user has clicked the menu item, you can then programmatically change its value from true to false and vice-versa. When the application is running, to put a check mark on it, the user can click the menu item. If an item is displaying a check mark and the user clicks it, the check mark disappears. In reality, this is not an automatic functionality and it doesn't happen at random: you must configure it.
|
You can make two or more menu items behave like a mutual-exclusive group. When one of the menu items is selected, it displays a round button but the other items in the same group don't. In other words, at one time, only one of the menu items in the group is equipped with a radio button. This behavior is not automatic. There are some steps (really easy) you must follow to configure to get this behavior. To support the behavior of a radio button, the TMenuItem class is equipped with a Boolean property named RadioItem. Its default value is False. Therefore, to apply a scenario of radio buttons on a group of menu items, set each RadioItem property to True. To put a radio mark of a menu item, set its Checked property to True. When you do this, the other menu items of the same group lose their radio mark.
A main menu can be recognized by the number of menu categories it contains. To get you this information, the Items property of the TMenu class is equipped with an integer property named Count. Here is an example of using it: //--------------------------------------------------------------------------- void __fastcall TForm1::mnuCountClick(TObject *Sender) { ShowMessage(L"Number of Menu Items: " + String(mnuMain->Items->Count)); } //---------------------------------------------------------------------------
After creating a few menu categories or a few menu items, you may find out that an item is missing in the sequence. You can then create a new menu before one of your choice:
To assist you with programmatically inserting a new menu, the TMenuItem class is equipped with a method named Insert. Its syntax is: void __fastcall Insert(int Index, Menus::TMenuItem * Item); This first argument is an index that specifies the position that the new menu will occupy among the child items. The first position is 0. The second is 1, and so on. Here is an example: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TMainMenu *mnuMain = new TMainMenu(this);
TMenuItem *mnuFile = new TMenuItem(mnuMain);
TMenuItem *mnuFileNew = new TMenuItem(mnuFile);
TMenuItem *mnuFileSave = new TMenuItem(mnuFile);
TMenuItem *mnuFileOpen = new TMenuItem(mnuFile);
mnuFile->Caption = L"&File";
mnuFileNew->Caption = L"&New";
mnuFile->Add(mnuFileNew);
mnuFileSave->Caption = L"&Save";
mnuFile->Add(mnuFileSave);
mnuFileOpen->Caption = L"&Open";
mnuFile->Insert(1, mnuFileOpen);
mnuMain->Items->Add(mnuFile);
}
//---------------------------------------------------------------------------
This would produce:
You can also use the TMenuItem::Insert() method to create new menu items.
You can visually copy a main menu or a popup menu from one (RAD Studio) application to another. To do this, on the form of the application that holds the desired main menu:
In the other application, click the form that will use the menu:
While designing your menu, you may find out that a menu category or a menu item is in the wrong position. You can then move the menu category or the menu item. To visually move a menu category, in the Menu Designer, click and hold the mouse on the menu category. Drag left or right in the desired direction. While moving the mouse, the cursor would display a small white rectangle:
When the mouse gets to the item that will succeed the new one, release the mouse. When you move a menu category, it moves with all its menu items. To move a menu item, in the Menu Designer, click and hold the mouse on the item. Drag up or down. While moving the mouse, the cursor would display a small white rectangle:
When the mouse gets to the item that will succeed the new one, release the mouse.
A menu item is said to be disabled if the user can see it but cannot use it. The caption of such a menu appears gray. Here are examples:
When a menu item is disabled, the user can position the mouse on it and click but nothing would happen. This indicates that the action of that menu item is not available at that time. The reverse to a disabled menu item is that it is enabled. The user cannot directly enable or disable a menu. You as the application developer decides when (and why) the item would be enabled or disabled. To support the ability to enable or to disable a menu item, the TMenuItem class is equipped with a Boolean property named Enabled. By default, a new menu item you have just created is enabled. This is because the default value of this property is true. To visually enable or disable a menu item, in the Menu Designer, click the menu item. In the Object Inspector, clear the Enabled field to set it to False. To programmatically enable or re-enable it, you can set the value to True. In the same way, to find out the enabled status of a menu item, you can check the value of its Enabled property.
A menu is said to be hidden if it is available behind the scenes but the user cannot see. Since the user cannot see it, he or she cannot use it. The reverse to a hidden menu item is that it is shown to the user. The user cannot directly hide or show a menu item. You as the application developer create a menu item and then decides when, how, and where (and why) the item would be hidden or shown. To support the ability to enable or to disable a menu item, the TMenuItem class is equipped with a Boolean property named Visible. After you have just created a new menu, by default, it is made visible to the user. This is because the default value of the Visible property is set to True. To hide a menu item, you can set its Visible property to false. To show or to reveal it, you can set the value to True. In the same way, to find out whether a menu item is currently displaying, you can check the status of its Visible property.
Instead of disabling or hiding a menu you don't want use anymore, you can simply delete it. If you do this, you cannot programmatically refer to it anymore, unless you re-create it. To visually delete a menu category or a menu item:
In the same way, you can keep deleting one item at a time. To assist you with programmatically deleting a menu item, the TMenuItem class is equipped with a method named Remove. Its syntax is: void __fastcall Remove(Menus::TMenuItem * Item); To call this method, use the menu item that is the parent and pass the menu item as argument. Here is an example: //---------------------------------------------------------------------------
void __fastcall TForm1::btnDeleteMenuItemClick(TObject *Sender)
{
mnuShow->Remove(mnuShowTownhouses);
}
//---------------------------------------------------------------------------
To visually delete the whole main menu or a popup menu, on the form
To help you delete all items, the TMenuItem class is equipped with a method named Clear. Its syntax is: void __fastcall Clear(void); When called, this method will get rid of all the child menu items of the menu item. Here is an example: //---------------------------------------------------------------------------
void __fastcall TForm1::btnDeleteMenuItemClick(TObject *Sender)
{
mnuShow->Clear();
}
//---------------------------------------------------------------------------
|
|
|||||||||||||||||||||||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|