C# How to get the Coordinates of a specific Point on Screen. (not mouselocation)

时光毁灭记忆、已成空白 提交于 2020-01-06 05:29:06

问题


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());


来源:https://stackoverflow.com/questions/4768677/c-sharp-how-to-get-the-coordinates-of-a-specific-point-on-screen-not-mouseloca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!