Mouse coordinate position in window

After searching on stackoverflow I got this

1
2
3
4
5
6
7
8
9
10
11
12
13
POINT p;
	
	for (int i = 0;; ++i)
	{
		HWND hwnd = GetConsoleWindow();
		system("cls");
		if (ScreenToClient(hwnd, &p))
		{
			//p.x and p.y are now relative to hwnd's client area
		}
		cout << p.x << "  " << p.y;
		Sleep(200);
	}

I'm looking for a function that gives current mouse position(x and y) in that particular console window.
I've tried above code but it is not working.

Can anyone correct me or give me another code to do this.
OK finally I got this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
HWND hwnd;
	for (int i = 1; i<20; i++)
		hwnd = FindWindow(NULL, TEXT("Mouseposition"));
	HDC hdc = GetDC(hwnd);

	POINT aPoint;
	
	/*while (1)
	{
		system("cls");
		GetCursorPos(&aPoint);
		ScreenToClient(hwnd, &aPoint);

		cout << "Mouse position (x/y) was: " << aPoint.x << "/" << aPoint.y << endl;
	}*/

	for (int q = 0;; ++q)
	{
		system("cls");
		GetCursorPos(&aPoint);
		ScreenToClient(hwnd, &aPoint);
		cout << "Mouse position (x/y) was: " << aPoint.x << "/" << aPoint.y << endl;
		Sleep(200);
	}


This gives cursor position of console window.
Tell me if the above code is modified or a better function is there.
It appears to look fine, just a few things I'm questioning:

First, avoid system. Its not a great thing to use due to security reasons. Apart from that why do you have line 2? And why do you have line 17 the way you do? These things don't seem to make much sense. Finally, you should probably put more error checking in - check to see if hwnd is valid, check to see if the ScreenToClient function succeeded, etc.
Okay
I'll avoid system but what I do if I want to clear screen.
clrscr(); not works in Visual Studio.
Two ways - the easy way and the WinAPI way.

The easy way:
 
cout << std::string(80, '\n'); // print 80 newlines 


The WinAPI way (from http://www.cplusplus.com/articles/4z18T05o/ )
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
#include <windows.h>

void ClearScreen()
  {
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }
OK thanks
I'll do second one.
because 1st one will be slower.
Topic archived. No new replies allowed.