Whether ondragover can be put down
Procedure tmainform. treeviewdragover (sender, source: tobject; X, Y: integer; State: tdragstate; var accept: Boolean); <br/> var <br/> targetnode, sourcenode: ttreenode; <br/> begin <br/> Accept: = false; <br/> targetnode: = ttreeview (sender ). getnodeat (x, y); <br/> If (Source = sender) and (targetnode <> nil) Then <br/> begin <br/> Accept: = true; <br/> sourcenode: = ttreeview (sender ). selected; <br/> If targetnode = sourcenode. parent then <br/> Accept: = false; <br/> If accept then <br/> begin <br/> while (targetnode. parent <> nil) and (targetnode <> sourcenode) DO <br/> targetnode: = targetnode. parent; <br/> If targetnode = sourcenode then <br/> Accept: = false; <br/> end;
Ondragdrop is the processing method of nodes after being put down
Procedure tmainform. treeviewdragdrop (sender, source: tobject; X, Y: integer); <br/> var <br/> targetnode, sourcenode: ttreenode; <br/> begin <br/> targetnode: = ttreeview (sender ). getnodeat (x, y); <br/> If targetnode <> nil then <br/> begin <br/> sourcenode: = ttreeview (sender ). selected; <br/> ttreeview (sender ). items. beginupdate; <br/> sourcenode. moveTo (targetnode, naaddchildfirst); <br/> ttreeview (sender ). selected: = sourcenode; <br/> ttreeview (sender ). items. endupdate; <br/> end;
In addition, Treeview also needs to be set to support drag and drop, two methods
1: You can drag and drop a static Treeview.
Procedure tmainform. formcreate (Sender: tobject); <br/> begin <br/> Treeview. dragmode: = dmautomatic; <br/>/< br/> end;
2. dynamically set events
Procedure tmainform. treeviewmousedown (Sender: tobject; button: tmousebutton; shift: tshiftstate; X, Y: integer); <br/> begin <br/> If (Button = mbleft) and (htonitem in ttreeview (sender ). gethittestinfoat (x, y) Then // click the left button and click <br/> begin <br/> If (ssctrl in shift) on the item) then // If you press Ctrl <br/> ttreeview (sender ). begindrag (false); // drag and drop allowed <br/> end; <br/>