With a Windows environment window, HWND
, how do you get the coordinates of a mouse click on that window?
edit: sorry for the vagueness. I have a HWND object and I am doing some image analysis on it. 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
The functions ScreenToClient
and ClientToScreen
convert between screen and client coordinate systems. Mouse messages are delivered to windows in client relative coordinates.
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. To be more complete, this is the full procedure:
Each window has a unique HWND
, which is a handle to an internal datastructure. That structure contains amongst other things a function pointer to a WindowProc
(window procedure). This window procedure is responsible for processing messages such as WM_LBUTTONUP
. Each window message has two associated parameters, historically called lparam
and wparam
. The lparam
parameter of WM_LBUTTONUP
contains both the x and the y coordinates you want. You can retrieve them via GET_X_LPARAM(lparam)
and GET_Y_LPARAM(lparam)
Windows supports the chaining of multiple WindowProc
's for a single HWND
. This is known as "subclassing"
精彩评论