Issue with getting the mouse co-ordinates

.exe file download: http://www.sendspace.com/file/xecn55

source code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Includes
#include <windows.h>
//Defines
#define ErrorMessageBox(a,b) MessageBox(a,b,"Error:",MB_ICONWARNING)
//Function declarations
void PrintText (int, int, char*, HWND);
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
//Entry point
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpsCommandLine, int iCmdShow)
{
	//Create and register a WindowClass
    WNDCLASSEX WindowClass;
    WindowClass.hInstance = hInstance;
    WindowClass.lpszClassName = "1";
    WindowClass.lpfnWndProc = WindowProcedure;
    WindowClass.style = 0;
    WindowClass.cbSize = sizeof (WNDCLASSEX);
    WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
    WindowClass.lpszMenuName = NULL;
    WindowClass.cbClsExtra = 0;
    WindowClass.cbWndExtra = 0;
    WindowClass.hbrBackground = CreateSolidBrush (RGB (255, 255, 255));
    if (!RegisterClassEx (&WindowClass))
    {
    	ErrorMessageBox (NULL, "Window class registration for class \"1\" has failed.");
    	return 0;
    }
    //Create the window
    HWND hWnd = CreateWindow ("1", "", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, NULL, NULL, hInstance, NULL);
    if (!hWnd)
    {
    	ErrorMessageBox (NULL, "Window handle \"hWnd\" is NULL.");
    	return 0;
    }
    //Show the window
    ShowWindow (hWnd, SW_SHOW);
    //Message loop
    MSG uMsg = {0};
    while (GetMessage (&uMsg, NULL, 0, 0) > 0)
    {
        TranslateMessage (&uMsg);
        DispatchMessage (&uMsg);
    }
    return 0;
}
//Function to print text
void PrintText (int iX, int iY, char *cpText, HWND hWnd)
{
	InvalidateRect (hWnd, NULL, FALSE); //Re-Paint the window
	PAINTSTRUCT ps;
	HDC hDC = BeginPaint (hWnd, &ps);
	TextOut (hDC, iX, iY, cpText, 1); //Print the "*"
	EndPaint (hWnd, &ps);
}
//Window procedure
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uiMsg)
    {
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
            //If we press the Left mouse button
		case WM_LBUTTONDOWN:
			{
				POINT cursorPos;
				GetCursorPos (&cursorPos);
				PrintText (cursorPos.x, cursorPos.y, "*", hWnd); //Call the print text function
			}
			break;
    }
    return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}


If you run the program then you will see that the "*" isn't printed at the right location. I tried subtracting some numbers from iX and iY in the PrintText function and it seemed like it was going to work, then I subtracted 10 more and it jumped to another part of the window. Every two+ times that I run it the distance changes a lot.

Any idea why this is happening and how to fix it? Thanks.
The GetCursorPos function gets the mouse position in SCREEN corordinates (that is relative to the very top left of the screen) NOT relative to your window client area.

You will find that if your window is positioned towrad the top left of the screen, the * might show up, BUT if you move your window towards the right and bootom of the screen you wont see the
* at all.


The WM_LBUTTONDOWN message contains the position of the mouse relative to the client area (which is what we want) in the LParam - change the code like this

1
2
3
4
5
6
7
8
9
     case WM_LBUTTONDOWN:
            {
                POINT cursorPos;
                cursorPos.x = LOWORD(lParam); 
                cursorPos.y = HIWORD(lParam);               
                //GetCursorPos (&cursorPos);  //Get rid of this              
                PrintText (cursorPos.x, cursorPos.y, "*", hWnd); //Call the print text function
            }
            break;



OR for the much shorter:
1
2
3
4
5
        case WM_LBUTTONDOWN:
            {
                PrintText (LOWORD(lParam), HIWORD(lParam), "*", hWnd); //Call the print text function
            }
            break;




PS
I have to mention that you method of painting the window isn't the way it is normally done.
Last edited on
Thanks, it worked (You most likely already knew that lol)
Topic archived. No new replies allowed.