简体   繁体   English

在Windows环境中,如何获取鼠标单击的坐标(相对于窗口)

[英]How do you get the coordinates (with respect to a window) of a mouse click, in a Windows environment

With a Windows environment window, HWND , how do you get the coordinates of a mouse click on that window? 使用Windows环境窗口HWND ,如何获得在该窗口上单击鼠标的坐标?

edit: sorry for the vagueness. 编辑:抱歉,含糊其辞。 I have a HWND object and I am doing some image analysis on it. 我有一个HWND对象,并且正在对其进行一些图像分析。 I want to be able to click on a spot on the image displayed within the HWND object and print out the x,y coordinate of my click, as well as some properties of the image at the x and y coordinate 我希望能够单击HWND对象中显示的图像上的一个点,并打印出单击的x,y坐标,以及在x和y坐标处的图像的某些属性

The functions ScreenToClient and ClientToScreen convert between screen and client coordinate systems. ScreenToClientClientToScreen函数在屏幕坐标系和客户端坐标系之间转换。 Mouse messages are delivered to windows in client relative coordinates. 鼠标消息以客户端相对坐标的形式传递到Windows。

However, your question isn't terribly clear, so if I've not provided the answer you are looking for, please edit your question to explain exactly what you are looking for. 但是,您的问题并不十分清楚,因此,如果我没有提供所需的答案,请编辑您的问题以确切说明您的要求。

The basics of the answer are already given by David Heffernan. David Heffernan已经给出了答案的基础。 To be more complete, this is the full procedure: 为了更完整,这是完整的过程:

Each window has a unique HWND , which is a handle to an internal datastructure. 每个窗口都有一个唯一的HWND ,它是内部数据结构的句柄。 That structure contains amongst other things a function pointer to a WindowProc (window procedure). 该结构除其他外还包含指向WindowProc (窗口过程)的函数指针。 This window procedure is responsible for processing messages such as WM_LBUTTONUP . 此窗口过程负责处理诸如WM_LBUTTONUP消息。 Each window message has two associated parameters, historically called lparam and wparam . 每个窗口消息都有两个关联的参数,历史上称为lparamwparam The lparam parameter of WM_LBUTTONUP contains both the x and the y coordinates you want. WM_LBUTTONUPlparam参数包含所需的x和y坐标。 You can retrieve them via GET_X_LPARAM(lparam) and GET_Y_LPARAM(lparam) 您可以通过GET_X_LPARAM(lparam)GET_Y_LPARAM(lparam)检索它们

Windows supports the chaining of multiple WindowProc 's for a single HWND . Windows支持单个HWND链接多个WindowProc This is known as "subclassing" 这称为“子类化”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM