467,077 Members | 1,003 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

How to get coordinates of node in TreeView?

I'm trying to implement a custom TreeView that shows a ghost image while
dragging. But the form I'm using is different from the sample code found
here: http://www.codeproject.com/cs/miscct...ewDragDrop.asp

The problem is I can't get the proper coordinates required when dragging
begins. I have two tree views in a panel with a splitter, and the panel is
on a TabControl.

The sample code just has a simple form with the treeview control filling the

Here is how the sample gets coordinates:

Point pt = PointToClient(Control.MousePosition);

In my implementation, that provides incorrect coordinates.

I have a splitter (to make one tree smaller and one larger) which means the
tree on the left is docked LEFT and the one on the right is docked FILL.
Does this mean both TreeView's coordinates start at the left edge of the

I've tried this:

Point pt = PointToClient(Form.MousePosition);

but no luck...

Below is more complete code.

What is the best way to get the coordinates of the node which is about to be
dragged so I can get a ghost image?

Thanks in advance.

private void tree_ItemDrag(object sender, ItemDragEventArgs e)
//PROBLEM: not getting ghost image, probably due to incorrect mouse
TreeView tree = (TreeView)sender;
dragNode = (TreeNode)e.Item;
tree.SelectedNode = dragNode;
imageListDrag.ImageSize = new Size(dragNode.Bounds.Size.Width +
tree.Indent, dragNode.Bounds.Height);
Bitmap bmp = new Bitmap(dragNode.Bounds.Width + tree.Indent,
Graphics gfx = Graphics.FromImage(bmp);
gfx.DrawImage(imageListConfiguration.Images[0], 0, 0);
// optional: Draw node label into bitmap
//gfx.DrawString("CFG", tree.Font, new SolidBrush(tree.ForeColor),
(float)tree.Indent, 1.0f);
//imageListDrag.Images.Add(ImageHelper.getImage(fold er));
//Point p = tree.PointToClient(Control.MousePosition);
Point p = tree.PointToClient(Form.MousePosition); //does not help
int dx = p.X + tree.Indent - dragNode.Bounds.Left;
int dy = p.Y - dragNode.Bounds.Top;
if (TreeHelper.ImageList_BeginDrag(imageListDrag.Hand le, 0, dx, dy))
tree.DoDragDrop(bmp, DragDropEffects.Move);

Mar 2 '06 #1
  • viewed: 3109

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Saradhi | last post: by
6 posts views Thread by Tim | last post: by
13 posts views Thread by André Nogueira | last post: by
6 posts views Thread by xla76 | last post: by
1 post views Thread by =?Utf-8?B?Rmx5Z3V5?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.