Text Colors

I've just started learning C++ this year, and am having a little trouble. I've been trying to get black as one of my colors, but I haven't been able to. I tried adding all three colors together, but it made it white. The code is below:
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
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
    /*Sets the window to full screen*/
    keybd_event(VK_MENU, 0x38, 0, 0);
    keybd_event(VK_RETURN, 0x1c, 0, 0);
    keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);
    
    
    HWND CurrentWin;
    CurrentWin = GetForegroundWindow();
    HANDLE consol;
    consol = GetStdHandle(STD_OUTPUT_HANDLE);
    
    /*Sets the text to red on a black background*/
    SetConsoleTextAttribute(consol,FOREGROUND_RED);
    cout << "Hello World\n";
    
    /*Sets the text to green on a black background*/
    SetConsoleTextAttribute(consol,FOREGROUND_GREEN); 
    cout << "Hello World\n";
    
    /*Sets the text to blue on a black background*/
    SetConsoleTextAttribute(consol,FOREGROUND_BLUE); 
    cout << "Hello World\n";
    
    /* Sets the text to bright green ("FOREGROUND_INTENSITY") and the    background to red */
    SetConsoleTextAttribute(consol,FOREGROUND_GREEN |FOREGROUND_INTENSITY| BACKGROUND_RED);
    cout << "BOO!\n";
    
    /*Sets the text to text to green on a bright blue background*/
    SetConsoleTextAttribute(consol,FOREGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY);
    cout << "HI!\n";
    

    SetForegroundWindow(CurrentWin);
    
    /* "Press any key to continue... */
    system("PAUSE");
    return EXIT_SUCCESS;
}


I use Dev C++, and I'm just not feeling it right now. Any suggestions?
Enjoy :)

SetConsoleTextAttribute(consol, 0);

Note: Be careful when using keybd_event(). While this works well when it's being run directly from a console; it can present errors when being run in an IDE . You should use #ifdef's and have different settings for your debug version than a release one.

Z.
Last edited on
Thanks so much Zaita. This is all kind of a work in progress, and eventually I'll learn about the #ifdef's and all that. Thanks again!
Topic archived. No new replies allowed.