computing
  • 0

How To Create One Line In C++ a Colour

  • 0

Ok well i’m currently making a C++ program and i would like to know how to make a sort of menu kinda thing with one line in the M-DOS C++ Program like Grey or something

A Bit like Zork how it had a Grey Line at the Top

Share

1 Answer

  1. Here is a very quick example that wrapping up the API into
    simplified functions.

    They could be set up however they will best serve, it
    doesn’t need to ,or indeed probably shouldn’t,
    be set up exactly how I did it.

    Edit: I should also say that there are probably
    perfectly good libraries around to do exactly this.

    #include <windows.h>
    #include <iostream>
    
    bool GotoCell(int,int);
    bool ChangeColour(unsigned short, unsigned short);
    bool ColourLine(unsigned short, unsigned short, unsigned short);
    
    
    
    int main()
    {
        //set up a new console window
        FreeConsole();
        AllocConsole();
        
    
        ColourLine(14, 5, 2);
        ChangeColour(14,5);
        GotoCell(0,2);
        std::cout << "No propper bounds checking - returns false if there is an error";
        GotoCell(0,5);
        std::cout << "press enter to exit";
        
        
        std::cin.ignore();
        return 0;
    }
    
    
    
    bool GotoCell(int xpos, int ypos)
    {
        //Get the console output handle
        HANDLE hConsoleOutput;
        hConsoleOutput = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
        if (hConsoleOutput == INVALID_HANDLE_VALUE)
        {
            return false;
        }
        
        //set the cursor position
        COORD coCellCoord;
        coCellCoord.X = xpos;
        coCellCoord.Y = ypos;
        if (!SetConsoleCursorPosition(hConsoleOutput, coCellCoord))
        {
            return false;
        }
        return true;
    }
    
    bool ChangeColour(unsigned short FGC, unsigned short BGC)
    {
        //only 16 colours
        if (FGC > 15 || BGC > 15)
        {
            return false;
        }
    
        //Get the console output handle
        HANDLE hConsoleOutput;
        hConsoleOutput = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
        if (hConsoleOutput == INVALID_HANDLE_VALUE)
        {
            return false;
        }
    
        //change the colours
        if (!SetConsoleTextAttribute(hConsoleOutput, ((BGC & 0x0F) << 4) + (FGC & 0x0F)))
        {
            return false;
        }
        return true;
    }
    
    
    bool ColourLine(unsigned short FGC, unsigned short BGC, unsigned short ypos)
    {
        //only 16 colours
        if (FGC > 15 || BGC > 15)
        {
            return false;
        }
    
        //Get the console output handle
        HANDLE hConsoleOutput;
        hConsoleOutput = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
        if (hConsoleOutput == INVALID_HANDLE_VALUE)
        {
            return false;
        }
    
        //retrieve the console info    
        CONSOLE_SCREEN_BUFFER_INFO sbiConsoleInfo;
        if (!GetConsoleScreenBufferInfo(hConsoleOutput, &sbiConsoleInfo;) )
        {
            return false;
        }
    
        //change the top line
        DWORD dwWritten = 0;
        COORD coWriteCoord;
        coWriteCoord.X = 0;
        coWriteCoord.Y = ypos;
        if (!FillConsoleOutputAttribute(hConsoleOutput, ((BGC & 0x0F) << 4) + (FGC & 0x0F), sbiConsoleInfo.dwSize.X,  coWriteCoord, &dwWritten;))
        {
            return false;
        }
        return true;
    }
    
    

    • 0