Einzelnen Beitrag anzeigen

marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#2

Re: TreeView Items in Popupmenu übertragen

  Alt 16. Aug 2005, 20:38
Hi API,

dein Code war doch schon fast fertig - warum hast du aufgehört?

Delphi-Quellcode:
function TDemoForm.NodePath(node: TTreeNode): string;
begin
  Result := node.Text;
  if Assigned(node.Parent) then
    Result := NodePath(node.Parent) + ' \ ' + Result;
end;

procedure TDemoForm.ProcessItems(item: TMenuItem; node: TTreeNode);
var
  i: integer;
  subItem: TMenuItem;
  subNode: TTreeNode;
begin
  for i := 0 to node.Count - 1 do begin
    subNode := node.Item[i];
    subItem := NewItem(subNode.Text, TextToShortCut(''), false, true, nil, 0, '');
    subItem.Tag := Integer(subNode);
    if not subNode.HasChildren then
      subItem.OnClick := MenuItemClick;
    item.Add(subItem);
    ProcessItems(subItem, subNode);
  end;
end;

procedure TDemoForm.PopulateMenuFromTreeNodes(menu: TMenu; nodes: TTreeNodes);
var
  item: TMenuItem;
  node: TTreeNode;
begin
  menu.Items.Clear;
  node := nodes.GetFirstNode;
  while Assigned(node) do begin
    item := NewItem(node.Text, TextToShortCut(''), false, true, nil, 0, '');
    item.Tag := Integer(node);
    menu.Items.Add(item);
    if node.HasChildren
      then ProcessItems(item, node)
      else item.OnClick := MenuItemClick;
    node := node.getNextSibling;
  end;
end;

procedure TDemoForm.MenuButtonClick(Sender: TObject);
begin
  PopulateMenuFromTreeNodes(PopupMenu, MenuTree.Items);
end;

procedure TDemoForm.MenuItemClick(Sender: TObject);
begin
  with Sender as TMenuItem do
    ShowMessage(Format('%s', [NodePath(TTreeNode(Tag))]));
end;
Freundliche Grüße vom marabu
  Mit Zitat antworten Zitat