How to get coordinates of node in TreeView?

D

deko

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/miscctrl/TreeViewDragDrop.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
form.

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
panel?

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
coordinates
TreeView tree = (TreeView)sender;
dragNode = (TreeNode)e.Item;
tree.SelectedNode = dragNode;
imageListDrag.Images.Clear();
imageListDrag.ImageSize = new Size(dragNode.Bounds.Size.Width +
tree.Indent, dragNode.Bounds.Height);
Bitmap bmp = new Bitmap(dragNode.Bounds.Width + tree.Indent,
dragNode.Bounds.Height);
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(bmp);
//imageListDrag.Images.Add(ImageHelper.getImage(folder));
//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.Handle, 0, dx, dy))
{
tree.DoDragDrop(bmp, DragDropEffects.Move);
TreeHelper.ImageList_EndDrag();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top