c programing changing font size and colour

I would like to know how I change the colour of font and the size. The simple code below would be nice to see displayed on a white background and the text red and bigger. Is this possible in the command prompt window and if so how

1
2
3
4
5
6
7
8
9
10
11
12
 #include<stdio.h>

int main()
{
printf("\n");
	printf("------------\n");
	printf("HELLO WORLD\n");
	printf("------------\n");
	printf("\n");

return (0);
}
Well for starters.. it depends on the operating system if you are using os specific api's. You could also just use a library that has multiple colors.

Here's how you would change color in windows api( several ways but I'll overload the operator << to make things easy. )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<windows.h>
enum COLOR
{
    black , blue , green , cyan , red , magenta , brown , normal , darkgrey , lightblue , lightgreen , lightcyan , lightred , lightmagenta , yellow , white
};

std::ostream& operator<<( std::ostream &stm , const COLOR &c)
{
    HANDLE out_handle = GetStdHandle( STD_OUTPUT_HANDLE );
    SetConsoleTextAttribute( out_handle , c );
    return( stm );
}

int main()
{
    std::cout << red << "apple" << yellow << "banana" << std::endl;
}


*had too many spaces in code and looked funny
Last edited on
thank you thus far but this really confuses me. going back to the very basics the code is written in gvim 7.3 then compiled and run in the command prompt. So talking about os specific api's well I am lost. do I need to look at running a new compiler?

thanks

Rodney
The command prompt doesn't give your program pretty graphics options like 'font'. You are stuck with the font the user has chosen.

You can change the colors, though. From a recent posting:
http://www.cplusplus.com/forum/general/105393/#msg568794

Enjoy!
You don't know what os you are on piczim? And I showed you how to change the color of the text =p well one of the ways.
Duaos way is probably better to do also but a bit more complicated than mine IMO


hi giblet

I must sound really dumb to you not knowing the basics that you are asking.

I assume that OS is operating system I am using windows 7.

I write the code in vim -vi 7.3 I then compile it and run it in the cmd panel on windows.

I have been working through the book "C" For dummies and am going to a course here in Harare. Unfortunately the course assumes you know certain things already. So I am at a distinct disadvantage.

It is very frustrating putting code together and working through it and understanding only part of it and why certain things happen.

Ideally I would like to know the absolute basics so when asked what operating system I can answer without looking or sounding like a fool.
Relax, you are doing fine.

Part of the learning process is that you accept a few things on faith -- that is, you don't know how everything works yet. You will get there.


Both giblit and I have posted examples of how to mess with the colors in your program. All you have to do is cut and paste.

The code giblit gave works the same way mine does, only it does not clean up after itself when your program ends or give you background colors.
The trick is that call to SetConsoleTextAttribute(), which is the Windows API function do do just what you want.

Hope this helps.
closed account (EwCjE3v7)
You could get an IDE that has customisable themes like visual studio or codeblocks
So I can carry on writing the code in VIM7.3 and compiling in the windows CMD prompt. I will cut and paste the text and let you know how It goes from there

Thanks for the help I do get myself in a state when I don't understand.

Best wishes

Rodney
When I try to compile the code in the command prompt

I get several errors the first being no iostream: no such file or directory. Is this a unique to c++ directory.

I think a lot of the confusion I am causing is from the fact that I am starting to learn from C.

Or the command prompt compiler can not run this code. This is one of my challenges
Oh, holy crap! You're using C.

I didn't even notice. Here's something you can use in C:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <windows.h>

enum 
  {
  black,
  dark_blue,
  dark_green,
  dark_cyan,
  dark_red,
  dark_magenta,
  dark_yellow,
  light_gray,
  dark_gray,
  light_blue,
  light_green,
  light_cyan,
  light_red,
  light_magenta,
  light_yellow,
  white
  };

int getcolors()
  {
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(
    GetStdHandle( STD_OUTPUT_HANDLE ), 
    &csbi
    );
  return csbi.wAttributes;
  }

int getfgcolor()
  {
  return getcolors() & 0x0F;
  }

int getbgcolor()
  {
  return getcolors() >> 4;
  }

void setfgcolor( int color )
  {
  SetConsoleTextAttribute(
    GetStdHandle( STD_OUTPUT_HANDLE ), 
    (getcolors() & 0xF0) | (color & 0x0F)
    );
  }

void setbgcolor( int color )
  {
  SetConsoleTextAttribute(
    GetStdHandle( STD_OUTPUT_HANDLE ), 
    ((color & 0x0F) << 4) | (getcolors() & 0x0F)
    );
  }

void setcolors( int fg, int bg )
  {
  SetConsoleTextAttribute(
    GetStdHandle( STD_OUTPUT_HANDLE ), 
    ((bg & 0x0F) << 4) | (fg & 0x0F)
    );
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main()
  {
  int initial_fg_color = getfgcolor();
  int initial_bg_color = getbgcolor();

  printf("\n");
  setbgcolor( white );
  printf("------------\n");
  setfgcolor( light_red );
  printf("HELLO WORLD\n");
  setfgcolor( initial_fg_color );
  printf("------------\n");
  setbgcolor( initial_bg_color );
  printf("\n");

  setcolors( initial_fg_color, initial_bg_color );
  return 0;
  }

Hope this helps.
Thank you VERY much it worked my hello world was red text on a white background. This is fantastic now I can study this, Play with it and learn how to do it THANK YOU

Best wishes

Rodney
Topic archived. No new replies allowed.