function Ttrvfrm.GetKey(x, y: integer): String; //返回健值; var HT: THitTests; Node: TTreeNode; begin Result := ''; HT := trv.GetHitTestInfoAt(x, y); if (HT - [htOnItem, htOnIcon, htNowhere, htOnIndent, htOnRight] <> HT) then begin if (htOnitem in HT) or (htOnIcon in HT) then begin trv.Cursor := crHandPoint; Node := trv.GetNodeAt(x, y); if Node = trv.TopItem then trv.PopupMenu := trvtop else trv.PopupMenu := trvPop; Result := intToStr(pinfo(Node.Data).ID); end; if (htNowhere in HT) or (htOnIndent in HT) or (htOnRight in HT) then begin trv.Cursor := crDefault; trv.Selected := nil; trv.PopupMenu := nil; lv.Items.Clear; Exit; end; end; end; 一个过去写的类似注册表,hit限定范围。如果用d7就不需要了。
实现鼠标右键选中结点1. 设置treeview的属性RightClickSelect为True2. 编写一个函数 procedure ChangeSelectNode(tree:TTreeView;x,y:integer;Button: TMouseButton); var anode:TTreeNode; begin if button=mbright then {是否按了右键} begin anode:=tree.GetNodeAt(x,y); {取x,y坐标下的结点} if anode<>nil then anode.Selected :=true; {设置点中结点为选中} end; end;3. 在treeview的OnMouseDown事件加入下面的语句 changeselectnode(treeview1,x,y,button);
var
HT: THitTests;
Node: TTreeNode;
begin
Result := '';
HT := trv.GetHitTestInfoAt(x, y);
if (HT - [htOnItem, htOnIcon, htNowhere, htOnIndent, htOnRight] <> HT) then
begin
if (htOnitem in HT) or (htOnIcon in HT) then
begin
trv.Cursor := crHandPoint;
Node := trv.GetNodeAt(x, y);
if Node = trv.TopItem then
trv.PopupMenu := trvtop
else trv.PopupMenu := trvPop;
Result := intToStr(pinfo(Node.Data).ID);
end;
if (htNowhere in HT) or (htOnIndent in HT) or (htOnRight in HT) then
begin
trv.Cursor := crDefault;
trv.Selected := nil;
trv.PopupMenu := nil;
lv.Items.Clear;
Exit;
end;
end;
end;
一个过去写的类似注册表,hit限定范围。如果用d7就不需要了。
begin
PopupMenu1.AutoPopup := False;
end;procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);begin
PopupMenu1.Popup(X, Y); // 在这里判断鼠标右键和 TreeView 选择。
end;
对Treeview的节点是否选中问题解决,你能再详细一点吗?
procedure ChangeSelectNode(tree:TTreeView;x,y:integer;Button: TMouseButton);
var
anode:TTreeNode;
begin
if button=mbright then {是否按了右键}
begin
anode:=tree.GetNodeAt(x,y); {取x,y坐标下的结点}
if anode<>nil then
anode.Selected :=true; {设置点中结点为选中}
end;
end;3. 在treeview的OnMouseDown事件加入下面的语句
changeselectnode(treeview1,x,y,button);