C++ Pixels In Console Window

前端 未结 6 1059
谎友^
谎友^ 2020-12-04 22:27

In C++ using Code::Blocks v10.05, how do I draw a single pixel on the console screen? Is this easy at all, or would it be easier to just draw a rectangle? How do I color it?

6条回答
  •  情深已故
    2020-12-04 23:24

    It depends on your OS. I suppose you are programming in a Windows platform, therefore you can use SetPixel but you have to use "windows.h" to get a console handle, so here an example for drawing the cos() function:

    #include
    #include
    #include 
    
    using namespace std;
    
    #define PI 3.14
    
    int main() 
    {
        //Get a console handle
        HWND myconsole = GetConsoleWindow();
        //Get a handle to device context
        HDC mydc = GetDC(myconsole);
    
        int pixel =0;
    
        //Choose any color
        COLORREF COLOR= RGB(255,255,255); 
    
        //Draw pixels
        for(double i = 0; i < PI * 4; i += 0.05)
        {
            SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
            pixel+=1;
        }
    
        ReleaseDC(myconsole, mydc);
        cin.ignore();
        return 0;
    }
    

    You can also use some others libraries like: conio.h allegro.h sdl, etc.

提交回复
热议问题