Reputation: 127
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));
Upvotes: 0
Views: 1973
Reputation: 51345
You cannot render into a window you do not own. The window returned from GetConsoleWindow()
is a window you do not own.
Translating character cell coordinates to pixel coordinates is a matter of scaling by the character cell's width and height. This information is available from GetConsoleFontSize()
even if not useful here.
Upvotes: 0
Reputation: 437
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.
Upvotes: -1
Reputation: 11116
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.
Upvotes: 3