C++ Pixels in Console Window

C++ Pixels set through SetPixel are disappearing when resizing the console or moving it out of the screen

The console window is not your window, you should not be drawing on it directly!

You are allowed to use FillConsoleOutputAttribute and FillConsoleOutputCharacter to create colored "graphics" with boxes and lines and play with the screen buffers but that is about it.

If you need pixel precision then you need to create your own window with CreateWindow and draw in WM_PAINT.

Convert SetPixel coordinates to pixels on console

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.

How to set pixels to Console Windows by passing String Pointer in C?

Not sure what you mean by "Console Windows" or "String", and what they have to do with SetPixel().

However it is true that modifying bitmaps using repeated calls to SetPixel() is very inefficient because it has high overhead. Instead, copy out the bitmap data to a buffer using GetDIBits(), modify the buffer, and once you're done copy them back into the bitmap using SetDIBits().



Related Topics



Leave a reply



Submit