Delphi Menu Designer

Source: Internet
Author: User
Tags word wrap

The menu contains most of the content of most Windows applications. Some Windows applications do not have menus, but most of them do. Delphi uses Menu Designer to make it easy to create menus. Menu Designer has the following features:

  • You can create a main menu and a pop-up menu (shortcut menu ).
  • Provides onclick events for menu item processing.
  • Insert menu from template or source file.
  • Save the customer menu as a template.

All Menu Designer commands are operated through the shortcut menu or object inspector. For example, the shortcut menu of Menu Designer is displayed:

In most cases, these shortcut menu items can be used by their names, so we will not discuss them further here.

Next we will learn about them by using menu items. First, we will createScratchpadThe application adds a main menu and a shortcut menu.

1. Create a Main Menu

Menu Designer can quickly create any menu. The menu structure of the main menu is composed of the mainmenu component, which is represented by the VCL class tmainmenu. Each item in the menu is a tmenuitem component and encapsulated in the tmenuitem class. You don't have to worry too much about how these classes work together, because Menu Designer makes it easy to create menus. The inheritance relationship between tmainmenu and tmenuitem is as follows:

 

1. Add the main menu to the form

You must first add the mainmenu component to the form.

Note

Now everyone has experience with Delphi. From this point on, you need to scale down some steps that require action. For example, from now on, "place the mainmenu component on the form" instead of "click the standard tag on the component panel, click the mainmenu button, and click the form to place the mainmenu component ." Don't worry. A lot of details will be given before new operations are introduced.

(1) Open the scratchpad project created in the previous lecture.

(2) Place the mainmenu component on the form and display it as an icon. For example, change its name attributeMainmenu. Note that the mainmenu component has few attributes and has no time. All menu work is completed by a single menu item.

(3) double-click the mainmenu icon to display the Menu Designer.

Menu Designer looks like an empty form without a coordinate grid point. It can be adjusted as needed. It is only for convenience and has little to do with how to operate menus in the running stage. In this sense, Menu Designer is waiting to start creating a menu. After creating the first menu, you will find that creating a menu is easy and visible.

2. manually create a menu

Although it is easier to create a file menu, we need to manually create the first menu. Menu Designer always has an empty menu item as the placeholder identifier of a new menu item. When You Start Menu Designer, select an empty project.

(1) Change the name attributeFilemenu.

(2) Click the caption attribute in the object inspector and enter"& File", Press enter to confirm, such:

Note

The & symbol is used to create an underline character for a menu item. The underline character is an accelerator that allows you to use the keyboard + ALT to quickly start the menu item. You can place & characters in any position of the menu item text. For example, if the commonly used text string of the exit menu item is E & xit, X is the acceleration key.

After you enter the File menu above, several events will occur. First, the File menu is displayed in Menu Designer, which also appears on the main menu behind Menu Designer, the other is that the new blank placeholder identifier is added under the File menu you just created (you must click the File menu in Menu Designer to view the placeholder identifier ). The object Inspector displays the empty menuitem component and waits for the input caption and name attributes. For example:

Continue to create menu

(1) Change the name attributeFilenew.

(2) Change the caption attribute& New, Press enter, and a blank item is created in Menu Designer.

(3) Repeat steps (1) and (2)Open,Save,SaveCreate a menu item. For example, don't worry about poor performance. You can always go back and correct the errors.

Tip

Make the menu standard as much as possible. Make sure that the acceleration key (underline) is the same as that of other Windows programs. Remember, there is a ellipsis (…) behind the menu item title (...) Is a dialog box.

 

New Term

A menu separator is a horizontal line that separates menu groups.

It is easy to use Delphi Menu Designer to add separators. What we do is to input hyphens (-) in the caption attribute (-). In "Save ..." The menu is a separator. The name attribute is N1, And the caption attribute is "-".

Note

Menu Designer always provides empty items at the bottom of each pop-up menu and on the right of the menu bar. You cannot and do not need to delete these blank menus. They are only used for Menu Designer and are not displayed on the menu when the program is running.

Now, the File menu is ready. The edit menu and help menu will be created below.

 

3. Insert menu from template

This time we will take an easy approach. First, click the blank menu item on the right of the File menu. Right-click and select"Insert from template...", The insert template dialog box is displayed, for example:

This dialog box displays a series of templates for selection. You can use predefined templates or create your own templates. In this case, we only selectEdit menuClick OK. The Edit menu is immediately inserted into the Menu Designer. For example, it is useless to delete its menu items. We will discuss how to delete the menu items later.

Here you can add the Help menu, click the blank placeholder on the right of the edit menu, and select "insert from template ...", This time, we inserted "Help Menu" and immediately displayed the Help menu in Menu Designer.

Note

You can insert a template to create a pop-up menu, which is as easy as creating a main menu item.

It is really easy to insert from a template. After using Delphi, most users will quickly and easily create menus using their own templates. Although you still need to update the name attribute to a meaningful name, it is much easier to create one by one from Menu Designer.

Note

Insert from resource works the same as insert from template, but it requires a resource file (RC extension) containing a valid menu definition ). You must use the begin/end menu resource syntax and cannot use curly brackets. For example, the following definition is invalid:

MENU_1 MENU{  POPUP "FILE"  {    MENUITEM "OPEN", 100    MENUITEM "ABOUT", 101  }}

The following are valid menu resources:

MENU_1 MENUBEGIN  POPUP "FILE"  BEGIN    MENUITEM "OPEN", 100    MENUITEM "ABOUT", 101  ENDEND

The preceding valid menu resources are only used for menus inserted by insert from resource, rather than general menu resources.

 

4. Delete menu items

The process of creating a Windows application rarely happens to be correct for the first time. Users need new features, the boss puts forward some improvements, and even some features need to be deleted. These changes require frequent updates to the Application menu. For example, the menu of the newly inserted edit template is very long. If some items are not required, they must be deleted without a doubt.

 

(1) Click on the edit menu.

(2) ClickRepeat <command>.

(3) PressDeleteOr select "delete" from the Menu Designer shortcut menu to delete this item. This option disappears and the remaining items are moved up.

(4) Delete the paste special item.

The Edit menu has not been processed completely. Before proceeding, we will mention the features used in Menu Designer. YesShift + ClickAndCTRL + Click. This technique can be used to select files in Windows Resource Manager. Menu Designer supports this technology.

(1) The edit menu should still be displayed. If not, click the edit menu to display the edit menu.

(2) Click the menu itemGo.

(3) press shift and click the menu itemObject, All items between the two have been selected.

(4) press the delete key to delete all items at a time.

(5) Click the Help menu to delete the two items in the middle and retain the items contents and about.

VisibleShift + ClickThe technology is used to quickly delete unwanted menu items.

 

5. Insert menu items

Insert a menu item directly. Click the menu item to insert a new item, and pressInsertOr Select Insert from The Menu Designer shortcut menu. An empty menu item is inserted. You can change the name and caption attributes as before.

Insert an item in the edit menu.

(1) Click Edit to display the edit menu.

(2) ClickFindMenu item.

(3) press the Insert key on the keyboard to insert a new menu item. All items under the new project are moved backward.

(4) Change the name attributeEditselectallAnd change the caption attribute to"Select & all ".

(5) Click the empty placeholder at the bottom of the edit menu to add the menu separator.

Click the placeholder to add a new item and set the name attributeEditwordwrapThe caption attribute is"& Word Wrap".

Now the edit menu looks as follows:

 

6. Move menu items

Menu items can be easily moved as needed. You can move them up or down in the pop-up menu where they are located, or remove the pop-up menu.

There are two ways to move menu items:

  • First, use cut and paste.
  • The second is to drag it to a new position and release it.

Next, let's try to place the Select All menu item under the Undo item. It's easy to move it.

(1) Click Edit to display the edit menu.

(2) Click Select All and drag it to know that the separation line under the Undo item is on.

(3) release the mouse and move the menu item.

This is Delphi, which is very easy.

 

