核心提示:新建个窗体,拖一个TreeView控件TreeView1,在TreeView1里面新增多个项目,在拖一个PopupMenu控件PopupMenu1,在PopupMenu新增一个MenuItem菜单N1...
新建个窗体,拖一个TreeView控件TreeView1,在TreeView1里面新增多个项目,在拖一个PopupMenu控件PopupMenu1,在PopupMenu新增一个MenuItem菜单N1,然后在TreeView的ContextPopup事件中写如下代码:
procedure TForm15.TreeView1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
var
TVNode : TTreeNode;
TV : TTreeView;
begin
TV := TTreeView(Sender);
TVNode := TV.GetNodeAt(MousePos.X, MousePos.Y) ;
if Assigned(TVNode) then
begin
TV.Selected := TVNode;
N1.Caption := Format('项目: %s',[TVNode.Text]) ;
end
else
begin
//不弹出菜单
Handled := true;
end;
end;