Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: setting text color for a consol C prog



jmh wrote:
I know I can change foregound and background color in DOS using ANSI escapes and I'm wonding if and how to do this with C. I'm just learning it and don't really have any programming background (hack a few things in DOS or bash or BASIC). Can someone point me to some info?

TIA

jmh


Well, assuming you're in a position in which you want to do this soley with ANSI escapes in C, and not in need of any mentioned interrupts and such as generously mentioned by Ed; I'm feeling charitable this morning, and going to provide you with a nice, simple example that will do what you want (I hope), in an alternative way that should with some luck be just fine for you.


Here's a set of definitions for ANSI sequences that I have found very useful over the years; this is pulled out of some old test application of mine, for your convenience:

/*
 * "Bright" ANSI sequences.
 */
#define COL_BRIGHT_RED          ""
#define COL_BRIGHT_BLUE         ""
#define COL_BRIGHT_YELLOW       ""
#define COL_BRIGHT_CYAN         ""
#define COL_BRIGHT_WHITE        ""
#define COL_BRIGHT_GREEN        ""
#define COL_BRIGHT_PURPLE       ""

/*
 * "Normal" ANSI sequences.
 */
#define COL_RED                 ""
#define COL_BLUE                ""
#define COL_YELLOW              ""
#define COL_CYAN                ""
#define COL_WHITE               ""
#define COL_GREEN               ""
#define COL_PURPLE              ""

/*
 * Others.
 */
#define COL_DARK_GREY           ""
#define COL_RESET               ""

Easily useable in your applications, for example:

#include <stdio.h>
#include <stdlib.h>

#include "mycolourdefs.h"

int
main(int argc, char* argv[])
{
        fprintf(stdout,
                "%sColourful %sExample!%s",
                COL_BRIGHT_RED,
                COL_BRIGHT_YELLOW,
                COL_RESET);
        return 0;
}

Have a good one, and good luck with your ANSI adventures :).

-----------------
James Skarzinskas
[EMAIL PROTECTED]




<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.