Thread: How do I...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    How do I...

    How do I change the colour of my text in a DOS program (or is this possible...?)

    Thanks
    -Chris

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    It depends on your compiler. My DOS compiler, Borland 3.1, has a special cprintf() function that prints in color if you set it beforehand with textcolor() and textbackground(). I don't know how you'd do it with cout, though.

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Does anybody know how to do this with cout?

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    What compiler do you have? If the answer is VC++6, than you can't do it without building a Win32 MFC or ATL project. DOS is mostly unsupported now a days, infact you have to work in a Win32 console in VC++6.
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I have Dev C++ and Rhide (I can use both)

  6. #6
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    well, i've unfortunately had the experience that Dev-C++ doesn't allow interrupts... (unless someone found out... but i'm pretty sure that question is dead...) so, i never did end up switching from DJGPP to Dev-C++... (because i really liked the editor...) if you can find ways to access conventional memory using Dev-C++, i believe the address for the text is 0xb800:0000... and with DJGPP use int 10h... (a good resource for interrupts is Ralph Browns Interrupt List... by the way...)

    hth...
    hasafraggin shizigishin oppashigger...

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There are lots of defined constants in windows.h. For text colour and background colour the ones we are interested in are:-
    FOREGROUND_RED
    FOREGROUND_GREEN
    FOREGROUND_BLUE
    FOREGROUND_INTENSITY
    BACKGROUND_RED
    BACKGROUND_GREEN
    BACKGROUND_BLUE
    BACKGROUND_INTENSITY

    first you need to get the output handle for the console.Use GetStdHandle(STD_OUTPUT_HANDLE)
    for actually setting the colour we use SetConsoleTextAttribute(hConsole,DWORD colour)

    so to put it all together for instance to make red text on a bright blue background we can say:-
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),FOREGROUND_RED|BACKGROUND_BLUE|BACKGROUND_IN TENSITY);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    79
    Actually for some reason it works with cout << if you use clrscr() first (at least with borland command line, don't know with dev-cpp).

  9. #9
    Flikm
    Guest

    Question

    its all great but are there really only 3 colors?

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Not quite.... you can get 16 colours....

    All you do is 'or' the different constants together.... i.e.

    FOREGROUND_RED|FOREGROUND_GREEN should give you yellow etc.
    if you want a brighter colour or in FOREGROUND_INTENSITY etc.

    play around with them... by using different combinations of the constants you can get 16 different colours.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed