How to dynamically create a menu

xiaoxiao2021-03-06  43

Reposted: Dynamic Creating Menu Full Contact Dynamic Creating Menu Full Contact Tall Red Rock (Gheghost)

Basic understanding: In the Delphi program development environment, the packaged VCL reduces our many development work, which makes the development in the interface are very fast, but in a lot, we need to design visual users. Interface, and in the operation of the program, then we have to use Delphi to provide us to complete the work we need, the author will discuss the basic knowledge of the "Menu" with friends. I hope this article gives the role of those who have just started to get into the jade. In Delphi's menu design, there are two Delphi menu controls: 1: TmainMenu; 2: TPopupMenu; the former is the menu of the creation window, the latter is the control of the right key pop-up menu, but in the Delphi huge library A class is closely related to these two controls, which is: TMENUITEM, window menu and Right-click Each entry of the pop-up menu is an object of the TMenuItem class. This TMENUITEM class does not appear on the control board, and the code can be created in the program. Basic knowledge: In TMAINMENU, there is an attribute in the TPopUpMenu control is items, this property is an array type, the parameter inside is the index value of the menu item. File editing View Insert format help ---- ---- ---- ---- ---- --- New Undo Ruler Object Font About Open Copy Source Code Color

I believe that you have seen the above menu is very familiar, in this menu, the index value of the menu head is as follows: "File" menu of the items value is 0; "Edit" menu Items value is 1; Such push. The items attribute is the TMenuItem type, and there is still an attribute in this type, which is items. If you know the relationship between "class", you don't even understand this similar "nested" relationship. The "New" menu option is the subclass of the File menu option, which is represented as TmainMenu.Items [0] .Items [0], "Open" menu option is TmainMenu.Items [0] .Items [1], Such push, and the code representing the "Copy" menu option in the Edit menu is TMAINMENU.Items [1] .Items [1], other menu code indicates this type.

Basic example: After knowing the Items structure of the menu, we can further boldly create its own orderly menu. The menu of the window and the right-click pop-up menu is an object of the TMenuitem class. Then we can create it for an instance of it, to add the menu you want. Example process: 1: New project. 2: Add a TMAINMENU control. 3: Add a Button control, and write the following code in the Button's OnClick event: procedure tForm1.button1click (sender: TOBJECT); varFiles, Edit: tMenuItem; {There is an instance declaration} beginfiles: = tmenuitem.create (Self) ); edit: = tMenuitem.create (self); files.caption: = 'file'; edit.caption: = 'editing'; mainmenu1.autohotkeys: = mamanual; {This sentence code is not allowed to automatically set shortcuts} Form1.mainMenu1.items.add (file); Form1.mainMenu1.items.add (edit); end; after running, some structures of the menu, as described above, this seems to be very simple to dynamically create menu items This undoubtedly encapsulates the system's function in Delphi. The menu head we created, followed by the menu item in the creation menu, by "Items attribute is the TMenuItem type, and there is an attribute in this type, it means that this sentence means that we can create a menu item, The code is as follows: 1: New project. 2: Add a TMAINMENU control. 3: Add a Button control and write the following code in the Button's OnClick event: Procedure TFORM1.BUTTON1Click (Sender: Tobject); Varfiles, Edit: TMenuItem; New, Copy: tMenuItem; BeginFiles: = tMenuItem.create (Self) Edit: = TMENUITEM.CREATE (Self); files.caption: = 'file'; edit.caption: = 'editing'; mainmenu1.autohotkeys: = mamanual; {This sentence code is not allowed to automatically set shortcuts} Form1 .MainMenu1.items.add (file1.mainMenu1.items.add (edit); {Upper code is created menu head} new: = tMenuitem.create (self); new; new .Caption: = 'New'; copy.caption: = 'copy'; files.add (new); edit.add (copy); {Upper code is created in the menu item} END; running effect and the above menu structure table Same, but at this point, when you click on the menu item, it is obvious that such software sales is not ideal, we can inevit the code to add an event. The code is as follows: 1: New project. 2: Add a TMAINMENU control. 3: PrivateProcedure ABC (Sender: TOBJECT); {Private Declarations} Varform1: TFORM1; Implementation

{$ R * .dfm}

procedure tform1.abc (sender: tobject); beginshowmessage ( 'welcome you click me !! :) xixi'); end; procedure TForm1.Button1Click (Sender: TObject); varfiles, edit: tmenuitem; new, copy: tmenuitem; beginfiles : = tMenuitem.create (self); edit: = tMenuitem.create (self); files.caption: = 'file'; edit.caption: = 'editing'; mainmenu1.autohotkeys: = mamanual; {This sentence code is not Let the system automatically set shortcuts} form1.mainMenu1.Items.add (files); form1.mainMenu1.items.add (edit); {Upper code is created in the menu head} new: = tMenuitem.create (self); copy: = TMENUITEM.CREATE (Self); new.caption: = 'New'; copy.caption: = 'copy'; copy.onclick: = Abc; new.add (new); edit.add; edit.add Copy; {The upper code is created in the menu item} END; then this software has a function of interacting. Sometimes there is a horizontal line and a menu that appears in the menu item. Then how the effect is implemented with the code, the following is the code example: 1: New project. 2: Add a TMAINMENU control.

3: procedure TForm1.Button1Click (Sender: TObject); varfiles, edit: tmenuitem; new, copy: tmenuitem; sub1, sub2, sub3, lines: tmenuitem; beginfiles: = tmenuitem.Create (self); edit: = tmenuitem.create (self); files.caption: = 'file'; edit.caption: = 'editing'; mainmentu1.autohotkeys: = mamanual; {This sentence code is not allowed to automatically set shortcuts} form1.mainMenu1.Items.Add ( Form1.mainMenu1.Items.add (edit); {Upper code is created by menu head} new: = tMenuitem.create (self); copy: = tmenuitem.create (self); new.caption: = 'New' Copy.caption: = 'copy'; files.add (new); Edit.Add (copy); {Upper code is created menu item} Sub1: = tMenuItem.create (SUB2: = TMENUIIIIIIIIIIITEM.CREATE (Self) ); SUB3: = tMenuitem.create (self); lines: = tMenuItem.create (self); lines.caption: = '-'; sub1.caption: = 'submenu 1'; sub2.caption: = 'submenu 2 '; Sub3.caption: =' submenu 3 '; new.add (sub1); new.add (sub3); copy.add (sub2); {above code A multi-item submenu And the effect of the horizontal line} END; the content of this discussion is to end, the creation of the menu is very simple in Delphi, there are many events and methods and properties in the TMenuItem class, such as Add, Clear, Click, The use of Create, Delete, Destroy, Find, IndexOf, Insert, Remove is very simple, and I wish all friends health. PopupMenu1: = TPopupMenu.create (self); with popupmenu1.Items do begin add (NewItem ('first menu', 0, false, true, menuitem1click, 0, 'menuitem1')); add ('second Menu ", 0, False, True, Menuitem2click, 0, 'Menuitem2'); Add ('Third Menu', 0, False, True, Menuitem3Click, 0, 'Menuitem3'); add (newline); // Add one Split Stick Add (NewItem ('Fourth Menu', 0, False, True, Menuitem4Click, 0, 'Menuitem4');

转载请注明原文地址:https://www.9cbs.com/read-72659.html

New Post(0)