Question

I am working in c++, normally the size of console is 25*80 pixels, but I am using setpixel() method to display on the console and it takes much larger values of coordinates, why it is so and how to convert from pixels to setpixel() coordinates.

here is my code for setpixel()

   HWND myconsole = GetConsoleWindow();
   HDC mydc = GetDC(myconsole);
   SetPixel(mydc,50,50,RGB(0,255,0));
Was it helpful?

Solution

The 25*80 you are referring to is not in pixels, but in characters. If you wish to use SetPixel to modify the console window, you first have to get the size of the client area, which can be done with GetClientRect.

The following would draw a red crosshair over the client area of your console window:

HWND myconsole = GetConsoleWindow();
HDC mydc = GetDC(myconsole);
RECT rect;
GetClientRect(myconsole, &rect);
for(int i = 0; i < rect.bottom - rect.top; ++i)
    SetPixel(mydc, (rect.right - rect.left) / 2, i, RGB(255, 0, 0));
for(int i = 0; i < rect.right - rect.left; ++i)
    SetPixel(mydc, i, (rect.bottom - rect.top) / 2, RGB(255, 0, 0));

Note that the console window can (and will) overwrite your drawings whenever it considers a redraw to be necessary.

OTHER TIPS

ScreenToClient offers a transformation of the screen co-ordinates to the client coordinates. Supply the co-ordinates to ScreenToClient and get the client co-ordinates and draw there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top