Getting cursor coordinates

Mar 17, 2012 at 12:04am
Ok so I've been looking at keeping track of my cursor coordinates on my main window. GetCursorPos() returns a pointer to a POINT struct that holds x and y coordinates. In order to separate this, I will need to do something like?

1
2
3
4
POINT * ptr = GetCursorPos()
POINT points = *ptr;
xCoord = points.x;
yCoord = points.y;
Mar 17, 2012 at 3:01am
GetCursorPos() returns a BOOL, and fills the POINT structure pointed to by the address given in the first parameter. At least the pure WinAPI call. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms648390(v=vs.85).aspx .

You would use it like this:

1
2
3
4
5
6
7
8
9
10
POINT curPos;
BOOL result = GetCursorPos(&curPos);
if (result)
{
    //The function call succeeded and curPos has valid data.
}
else
{
    //The function call failed and you cannot rely on the information in curPos.
}
Mar 17, 2012 at 8:25pm
Ugh, not sure what I'm doing wrong here. Compiles just fine, but does no writing at all.

1
2
3
4
5
6
7
8
9
10
11
12
case WM_LBUTTONDOWN:
			POINT pts;
			BOOL result;
			hdc = BeginPaint(hWnd, &ps);
			result = GetCursorPos(&pts);
			if(result)
			{
			TextOut(hdc,
				pts.x, pts.y,
				greeting, _tcslen(greeting));
			}
			EndPaint(hWnd, &ps);
Mar 17, 2012 at 8:35pm
GetLastError() tells me that GetCursorPos returns 0, but operation completes successfully. MSDN says that GetCursorPos returns 0 when it fails.
Mar 18, 2012 at 2:14am
It should work if you change the calls to BiginPaint and EndPaint to GetDC and ReleaseDC respectively.

This code I tested works although the coordinates are wrong.
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
wchar_t* greeting = TEXT("HELLO");
	switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
		case WM_LBUTTONDOWN:
			HDC hdc;
			
			PAINTSTRUCT ps;
			POINT pts;
			BOOL result;
			hdc = GetDC(hwnd);//BeginPaint(hwnd, &ps);
			result = GetCursorPos(&pts);
			if(result)
			{
			TextOut(hdc,
				pts.x, pts.y,
				greeting, _tcslen(greeting));
			}
			ReleaseDC(hwnd,hdc);//EndPaint(hwnd, &ps);
			break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }


Edit:
Very simple fix for the coordinates
1
2
3
4
5
6
7
if(result)
{
	ScreenToClient(hwnd,&pts);
	TextOut(hdc,pts.x,pts.y,
	greeting,
	_tcslen(greeting));
}
Last edited on Mar 18, 2012 at 3:33am
Mar 18, 2012 at 6:30am
Note that BeginPaint/EndPaint should only be used when painting (ie: WM_PAINT) because they only redraw invalidated/dirty areas.

If you are drawing outside of the normal paint, then you'd want GetDC/ReleaseDC like naraku suggested.



Although that has its own problems. For example if you click to display that text, then drag another window over your main window, then flip back to your program, the text will be gone. The window needs to be repainted when something goes over it. Windows sends a WM_PAINT message when that happens, which is one of the reasons why it's best to keep your drawing code in WM_PAINT.
Topic archived. No new replies allowed.