Solved

Hide TTreeView node highlight during a drag operation

Posted on 2004-08-30
21
695 Views
Last Modified: 2010-04-05
Hi,

I am dragging nodes in a TTreeView. As I drag a node, the node pased over is highlighted. This makes it hard to see the name of the node being dragged. The drag cursor is also hard to see.

My question is, how do I turn off the automatic node highlighting?

Regards,

Bill
0
Comment
Question by:wipnav
21 Comments
 
LVL 4

Expert Comment

by:Evarest
Comment Utility
I don't know how to do this with TTreeView as I never use this component.

If you want to make your apps a bit better looking (you want to change the colors of the nodes, change fonts, whatever) or you want to create large (> 100) items in your treeview, consider using TVirtualTreeView. This excellent component will be a bit odd to get used to (you'll need to get used to working with records for example), but once you know how to use it, you'll use it for everything.

Just take a look at http://www.delphi-gems.com/VirtualTreeview/VT.php to see the screenshot for yourself :-)

Or download the Demo applications from http://scripts.soft-gems.net/download.php?ID=33

If you want, I can guide you through the basic use of this component, so you can go on with your normal work...

PS.: i'm not affiliated in any way with the creator of TVirtualTreeView ;-)

Evarest
0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
Thank you. That is something to keep in mind.

I need to stick with using TTreeView for now though.

Bill
0
 
LVL 10

Expert Comment

by:_Katka_
Comment Utility
Hi, I'm sorry to tell you, but the only solution is.. to use ownerdraw of TTreeView..

regards,
Kate
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if TreeView1.Dragging  then
  if Node.Selected then
    TreeView1.Canvas.Brush.Color := clActiveCaption
  else
    TreeView1.Canvas.Brush.Color := clWhite;
end;
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
If you want to use different color:


var DragOverNode: TTreeNode=nil;
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  AnItem: TTreeNode;
  AttachMode: TNodeAttachMode;
  HT: THitTests;
begin
  if TreeView1.Selected = nil then Exit;
  DragOverNode := TreeView1.GetNodeAt(X, Y);

 // Do other things and set Accept  -----------

end;

procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if TreeView1.Dragging  then
  if Node.Selected then
    TreeView1.Canvas.Brush.Color := clActiveCaption
  else
    if Node = DragOverNode then
      TreeView1.Canvas.Brush.Color := clMoneyGreen   // Set the color
    else
      TreeView1.Canvas.Brush.Color := clWhite;
end;

0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if TreeView1.Dragging  then
  if Node.Selected then
    TreeView1.Canvas.Brush.Color := clActiveCaption
  else
    TreeView1.Canvas.Brush.Color := clWhite;
end;

This does what I want, though now I lose the name of the item being dragged. All that gets dragged now is the item icon. Do you know how to get back the name of the item being dragged?
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
Do you have image list assigned to the treeview?
It's sounds stupid, but without ImageList, by dragging you cannot see the text:

If you have the second question is: Do you create new DragObject ?
0
 
LVL 4

Accepted Solution

by:
mottor earned 250 total points
Comment Utility
Try this:

procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if TreeView1.Dragging and (State=[]) then
      TreeView1.Canvas.Brush.Color := clWhite;
end;

0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
if TreeView1.Dragging and (State=[]) then
  TreeView1.Canvas.Brush.Color := clWhite;

Thanks. This code works much better. I can see the icon and the name of the item being dragged.

There is one thing that seems a bit strange, thought it isn't related to the highlighting. I have two cursors when I'm dragging. One is a crArrow, and one is the drag cursor that I define in my code. Have you ever seen this sort of behavior? When I move to a no drop area, the cdArrow turns into a crNo (and it stays a crNo even if I move back to a drop allowed area).

0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
I meant to say that the crArrow turns into a cdNoDrop cursor.

This seems to be the default behavior of the TTreeView control when you have an image list assigned. The crArrow cursor tracks mouse movement in real-time, but the item being dragged moves more slowly, eventually catching up when you stop moving the mouse. If you move to a no drop area, you can never get the crArrow cursor back. Seems like a Delphi bug.

When there is no image list assigned, there is only the drag cursor, and it tracks mouse movement in real-time.

I wonder if there is a way to hide the crArrow cursor that is tracking mouse movement, and only use the drag cursor?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:mottor
Comment Utility
May I see, how you define and assign your drag cursor.
By me it's OK
One sample with changing cursor:


implementation

{$R *.dfm}

type
  TDragObjectExE = class(TDragControlObjectEx)
  protected
    function GetDragCursor(Accepted: Boolean; X, Y: Integer): TCursor; override;
  end;
var t: TDragObjectExE ;
procedure TForm1.TreeView1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  if DragObject = nil then begin
    DragObject:= TDragObjectExE.Create(TControl(Sender)) ;
  end;
end;

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
;
end;

procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if TreeView1.Dragging and (State=[]) then
      TreeView1.Canvas.Brush.Color := clWhite;
end;

{ TDragObjectExE }

function TDragObjectExE.GetDragCursor(Accepted: Boolean; X,
  Y: Integer): TCursor;
begin
  if Accepted then Result := crHourGlass
  else Result := crHSplit;
end;
0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
To see the behavior that I am talking about try these two things:

Csse A -
  Drop a TTreeView onto a form.
  Set DragMode to dmAutomatic.
  Set Accept := True in the OnDragOver event handler.
  Add a few nodes so that you have something to drag.
Run the program and drag a node. Everything works as expected. If you attempt to drag off of the TTreeView, the crDrag cursor changes to crNoDrop, and when youi return over the TTreeView it changes back to crDrag.

Case B -
  Use the project from case A, but assign a TImageList (with at least one image) to the Images propery. The first TImageList iitem should appear next to each node as an icon.
Run the program and drag a node. Instead of a single crDrag cursor, there are two cursors (one crArrow and one crDrag). The node icon and name are also dragged along (which is a nice featuee). Notice what happens if you drag off of the TTreeView. The crArrow becomes a crNoDrop, and stays that way, even if you move back onto the control.

I would like to hide the crArrow, and only see the crDrag cursor (and of course the node icon and name).
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
I cannot reproduce the case B
Everything is OK.
Delphi 6 and Windows 2000
Note that a drag cursor consist of arrow and something like a page
0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
I'm using Delphi 7 and Windows XP.

Does your tree node icon and name drag also?
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
Yes
0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
Oh well. I created a blank cursor resource that I assign in the OnStartDrag event handler. That eliminates the crArrow that I have. Now all I see as I drag is the drag cursor, and the node icon and name. It looksr good. The only problem is if someone drags off of the tree view component, then I'm stuck with a crNoDrop as long as they continue the drag. I can live with that I guess.

Thanks. for your help.

Bill
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
You didn't tell me how do you assign this drag cursor.
0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
Look at case A above. That is all that I do.
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
I will install next week Delphi 7 and WinXP.  
I'll try it and tell you
0
 
LVL 1

Author Comment

by:wipnav
Comment Utility
Thank you.
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
I have installed them, but nothing is changed.
All is OK.

Regards
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now