结构定义:
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;