结构定义:
TMenuInfos=packed record
MenuID :Integer;
MenuCaption :string[60];
end;
TMenuInfosArray=array[0..1999] of TMenuInfos;
例子中将遍历到菜单保存到结构体数组中:
递归函数:
//参数说明:AArray结构体数组,AMenuItem某菜单项
procedure GetMenuInfos(AArray: TMenuInfosArray; const AMenuItem: TMenuItem);
var
i :Integer;
begin
if AMenuItem.Count > 1 then
begin
for i:=0 to AMenuItem.Count - 1 do
GetMenuInfos(AArray, AMenuItem.Items[i])
end
else
begin
AArray[AMenuItem.Command].MenuID := AMenuItem.Command;//菜单项ID
AArray[AMenuItem.Command].MenuCaption := AMenuItem.Caption;//菜单项caption
end;
end;
遍历主菜单及弹出菜单:
定义:FMenuInfosArray :TMenuInfosArray;
procedure TForm1.Button2Click(Sender: TObject);
var
i, j :Integer;
begin
for i:=0 to form1.ComponentCount - 1 do
if form1.Components[i] is TMenu then
begin
for j:=0 to (form1.Components[i] as TMenu).Items.Count - 1 do
GetMenuInfos(FMenuInfosArray, (form1.Components[i] as TMenu).Items[j]);
end
else if form1.Components[i] is TPopupMenu then
begin
for j:= 0 to (form1.Components[i] as TPopupMenu).Items.Count - 1 do
GetMenuInfos(FMenuInfosArray, (form1.Components[i] as TPopupMenu).Items[j]);
end;
end;
本文介绍了一种使用Delphi语言遍历并存储菜单信息的方法。通过定义特定的数据结构TMenuInfos来保存菜单ID和标题,并利用递归函数GetMenuInfos实现对主菜单及其子菜单的遍历。此方法同样适用于处理弹出菜单。

548

被折叠的 条评论
为什么被折叠?



