如何动态的读取Delphi中的PopupMenu的Caption

2025-05-03 21:41:23
推荐回答(2个)
回答(1):


Procedure TForm1.FormCreate(Sender: TObject);

  Procedure DoAddCaption(MenuItem: TMenuItem; List: TStrings);
  Begin
    If Not MenuItem.IsLine Then
    Begin
      List.Add(MenuItem.Caption);
    End;
  End;

  Procedure GetChildItemCaption(MenuItem: TMenuItem; List: TStrings);
  Var
    I: Integer;
  Begin
    For I := 0 To MenuItem.Count - 1 Do
    Begin
      DoAddCaption(MenuItem[I], List);
      If MenuItem[I].Count > 0 Then
      Begin
        GetChildItemCaption(MenuItem[I], List);
      End;
    End;
  End;
Var
  I: Integer;
  alist: TStringList;
Begin
  alist := TStringList.Create;
  Try
    With PopupMenu1 Do
    Begin
      For I := 0 To Items.Count - 1 Do
      Begin
        DoAddCaption(Items[I], alist);
        If Items[I].Count > 0 Then
        Begin
          GetChildItemCaption(Items[I], alist);
        End;
      End;
    End;
    Memo1.Lines.Text := alist.Text;
    //Application.MessageBox(PChar(alist.Text), 'msg', 64);
  Finally
    alist.Clear;
    alist.Free;
  End;
End;

回答(2):

TPopupMenu类没有Caption属性,无法读取。