Find Coordinates for point on screen?

南楼画角 提交于 2019-12-21 20:36:47

问题


The thing is I have some graphics shown in a form, rectangle for example, and I want to capture when the point gets over thees fields. So I Thoght I try to find the corrrdinates for thees rectangles, but as thay are the coords in the form it dose not match the ones with the mouse location.

So I wonder is there a way to find what coord on the screen a Point has on the screen and not in the form or controller?


回答1:


Each control hs PointToFoo methods for conversion. Note that you should call this from the parent of the object who's location you want:

Point scrPos = this.PointToScreen(panel1.Location);

Alternatively, you can get the panel's screen coordinates with:

Point scrPos = panel1.PointToScreen(new Point(0,0));

Note that the above two examples could gve different result due to the border-size of the panel.




回答2:


If you are using the graphics object for the form by calling this.CreateGraphics() within the form, then the coordinates used when you draw the rectangle should be exactly the same as those returned by the click event on the form.




回答3:


Do you know what coordinates your pointer is in? You can get the coordinates for your window with a call to GetWindowRect() and subtract the top/left from your mouse cursor to get client coordinates.

I seem to remember there being a function to do that for you in fact, but it's been some time since I dabbled in custom GUI controls.



来源:https://stackoverflow.com/questions/1438283/find-coordinates-for-point-on-screen

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