814123
2018-06-24 03:15
采纳率: 100%
浏览 996
已采纳

delphi Treeview弹出右键菜单?

图片说明
我只想右键‘222’,‘111’的时候右键菜单才弹出,右键其他文字不弹出菜单,代码如何实现啊?
‘222’,‘111’是分组名称,也行是其他的文字。麻烦老师给点参考。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • blownewbee 2018-06-24 17:29
    已采纳

    图片说明

     unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Menus, ComCtrls;
    
    type
      TForm1 = class(TForm)
        TreeView1: TTreeView;
        PopupMenu1: TPopupMenu;
        N1: TMenuItem;
        N2: TMenuItem;
        N3: TMenuItem;
        procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
    begin
    
      if (Node.Parent <> nil) and (Node.Parent.Text = 'È«²¿´ý°ì') then
          treeView1.PopupMenu := PopupMenu1
      else
          treeView1.PopupMenu := nil;
    
    end;
    
    end.
    
    
    点赞 打赏 评论
  • 堕落恶魔_Delphi 2018-06-27 01:21

    设置好treeview的PopupMenu 然后在下面的事件里写条件就行了

     procedure TForm1.TreeView1ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    var
      nNode: TTreeNode;
    begin
      nNode := TTreeView(Sender).GetNodeAt(MousePos.X, MousePos.Y);
      Handled := True;
      if nNode = nil then
        Exit;
      if (nNode.Text = '111') or (nNode.Text = '222') then {这行改成你需要的判断条件}
      begin
        nNode.Selected := True;
        Handled := False;
      end;
    end;
    
    点赞 1 打赏 评论

相关推荐 更多相似问题