How to develop MenuItem

xiaoxiao2021-03-14  189

My small trip is this: all the * .txt of a directory is dynamically based on a Menuitem. For example: C: / test has three .txt, respectively aa.txt, bb.txt, cc.txt. When the "file" function menu is in the user, it will appear.

__________________________ / file (f) --------------------------- | AA | | BB | | CC | --------- - If there is a multi-file DD.txt, the function menu will display more DD.

You ask questions ... The concept and technology of the involvement, including: Dynamic generating objects and events to handle events, but also to find all the files in the specified directory. You should be in my study notes ( http://www.geocities.com/~wchien found the relevant instructions.

The following is an example proposed for this question:

1. Form Design object mnuMain: TMainMenu Left = 4 Top = 4 object mnuFile: TMenuItem Caption = 'file' ShortCut = 0 endendobject Button1: TButton Left = 176 Top = 56 Width = 75 Height = 25 Caption = 'Refresh menu' TabOrder = 0 OnClick = Button1Clickend 2. Program section Unit Unit1;

Interface

Uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Menus, Stdctrls;

type TForm1 = class (TForm) mnuMain: TMainMenu; mnuFile: TMenuItem; Button1: TButton; procedure FormCreate (Sender: TObject); procedure Button1Click (Sender: TObject); private {Private declarations} FDir: string;

Procedure menuclick (sender: TOBJECT); Procedure UpdateFileMenu; public {public declarations}

Var Form1: TFORM1;

IMPLEMENTATION

{$ R * .dfm}

Procedure tform1.formcreate (sender: TOBJECT); begin fdir: = 'C: / TEMP /'; UPDATEFILEMENU; END;

Procedure TForm1.Menuclick (Sender: TOBJECT); Begin if sender is TMenuItem Then Winexec (Pchar ('NOTEPAD.EXE' FDIR TMENUITEM (Sender) .caption), SW_NORMAL);

procedure TForm1.UpdateFileMenu; var i: integer; SearchRec: TSearchRec; iFindResult: integer; mnuNew: TMenuItem; MenuItem chopped for i in the first begin // File: = mnuFile.Count - 1 downto 0 do mnuFile.Items [i ] .Free; // Search one by one .TXT profile ifindresult: = FindFirst (fdir '* .txt', faanyfile, searchrec); while ifindresult = 0 do begin // dynamically generated a TMenuItem mnunew: = tMenuItem.create (Self); with mnunew do begin caption: = searchRec.name; // Taking the file as a caption onclick: = menuClick; // This specifies the OnClick event end; mnufile.add (mnunew); // Add to [file ] This menu item under the IFINDRESULT: = FINDNEXT (SearchRec); // Continue to find a .txt file end; end;

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT); begin updateFileMenu;

End.

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

New Post(0)