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

c#, winforms

Solution

You can use:

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

to get relative coordinates.

Problem

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.

Original source