Recent

Author Topic: [SOLVED] How to Highlight a Treeview node on DragOver event  (Read 3167 times)

jamestien

  • New Member
  • *
  • Posts: 35
[SOLVED] How to Highlight a Treeview node on DragOver event
« 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.
« Last Edit: October 25, 2021, 05:52:55 am by jamestien »
Lazarus 2.0.12/2.2.0RC1 (Win10, Xubuntu, Pop!Os)

howardpc

  • Hero Member
  • *****
  • Posts: 3829
Re: [Need help] How to Highlight a Treeview node on DragOver event
« Reply #1 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.



jamestien

  • New Member
  • *
  • Posts: 35
Re: [Need help] How to Highlight a Treeview node on DragOver event
« Reply #2 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.
Lazarus 2.0.12/2.2.0RC1 (Win10, Xubuntu, Pop!Os)

jamestien

  • New Member
  • *
  • Posts: 35
Re: [Need help] How to Highlight a Treeview node on DragOver event
« Reply #3 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?
« Last Edit: October 25, 2021, 03:06:49 am by jamestien »
Lazarus 2.0.12/2.2.0RC1 (Win10, Xubuntu, Pop!Os)

jamestien

  • New Member
  • *
  • Posts: 35
Re: [Need help] How to Highlight a Treeview node on DragOver event
« Reply #4 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.  
Lazarus 2.0.12/2.2.0RC1 (Win10, Xubuntu, Pop!Os)

 

TinyPortal © 2005-2018