color and background color

Just wondering the best way to do font color and background color. Finished my class in c++, but I want to sharpen my skills, but I don't know what else to make.

the standard language does not support color, you need a library for that. Which is fine: you can learn how to use a library and link it in etc ... try ncurses for this.

Font may not be doable at all. The console is often one font, and if you change it, you just change all the letters at once, like notepad would do.

How it's done depends on the operating system, and if you want to muck around with a console app or GUI.

There are a couple of articles here that show how to do it in a Windows console:

http://www.cplusplus.com/articles/2ywTURfi/
http://www.cplusplus.com/articles/Eyhv0pDG/

The 2nd example is the preferred version.

Windows GUI apps can be done with the Win32 SDK, GDI+ or DirectX. Direct2D is designed for not-full-screen GUI apps.

I don't do *nix or Mac, so I don't have a clue what resources are available to learn those systems or where to look.
Adding color to the console is cool and all, but if you're really trying to "sharpen your skills", simply adding a few coloring commands isn't really doing that, in my opinion.

I would rather start designing a somewhat-complex program, something you would be interested in creating and using.

You can also look into open-source projects and see how you can contribute to them. Every complex program either has bugs of some sort or things that can be improved.
For example, here's the list of bugs for Notepad++, a program which I use almost every day.
https://github.com/notepad-plus-plus/notepad-plus-plus/issues
https://notepad-plus-plus.org/contribute/
Last edited on
@Furry Guy

GUI for all major operating systems is typically done with a framework like Qt or WxWidgets (the result can then run on Windows, Linux, MAC, etc.)

@Cjigsaw...and that's likely where you are headed anyway.

Stroustrup points out in various discussions and lectures that modern students really need to "see GUI" for their own interests. He realized that as a teacher, modern students just lose too much interest if their programs don't have GUI. In his books he uses FLTK, because it is "simple" - too simple, really, but simple.

I've used WxWidgets on a lot of products and can recommend it. Qt may be perceived to be better, but not by much. It also has some minor licensing concerns that WxWidgets does not.

It is an important study, and one you're not expecting.

You've focused on text color and font selection. No doubt that is motivated by using a GUI based computer. However, GUI applications have at the very least one more important difference in how they are made compared to the console applications you're familiar with at this point. They are event driven. You've never seen that from the inside before.

That said, though you have completed a C++ course, I'm aware that your next phase in the study of the language is about classes (objects), inheritance, virtual functions.

I highly recommend that you find a copy of Stroustrup's C++ Principles and Practice. That will walk you through what you really need at this point.
Last edited on
@Nicollo,

Being a self-taught (badly) programmer hobbyist I kinda already knew the bare minimum basics of GUI frameworks, having played around a bit with FLTK courtesy of Stroustrup's book and glanced at the webpages of WxWidgets and Qt.

None of it all really sparked an interest for me. As bloated and over-burdensome as Win32 desktop API programming can be I'm somewhat comfortable with how it is supposed to work.

Being self-taught Win32/DirectX has the slight advantage of having numerous Learn How To books available. All rather mediocre, but make for somewhat of a decent overview.
Hello Cjigsaw,

If you are like me, you have finished what you were working on and want to do something fun.

Several years ago I found that when the lighting went dim in the evening that white on black was hard to read and that white on dark blue was easier to read.

Doing some research I found that the old DOS way of changing the colour was no longer available and went looking for a C++ way and found this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void SetColor(int foreground = 7, int background = 1);

void SetColor(int foreground, int background)
{
	WORD consoleColor;
	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;

	if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
	{
		//consoleColor = (csbi.wAttributes & 0xF0) + (ColorPicker & 0x0F);
		consoleColor = (foreground + (background * 16));
		SetConsoleTextAttribute(hStdOut, consoleColor);
	}
}  //  End SetColor() 

In the prototype I set a default foreground and background colour. This allows you to use SetColor(); to reset to the defaults. using just one number in the parameters will use the default background colour.

The advantages are that you can change the colour of each letter in a word, the colour of one or more words, a whole paragraph or even the whole screen if you follow the call to the function with some way of clearing the screen.

The biggest disadvantage is the it is not easy to use if you have many changes to make.

An example:
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
#include <iostream>
#include <string>
#include <Windows.h>

void SetColor(int foreground = 7, int background = 0);

int main()
{
	std::string word{ "test" }, word2{ "second" }, word3{ "Third" };

	SetColor();
	std::cout << "\n " << word[0];
	SetColor(8);
	std::cout << word[1];
	SetColor(9);
	std::cout << word[2];
	SetColor(10);
	std::cout << word[3];

	SetColor(11);
	std::cout << "\n\n " << word2;

	std::cout << "\n\n " << word << ' ';
	SetColor(12);
	std::cout << word2 << ' ';
	SetColor();
	std::cout << word3 << std::endl;

		// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	SetColor(14);
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;}

void SetColor(int foreground, int background)
{
	WORD consoleColor;
	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;

	if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
	{
		//consoleColor = (csbi.wAttributes & 0xF0) + (ColorPicker & 0x0F);
		consoleColor = (foreground + (background * 16));
		SetConsoleTextAttribute(hStdOut, consoleColor);
	}
}  //  End SetColor() 

Just to give you an idea.

Almost forgot. This is for a Windows OS if you have not figured that out yet.

Hope that helps,

Andy
Topic archived. No new replies allowed.