Search code examples
c++windowsconsoleconsole-applicationpixel

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


So I am currently using the SetPixel function to recolor some pixels on the screen. But when I resize the console or move the console out of the screen, the pixels which went out of the screen are black again.

How can I prevent them from getting black?

regards, TPRammus

EDIT: Here is an example:

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>

using namespace std;

HWND consoleWindow = GetConsoleWindow();    // Get a console handle

int main()
{
    HDC consoleDC = GetDC(consoleWindow);        // Get a handle to device context

    SetPixel(consoleDC, 20, 20, RGB(255, 255, 255));

    ReleaseDC(consoleWindow, consoleDC);
    cin.ignore();
    return 0;
}

Solution

  • 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.