Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
491 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


TreeView control DragDrop Operation helper functions Format this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 5.x (or higher)
Category:
GUI
Skill Level:
Scoring:
Last Update:
05/04/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet TreeView DragDrop auto-scroll auto-expand
Times Scored:
5
Visits:
3458
Uploader: Wei bao
Company:
Reference: N/A
 
Question/Problem/Abstract:
make Treeview control dragdrop operation programming easily.
1. expand/collaps automatically
2. move with children nodes
3. disable drop to child and self
4. auto scroll the control while the cursor near the top or bottom of the control
Answer:



unit TreeDragHelp;

interface

// TreeView control DragDrop Operation helper functions
// Author : bwsunv@163.com
// Date : 2003-05-04
// Features:
// 1. expand/collaps automatically
// 2. move with children nodes
// 3. disable drop to child and self
// 4. auto scroll the control while the cursor near the top
//     or bottom of the control
// usage :
//   In the 4 event functions call the bwXXXX  
//  event : StartDrag/EndDrag/DragOver/DragDrop  

uses windows, classes, Controls, Forms, ComCtrls, ExtCtrls;
var
  lastitem, dragitem : TTreeNode;
  DragTimer : TTimer;

procedure bwTreeViewStartDrag(Sender: TObject;
  var DragObject: TDragObject; timer1 : TTimer = nil);

procedure bwTreeViewEndDrag(Sender, Target: TObject; X, Y: Integer);

procedure bwTreeViewDragDrop(Sender, Source: TObject; X, Y: Integer);
                                          
procedure bwTreeViewDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
  
// Timer used to expand / collasp automatically
procedure bwtimer(Sender : TObject);

implementation

procedure bwTreeViewStartDrag(Sender: TObject;
  var DragObject: TDragObject; timer1 : TTimer);
begin
  DragItem := (Sender as TTreeView).selected;
  lastitem := nil;
  DragTimer := Timer1;
  if Assigned(DragTimer) then
  begin
    DragTimer.Enabled := False;
  end;
end;

procedure bwTreeViewEndDrag(Sender, Target: TObject; X, Y: Integer);
begin
  if Assigned(DragTimer) then
    DragTimer.Enabled := false;
end;

procedure bwTreeViewDragDrop(Sender, Source: TObject; X, Y: Integer);
var
  DropItem : TTreeNode;
begin
  if Source = Sender then
  begin
    DropItem := (Sender as TTreeView).GetNodeAt (x, y);
    dragitem.MoveTo(DropItem, naAddChild);
  end;
end;

procedure bwTreeViewDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  DropItem, TopItem : TTreeNode;
  rt : TRect;
begin
  Accept := False;
  if Source = Sender then
  begin
    DropItem := (Sender as TTreeView).GetNodeAt (x, y);
    if DropItem = nil then
    begin
      if Assigned(DragTimer) then
        DragTimer.Enabled := False;
      Exit;
    end;
    if DropItem <> lastitem then
    begin
      if Assigned(DragTimer) then
      begin
        DragTimer.Enabled := False;
        DragTimer.Enabled := True;
      end;
      Lastitem := DropItem;
    end;
    // if on the top , auto scroll
    if DropItem = (Sender as TTreeView).topitem then
    begin
      if (Sender as TTreeView).topitem.GetPrevVisible <> nil then
        (Sender as TTreeView).topitem :=
        (Sender as TTreeView).topitem.GetPrevVisible;
      if DropItem <> (Sender as TTreeView).topitem then
        Exit;
    end;
    rt := dropItem.DisplayRect (False);
    //// if on the bottom, auto scroll
    if rt.Bottom > (Sender as TTreeView).Height - 8 then
    begin
      TopItem := (Sender as TTreeView).topitem;
      if (Sender as TTreeView).topitem.GetNextVisible <> nil then
        (Sender as TTreeView).topitem :=
        (Sender as TTreeView).topitem.GetNextVisible;  
      if Topitem <> (Sender as TTreeView).topitem then
        Exit;
    end;
    if DropItem = DragItem then// nothing
    else
    if DropItem.HasAsParent ( DragItem) = False then// not children
      Accept := True;
    if DragItem.HasAsparent(DropItem) = True then// don't collasp parent node
      if Assigned(DragTimer) then
        DragTimer.Enabled := False;
  end;
end;

procedure bwtimer(Sender : TObject);
begin
  if lastitem <> nil then
    lastItem.Expanded := not LastItem.expanded;// expand/collaps automatically
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
R. Lefter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)