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.
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.
#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 );
}