Lazarus

Programming => LCL => Topic started by: SassyPenguin on October 24, 2021, 02:55:03 pm

Title: [SOLVED] How to Highlight a Treeview node on DragOver event
Post by: SassyPenguin on October 24, 2021, 02:55:03 pm
Hello mate,

Any hint on to highlight the node to reflect it as drop target during Treeview.OnDragOver event? I've searched the forum topics but found none.
Title: Re: [Need help] How to Highlight a Treeview node on DragOver event
Post by: howardpc on October 24, 2021, 05:56:49 pm
It is not clear what you want to drop on your treeview's node. But for the simple case of dragging another node from the treeview itself, the attached (incomplete) project shows one way to go about this.
It is incomplete because no action is taken when the node is dropped, and because the Accept parameter is simply set to True unless you try to drop a node on itself. A real app would obviously have a customised algorithm for this so only acceptable nodes were highlighted.


Title: Re: [Need help] How to Highlight a Treeview node on DragOver event
Post by: SassyPenguin on October 25, 2021, 02:31:19 am
It is not clear what you want to drop on your treeview's node. But for the simple case of dragging another node from the treeview itself, the attached (incomplete) project shows one way to go about this.
It is incomplete because no action is taken when the node is dropped, and because the Accept parameter is simply set to True unless you try to drop a node on itself. A real app would obviously have a customised algorithm for this so only acceptable nodes were highlighted.

Thank you howardpc.
Tested your example, its just what I was looking for. btw, Im drag & drop items from Listview to Treeview, with your sample, I think I can manage to make it work.
Title: Re: [Need help] How to Highlight a Treeview node on DragOver event
Post by: SassyPenguin on October 25, 2021, 03:00:23 am
One more question, how can we stop showing the Gray target rect as seen in the screenshot?
Title: Re: [Need help] How to Highlight a Treeview node on DragOver event
Post by: SassyPenguin on October 25, 2021, 05:51:47 am
Got it.
To remove that Gray rect, I need to set a Flag to indicate that item has been dropped and not to draw it as drop target (highlight).

1. set ItemDropped = FALSE in DragOver event.
2. set ItemDropped = TRUE after process DragDrop event.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.TV1AdvancedCustomDrawItem(Sender: TCustomTreeView;
  2.   Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage;
  3.   var PaintImages, DefaultDraw: Boolean);
  4. var
  5.   textR: TRect;
  6.   textY: Integer;
  7. begin
  8.   if Stage = cdPostPaint then
  9.       Exit;
  10.   case node.Equals(HighlightNode) and (not ItemDropped) of
  11.     FALSE:
  12.       begin
  13.          DefaultDraw := True;
  14.          Exit;
  15.       end;
  16.     TRUE:
  17.       DefaultDraw := False;
  18.   end;
  19.  
  20.   {draw highlight item here}
  21.  
  22. end;
  23.  
TinyPortal © 2005-2018