D
del_today
Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:
本人出学delphi,请多指教!
1、以下为一个TreeView控件中,拖拽的例子中的代码片断,我搞不明白,请给出详细解释好吗?谢了!
2、获取鼠标当前点中的位置的x、y坐标的函数是什么?
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
TargetNode, SourceNode: TTreeNode;
begin
TargetNode := TreeView1.GetNodeAt (X, Y);
// accept dragging from itself
if (Source = Sender) and (TargetNode <> nil) then
begin
Accept := True;
// determines source and target
SourceNode := TreeView1.Selected;
// look up the target parent chain
while (TargetNode.Parent <> nil) and
(TargetNode <> SourceNode) do
TargetNode := TargetNode.Parent;
// if source is found
if TargetNode = SourceNode then
// do not allow dragging over a child
Accept := False;
end
else
Accept := False;
end;
procedure CopyNodeUnder (TreeView: TTreeView;
SourceNode, TargetNode: TTreeNode);
var
NewNode: TTreeNode;
I: Integer;
begin
// add a new node
NewNode := TreeView.Items.AddChildFirst (TargetNode, '');
// copy the source
NewNode.Assign (SourceNode);
// recursively copy subitems
for I := SourceNode.Count - 1 downto 0 do
CopyNodeUnder (TreeView, SourceNode.Item , NewNode);
// delete source after copy (move)
TreeView.Items.Delete (SourceNode);
end;
procedure TForm1.TreeView1DragDrop(Sender, Source: TObject;
X, Y: Integer);
var
TargetNode, SourceNode: TTreeNode;
begin
TargetNode := TreeView1.GetNodeAt (X, Y);
if TargetNode <> nil then
begin
SourceNode := TreeView1.Selected;
SourceNode.MoveTo (TargetNode, naAddChildFirst);
TargetNode.Expand (False);
TreeView1.Selected := TargetNode;
end;
end;
本人出学delphi,请多指教!
1、以下为一个TreeView控件中,拖拽的例子中的代码片断,我搞不明白,请给出详细解释好吗?谢了!
2、获取鼠标当前点中的位置的x、y坐标的函数是什么?
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
TargetNode, SourceNode: TTreeNode;
begin
TargetNode := TreeView1.GetNodeAt (X, Y);
// accept dragging from itself
if (Source = Sender) and (TargetNode <> nil) then
begin
Accept := True;
// determines source and target
SourceNode := TreeView1.Selected;
// look up the target parent chain
while (TargetNode.Parent <> nil) and
(TargetNode <> SourceNode) do
TargetNode := TargetNode.Parent;
// if source is found
if TargetNode = SourceNode then
// do not allow dragging over a child
Accept := False;
end
else
Accept := False;
end;
procedure CopyNodeUnder (TreeView: TTreeView;
SourceNode, TargetNode: TTreeNode);
var
NewNode: TTreeNode;
I: Integer;
begin
// add a new node
NewNode := TreeView.Items.AddChildFirst (TargetNode, '');
// copy the source
NewNode.Assign (SourceNode);
// recursively copy subitems
for I := SourceNode.Count - 1 downto 0 do
CopyNodeUnder (TreeView, SourceNode.Item , NewNode);
// delete source after copy (move)
TreeView.Items.Delete (SourceNode);
end;
procedure TForm1.TreeView1DragDrop(Sender, Source: TObject;
X, Y: Integer);
var
TargetNode, SourceNode: TTreeNode;
begin
TargetNode := TreeView1.GetNodeAt (X, Y);
if TargetNode <> nil then
begin
SourceNode := TreeView1.Selected;
SourceNode.MoveTo (TargetNode, naAddChildFirst);
TargetNode.Expand (False);
TreeView1.Selected := TargetNode;
end;
end;