Hack Forums
[C] play with colors in console with ansi C - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] play with colors in console with ansi C (/showthread.php?tid=4990064)



play with colors in console with ansi C - sexytampaxlover - Sep 16, 2015

Hi there guys
im here cuz i want to know which simple mode i can use to change color of my cmd window by using ansi c.
I dont want to use system(color fd); i want something which dont call the system ,something purely by c
like in this example here :
Code
#include <iostream>
#include <windows.h>

int main()
{
    const WORD colors[] =
        {
        0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F,
        0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6
        };

    HANDLE hstdin  = GetStdHandle( STD_INPUT_HANDLE  );
    HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    WORD   index   = 0;

    // Remember how things were when we started
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo( hstdout, &csbi );

    // Tell the user how to stop
    SetConsoleTextAttribute( hstdout, 0xEC );
    std::cout << "Press any key to quit.\n";

    // Draw pretty colors until the user presses any key
    while (WaitForSingleObject( hstdin, 100 ) == WAIT_TIMEOUT)
    {
        SetConsoleTextAttribute( hstdout, colors[ index ] );
        std::cout << "\t\t\t\t Hello World \t\t\t\t" << std::endl;
        if (++index > sizeof(colors)/sizeof(colors[0]))
            index = 0;
    }
    FlushConsoleInputBuffer( hstdin );

    // Keep users happy
    SetConsoleTextAttribute( hstdout, csbi.wAttributes );
    return 0;
}
I dont understood very well this example ,because i need one simple ,just for change the background and foreground color ,like im doing this in ordinary cmd window by typing color fd .


* - Corios - Sep 16, 2015

You're not going to get a platform independent way. And you already posted up a windows solution, what's the issue. If you don't understand it, Google it lol?


* - sexytampaxlover - Sep 16, 2015

(Sep 16, 2015 03:48 PM)Corios Wrote:  You're not going to get a platform independent way. And you already posted up a windows solution, what's the issue. If you don't understand it, Google it lol?

1° this is in c++ im asking about ANSI C
2° i barely understood what this code are doing i've also compiled it and this is not mine stuff.
3° i think you misunderstood me ,i ask about how to do this in c like im doing it on cmd windows by typing color fd or color 84.

At least i mean that i dont need something of special just an line of c code which change my color inside my console program.
This is all ,if you not yet understand me just leave me in peace
Thanks to all


* - RISC - Sep 16, 2015

(Sep 16, 2015 04:35 PM)sexytampaxlover Wrote:  1° this is in c++ im asking about ANSI C
2° i barely understood what this code are doing i've also compiled it and this is not mine stuff.
3° i think you misunderstood me ,i ask about how to do this in c like im doing it on cmd windows by typing color fd or color 84.

At least i mean that i dont need something of special just an line of c code which change my color inside my console program.
This is all ,if you not yet understand me just leave me in peace
Thanks to all

1: The only thing that's C++ about your code is the iostream and cout.
The rest, I don't need to answer.


* - Masm4ever - Sep 17, 2015

Simple code in C, that's a challenge, of course... is ConsoleColor(cRed, cYellow); simple enough?

Code
#include <stdio.h>
#include <windows.h>
#pragma comment(linker, "/subsystem:console" )

#define cBlack    0
#define cDarkBlue    1
#define cDarkGreen    2
#define cDarkCyan    3
#define cDarkRed    4
#define cDarkMagenta    5
#define cDarkYellow    6
#define cGray        7
#define cDarkGray    8
#define cBlue        9
#define cGreen    10
#define cCyan        11
#define cRed        12
#define cMagenta    13
#define cYellow    14
#define cWhite    15
#define ConsoleColor(fcol, bcol)  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fcol+16*bcol);

int main(int argc, char* argv[]) {
  printf("Hello world, default colours\n");
  ConsoleColor(cRed, cYellow);
  printf("Hello world, red on yellow\n");
  ConsoleColor(cBlue, cWhite);
  printf("Hello world, blue on white\n");
  ConsoleColor(cRed, cBlue);
  printf("Hello world, red on blue \n");
  _getch();
}



* - Benoit ᵇᵃˡˡˢ - Sep 17, 2015

(Sep 16, 2015 05:42 PM)RISC Wrote:  1: The only thing that's C++ about your code is the iostream and cout.
The rest, I don't need to answer.

Are you sure you have no idea how to convert that into C?
hint: std::cout == printf


* - RISC - Sep 17, 2015

(Sep 17, 2015 08:02 AM)๖ۣۜKeDDy Wrote:  Are you sure you have no idea how to convert that into C?
hint: std::cout == printf

Are you retarded? When did I say I didn't know how to convert it to C?
I was making a point at how obvious and stupid the OP's question was. Which is why I said the code is practically C as is, the only thing that's C++ is iostream and cout. Stupid shit.

Edit: I remember you. You're the idiot I shut down continuously for your lack of knowledge.
I'm the reason you have "Professional Skid and Retard" as your title.


* - sexytampaxlover - Sep 19, 2015

Maybe im stupid and dumbass ,but you need to know that im an clean beginner ,so you cant harass me about my lacks of knowledge.


ps
im study from an book and in that books there's no explanation about how the color working in this kind of case
So thanks to all


* - Kantos - Sep 19, 2015

Just use ncurses, don't bother dabbling with all this stuff.


* - sexytampaxlover - Sep 19, 2015

(Sep 19, 2015 04:44 PM)Kantos Wrote:  Just use ncurses, don't bother dabbling with all this stuff.

no problem buddy,but for me is hard because im usind modo 3d on windows


* - Masm4ever - Sep 19, 2015

(Sep 19, 2015 04:44 PM)Kantos Wrote:  Just use ncurses, don't bother dabbling with all this stuff.

What's wrong with the simple ConsoleColor macro I posted above - it doesn't work on your machine?