How to get/set the position of the mouse relative from the application window?

蹲街弑〆低调 提交于 2019-12-11 09:37:30

问题


I want to be able to get the current location of the mouse pointer, relative from where my form window is. So if I would put my mouse on the top left of the form, it would give me the x and y values of x=0 and y=0, while the form itself might be in the middle of the screen.

Also I want to set the position of the mouse.

How can this be done in C#? I'm using a windows forms application.


回答1:


Obtain the screen coordinates by using Cursor.Position, then convert to window coordinates invoking PointToClient on the window.

Point p = this.PointToClient(Cursor.Position);



回答2:


Check out the Cursor.Position property



来源:https://stackoverflow.com/questions/1728286/how-to-get-set-the-position-of-the-mouse-relative-from-the-application-window

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