Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert screen coordinates to form relative coordinates (winforms)?

Tags:

c#

winforms

I have the following function (that is incorrect):

private void TreeView_DragDrop(object sender, DragEventArgs e)
{
    TreeNode CurrentNode = 
        TreeView.GetNodeAt(e.X - this.Left - NotesView.Left, 
                           e.Y - this.Top - NotesView.Top);
    // [snip]...
}

But this is incorrect because it doesn't take into account the forms decorations... I'm sure there has to be a better way to do this other than hard coding it (which'll be wrong anyway, depending on several things such as Vista vs XP vs Win2k), but I can't find it.

like image 936
Matthew Scharley Avatar asked Jun 17 '09 08:06

Matthew Scharley


1 Answers

You can use:

 Point clientPoint = TreeView.PointToClient( new Point( e.X, e.Y ) );

to get relative coordinates.

like image 50
Dr Spack Avatar answered Sep 18 '22 00:09

Dr Spack