function MenuItemToTreeNode(mMenuItem: TMenuItem; mTreeView: TTreeView; mTreeNode: TTreeNode): Boolean;
var
I: Integer;
vTreeNode: TTreeNode;
begin
Result := False;
if (not Assigned(mTreeView)) or (not Assigned(mMenuItem)) then
Exit;
try
for I := 0 to Pred(mMenuItem.Count) do
if mMenuItem[I].Visible then
begin
vTreeNode := mTreeView.Items.AddChildObject(mTreeNode, StripHotkey(mMenuItem[I].Caption), mMenuItem[I]);
vTreeNode.ImageIndex := mMenuItem[I].ImageIndex;
vTreeNode.SelectedIndex := mMenuItem[I].ImageIndex;
MenuItemToTreeNode(mMenuItem[I], mTreeView, vTreeNode);
end;
except
Exit;
end;
Result := True;
end;
function TreeNodeToMenuItem(mTreeView: TTreeView; mTreeNode: TTreeNode; mMenuItem: TMenuItem): Boolean;
var
I, J, K: Integer;
vMenuItem: TMenuItem;
begin
Result := False;
if (not Assigned(mTreeView)) or (not Assigned(mMenuItem)) then
Exit;
if Assigned(mTreeNode) then
begin
vMenuItem := TMenuItem.Create(mMenuItem.Owner);
vMenuItem.Caption := mTreeNode.Text;
vMenuItem.ImageIndex := mTreeNode.ImageIndex;
vMenuItem.Tag := mTreeNode.Index;
mMenuItem.Add(vMenuItem);
for I := 0 to mTreeNode.Count - 1 do
TreeNodeToMenuItem(mTreeView, mTreeNode[I], vMenuItem);
end
else
begin
K := -1;
for I := 0 to mTreeView.Items.Count - 1 do
begin
for J := mTreeView.Items[I].Level to K do
mMenuItem := mMenuItem.Parent;
vMenuItem := TMenuItem.Create(mMenuItem.Owner);
vMenuItem.Caption := mTreeView.Items[I].Text;
vMenuItem.ImageIndex := mTreeView.Items[I].ImageIndex;
vMenuItem.Tag := I;
mMenuItem.Add(vMenuItem);
mMenuItem := vMenuItem;
K := mTreeView.Items[I].Level;
end;
end;
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MenuItemToTreeNode(MainMenu1.Items, TreeView1, nil);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
TreeNodeToMenuItem(TreeView1, nil, MainMenu1.Items);
end;