C# How to get the Coordinates of a specific Point

2019-08-29 00:10发布

问题:

I have the following problem: I'm writing a WinForms application with C# and i want to get the Screen - Coordinates of a part of a picture, in this case the top of this hand (marked by the red point).

Does anybody know how i can do this programmaticaly?

("Koordinaten dieses Punktes" = Coordinates of this Point)

EDIT: sry for confusing you, the picture above should only demonstrate my problem. The actual target of my program is to move the mouse-controlled-hand of a dart game to the right position, but it isn't possible by only setting the MouseLocation to a fix Point, because every turn the dart-hand gets another x:y distance to the MouseLocation. So I need to find the Location of the dart (-arrow).

I hope that everybody knows what my problem is now.

Picture of the dart game

回答1:

In some event of the form use:

this.PointToScreen(new System.Drawing.Point(250, 300));

Replace the Point by the point (relative to the form) you are interested in.



回答2:

Control.PointToScreen won't work correctly if your window is minimized, hidden or off the screen. You'll have to drop down to Interop with Win32 APIs:

so start by importing the API:

[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool GetCursorPos(out Point lpPoint);

and use it:

Point pts;
GetCursorPos(out pts);
MessageBox.Show(this, pts.ToString());