Matthew Scharley
Matthew Scharley

Reputation: 132274

How to convert screen coordinates to form relative coordinates (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.

Upvotes: 7

Views: 9820

Answers (1)

Dr Spack
Dr Spack

Reputation: 181

You can use:

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

to get relative coordinates.

Upvotes: 12

Related Questions