1

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; } 
9
  • What library is SetPixel from? Commented Mar 4, 2017 at 15:23
  • 1
    Assuming this is the Win32 function, you need to draw the pixels whenever you get a WM_PAINT message. Commented Mar 4, 2017 at 15:30
  • @BrandonIbbotson It is declared in the wingdi.h. Commented Mar 4, 2017 at 16:00
  • 3
    You need to read a book on WIn32 programming - that's far too broad a topic to discuss here, but basically you need a message loop and a window procedure.. Commented Mar 4, 2017 at 16:32
  • 2
    you need a message loop and a window procedure ... for that you have to create your own window using CreateWindow() function. You can't just handle WM_PAINT for the console window because the console window runs in a different process ("cmd.exe") where you don't have access to. Commented Mar 4, 2017 at 19:42

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

One solution you can do is to create an infinite loop, then inside infinite loop, the set pixel is being called here.

Kindly look at the sample code (based on what you have given):

#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 while(true){ SetPixel(consoleDC, 20, 20, RGB(255, 255, 255)); SetPixel(consoleDC, 20, 21, RGB(255, 255, 255)); SetPixel(consoleDC, 20, 22, RGB(255, 255, 255)); SetPixel(consoleDC, 20, 23, RGB(255, 255, 255)); SetPixel(consoleDC, 21, 20, RGB(255, 255, 255)); SetPixel(consoleDC, 21, 21, RGB(255, 255, 255)); SetPixel(consoleDC, 21, 22, RGB(255, 255, 255)); SetPixel(consoleDC, 21, 23, RGB(255, 255, 255)); SetPixel(consoleDC, 22, 20, RGB(255, 255, 255)); SetPixel(consoleDC, 22, 21, RGB(255, 255, 255)); SetPixel(consoleDC, 22, 22, RGB(255, 255, 255)); SetPixel(consoleDC, 22, 23, RGB(255, 255, 255)); SetPixel(consoleDC, 23, 20, RGB(255, 255, 255)); SetPixel(consoleDC, 23, 21, RGB(255, 255, 255)); SetPixel(consoleDC, 23, 22, RGB(255, 255, 255)); SetPixel(consoleDC, 23, 23, RGB(255, 255, 255)); } ReleaseDC(consoleWindow, consoleDC); cin.ignore(); return 0; } 

Not so perfect solution, as when you scroll down the console, the pixel is being replicated and looks like a trailing dot, but it gives you the idea on how to get things done. :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.