Mouse position inside a window

I need help with a program I need to make. Basically, it is a tic-tac-toe program that you could play with either the mouse or the keyboard. While I got the keyboard part down, I can't figure out how to do the mouse. Using GetCursorPos() only gets the point in terms of the entire screen. Is there a way to get the cursor position in terms of the program window?
After calling GetCursorPos(), call ScreenToClient(HWND, POINT). The first argument is the window that you want the cursor position relative to; the second argument is the point structure you used in GetCursorPos().
You could process the WM_LBUTTONDOWN message if you're using windows api, the LPARAM LOWORD is the x position, the y is the HIWORD of LPARAM.

1
2
3
4
case WM_LBUTTONDOWN:
    x = LOWORD(lparam);
    y = HIWORD(lparam);
return 0;


Thanks tyler22, that helped me out a lot, it's exactly what I needed.

newbieg, the problem I have with this solution is that I don't know how to get the LPARAM variable into my function. The WinMain function I am using is

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

How would I pull in the lparam for LOWORD() and HIWORD() if it is not in the WinMain declaration? WinMain is where the function that uses the click is called in my program.

Sorry if this is a rudimentary question, my teacher did not go into many details of programming with Windows other than very basic things.
I believe the lparam is in the windowprocedure function or hat ever you called it. here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){

    switch( msg ){
		case WM_PAINT:
                        // do some painting
                        break;
                case WM_LBUTTONDOWN:
                        // get the x and y coordinates
                        x = LOWORD(lparam);
                        y = HIWORD(lparam);
                        break;
// more code... 


anyone is allowed to correct me if im wrong. but i think this would be where the lparam is found
Hey, I'm interested in this as I'm starting to get a little to grips with proper game programming... Does the LPARAM (when WM_LBUTTONDOWN is sent) store the position in terms of screen size, in terms of window size, or in terms of client size?
Last edited on
It is in terms of the client screen of whichever window receives the message. (which means that if you have a child window open inside a parent window and the child window processed the click then it will be relative to the child window's origin, not the parent's).

The message is only sent to the window that gets clicked on, so if you are using child windows it is the child window's procedure responsibility to pass the appropriate message on to the parent when needed.

code below is sending a message from child to parent, this would be placed under a WM_ that you want to send straight to the parent. You can also send a different message or even define your own messages as long as you define their number as greater than WM_USER;

1
2
3
HWND parentHWND;
parentHWND = GetWindowLongPtr(hwnd, GWLP_HWNDPARENT)
postmessage(parentHWND, msg, wparam, lparam);


A great resource is http://msdn.microsoft.com/en-us/library/windows/desktop/ff818516%28v=vs.85%29.aspx - I usually type any function or WM_MESSAGE I have a question about in the search bar at the top of the screen there.
Last edited on
i am also helpless....
Topic archived. No new replies allowed.