2

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@apple You need to handle WM_LBUTTONDOWN and/or WM_LBUTTONUP messages. Of course, if your are using MFC or WinForms or VCL or wxWidgets or Qt etc. then these will be wrapped up for you. Why don't you tell us more. We can't really guess at what you are doing. Details like language, framework, app type will help.
It's windows c++, as seen in the tags on the question. Nothing more. A function updates the window video WNDCLASS.lpfnWndProc. I'm not using QT or wxWidgets or the sorts. Maybe I'm misunderstanding what you're asking, though
@apple What does "window video WNDCLASS.lpfnWndProc" mean? Do you have a WndProc with a switch statement? Have you tried handling the messages I mention.
2

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"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.