Menus with historical information Instance description
In some software, you can record opened file information in the menu bar to make operations simple and convenient. When you want to open a file that has been opened, you do not need to repeat the search. You only need to select the file that has been opened in the menu to open the file. Run this example, as shown in figure 1.
Figure 1 menus with historical information
Ideas and technologies
This example uses the dynamic menu insertion technology. By using the dynamic menu insertion technology, you can complete the function of recording opened documents in the menu, that is, when a file is opened, insert the complete path of the file to the corresponding menu location. The insert () method of the tmainmenu component is used. In the onchange event of the menu, specify the corresponding event for the menu item. The code for dynamically inserting a menu item is as follows:
//------------------------------------------------------------------------------procedure TForm1.mni_OpenClick(Sender: TObject);
var
menuItem:TMenuItem;
begin
menuItem:=TMenuItem.Create(nil);
With TOpenDialog.Create(nil) do
begin
if Execute then
begin
menuItem.Caption:=FileName;
MainMenu1.Items.Items[0].Items[2].Insert(0,menuItem);
end;
end;
end;
0 insert configuration to open the file path menu
1. Click this menu to return to the path of this menu.
2. Add one more configuration to save
3. Read configuration information
Note: To dynamically insert a menu item, you must first create a tmenuitem menu object.
Development procedure
(1) create a new project. The project automatically creates a main form named form1 by default.
(2) Add the tmainmenu component and topendialog component to the form.
(3) complete program code is as follows:
UnitUnit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, menus, stdctrls;
Type
Tform1 =
Class(Tform)
Mainmenu1: tmainmenu;
Mni_file: tmenuitem;
Mni_open: tmenuitem;
N3: tmenuitem;
Mni_close: tmenuitem;
N5: tmenuitem;
Button1: tbutton;
Mni_recent: tmenuitem;
ProcedureMni_openclick (Sender: tobject );
ProcedureMni_closeclick (Sender: tobject );
ProcedureMainmenu1change (Sender: tobject; Source: tmenuitem;
Rebuild: Boolean );
ProcedureButton1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
ProcedureMenuitemexecute (Sender: tobject );
End;
VaR
Form1: tform1;
Implementation
{$ R *. DFM}
//------------------------------------------------------------------------------
ProcedureTform1.mni _ openclick (Sender: tobject );
VaR
Menuitem: tmenuitem;
Begin
Menuitem: = tmenuitem. Create (
Nil);
WithTopendialog. Create (
Nil)
Do
Begin
IfExecute
Then
Begin
Menuitem. Caption: = filename;
Mainmenu1.items. items [0]. items [2]. insert (0, menuitem );
End;
End;
End;
//------------------------------------------------------------------------------
ProcedureTform1.mni _ closeclick (Sender: tobject );
Begin
Close;
End;
//------------------------------------------------------------------------------
ProcedureTform1.mainmenu1change (Sender: tobject; Source: tmenuitem;
Rebuild: Boolean );
VaR
I: integer;
Begin
ForI: = 2
ToMainmenu1.items. items [0]. Count-3
Do
Begin
Mainmenu1.items. items [0]. items [I]. onclick: = button1.onclick;
End;
End;
//------------------------------------------------------------------------------
// Return the title of the current click
ProcedureTform1.menuitemexecute (Sender: tobject );
Begin
Showmessage (tmenuitem (sender). Caption );
End;
//------------------------------------------------------------------------------
ProcedureTform1.button1click (Sender: tobject );
Begin
Menuitemexecute (sender );
End;
//------------------------------------------------------------------------------
End.
From Weizhi note (wiz)
Attachment List