7. modify attributes in batches

Sometimes you want to change the attributes of several menu items at a time. For example, in the scratchpad application, there are several menu items that are not prepared to be completed this time. For example, you are not prepared to support printing or to complete the help system. Therefore, you need to block these menu items.

(1) SelectHelp | Contents.

(2) Change its enabled attributeFalseThis menu item is blocked.

(3) Click the File menu.

(4) Click the print menu item, hold down the Shift key, and click the print setup menu item. Both items are selected.

(5) In object inspector, change their enabled attributeFalse, Both menu items are blocked.

Repeat steps (4) and (5) to block the find and replace menu items in the edit menu.

This method can be used to change a set of menu items at a time. You only need to select the items you want to change and change their attributes as required.

 

8. Add a bitmap to the menu bar.

You can easily add bitmap to the menu. Click on the menu to add a bitmap, and double-click the value column of the bitmap attribute in the object Inspector. The picture editor dialog box is displayed. Select the bitmap to display. A Bitmap can be a single image or a list of images. If you use the image list, set the imageindex attribute to the index number in the image list to be displayed.

Note

In the design phase, the menu project bitmap is not displayed in Menu Designer or the form. You must run a program to view the bitmap menu.

 

9. Create a sub-menu

There is nothing special or requires tips to create a sub-menu. A sub-menu is a single menu item. More menu options can be expanded when you click it. A sub-menu is marked by a right arrow near the menu item title. You can select create submenu from The Menu Designer shortcut menu or press Ctrl + right arrow to create a sub menu. As follows:

When creating a sub-menu, an empty menu item is arranged on the right of the menu item. You can add menu items to the sub-menu to create the main menu. You can also insert a menu template to create a sub-menu. As follows:

 

10. Add shortcuts

You can add shortcut keys for menu items by changing the shortcut Cut attribute in object inspector. The Edit menu that was previously inserted already has an internal shortcut key. For example, the shortcut key of cut is Ctrl + X. If you look at the edit menu, you will see Ctrl + X next to the cut project.

Click on the cut menu, and the value is displayed in the cut attribute of the object inspector.CTRL + x. Click the value column of the shortcut Cut attribute. The drop-down button is displayed. click the button to list all shortcut keys. To set shortcuts for menu items, you only need to select from the table.

The standard shortcut key for the Select All menu item is Ctrl + A, so we add a shortcut key for the Select All menu item.

(1) Select from Menu DesignerEdit | select all.

(2) Click the Cut attribute in object inspector.

(3) select from the listCTRL +, CTRL + A is displayed for select all. As follows:

In this case, all you need to do is to take care of it by using Delphi. You do not need to write any code for the shortcut keys.

 

11. Final work

Download to complete the menu.

First, the word wrap menu item is used to open or close the line feed. When this option is enabled, there is a check mark in front of the word wrap menu item; when this option is disabled, there is no check mark. Set the checked attribute of the Word Wrap menu item to true to indicate that the line feed feature is enabled. As follows:

Another thing is to change the name attribute of all menu items inserted from the template. Default values are used for default inserts. You need to change them to more meaningful names.

(1) ClickEdit | undoMenu item. Change the name attributeEditundo. If the default value is used, you need to add the edit prefix to undo1 and delete the 1 at the end.

(2) You can use the naming convention, but the attributes of cut, copy, paste, find, and replace menu item name must be consistent.Editcut, editcopy, editpaste, editfind, and editreplace.

(3) change the Help menu and change the name attribute of the contents menu itemHelpcontents, Change the name attribute of the about menu itemHelpabout.

This is about to complete the menu. Check it again and make necessary modifications if an error is found. When everything is okay, click the close box to close Menu Designer.

Note

You can double-click any menu item to directly access code editor from Menu Designer. Double-click the menu item to display the onclick event of the item. At this time, you can start to enter the code. In the following illustration, I will write the menu code.

The above code has passed the test in Delphi 7. As of now, we will download the sample code:Scratchpad())..rar

(Please continue with the next lecture)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.