How do I get the position of the mouse (x and y) on the console? C ++

3

I have the following code:

#include<windows.h>
#include<iostream>
#include <cmath>

using namespace std;

int main()
{
    HWND myconsole = GetConsoleWindow();
    HDC mydc = GetDC(myconsole);

    int x = 150;
    int y = 150;

    COLORREF COLOR = RGB(255, 255, 255);

    SetPixel(mydc, x, y, COLOR);

    ReleaseDC(myconsole, mydc);
    cin.ignore();
    return 0;
}

Where a pixel is drawn in the console's coordinate (150, 150).

I would like to know how I can get the coordinate of the mouse position, to use it to draw such a pixel.

    
asked by anonymous 14.04.2016 / 14:41

1 answer

5

I assume your code is specific to windows ( windows.h ).

In Windows environments you can use the GetCursorPos function to get the position of the cursor relative to the screen.

To translate the coordinates so that positions relative to the window are available, you can use the ScreenToClient .

POINT pt;
GetCursorPos(&pt);
ScreenToClient(HWND, &pt);
// pt.x e pt.y guardam as coordenadas 

Reference : SOen- Get current cursor position

    
14.04.2016 / 15:00