Einzelnen Beitrag anzeigen

Nuclear-Ping
(Gast)

n/a Beiträge
 
#2

Re: Markiertes MainMenu-Item in Label ausgeben

  Alt 16. Mai 2008, 22:33
Du musst halt einen Ereignis-Handler für den OnClick-Event machen. Ausserdem warum übergibst du bei TMenuItem.Create das Item selber als Owner ... ?
Delphi-Quellcode:
interface

type
  TForm1 = class (TForm)
    MainMenu1: TMainMenu; // ... oder so, dein Main-Menü halt
    // ...
  private
    procedure CreateTreeMenus (Path : String; Root : TMenuItem);
    procedure MyMenuOnClick (Sender: TObject);
    // ...
  end;

implementation
    
procedure TForm1.CreateTreeMenus (Path : String; Root : TMenuItem);
Var
   SR : TSearchRec;
   Result : Integer;
   Item : TMenuItem;
Begin
    Path := IncludeTrailingBackSlash( Path );
    Result := FindFirst( Path + '*.*', faDirectory, SR );
    While ( Result = 0 ) Do Begin
        If ( ( ( SR.Attr And faDirectory ) <> 0 ) And ( SR.Name <> '.' ) And ( SR.Name <> '..' ) ) Then Begin
            Item := TMenuItem.Create(Root); // wenn du schon ein Root übergibst, nutz es auch
            Item.Caption := SR.Name;
            Item.OnClick := MyMenuOnClick;
            Root.Add( Item );
            CreateTreeMenus( Path + SR.Name, Item );
        End;
        Result := FindNext( SR );
    End;
    SysUtils.FindClose( SR );
End;

procedure TForm1.MyMenuOnClick (Sender: TObject);
begin
  ShowMessage ((Sender as TMenuItem).Caption);
end;
  Mit Zitat antworten Zitat