Change color of selected text

Is there any library or code to change selected text to some color?
I know <windows.h> is working for changing color but it only change the whole thing to the color.
What do you mean by "selected text"? Are you using the console, or some form of text control (like a memo or editbox)?
I'm doing a project that uses array, I want to change the text of selected row or element inside the array, let's say a[1][5]='*', I want to change the '*' from default to some color, what does it mean by console and text control?
sorry I'm such a noob... how to put my array into
BOOL WINAPI SetConsoleTextAttribute(HANDLE,WORD); ??
You don't. That function changes the color that cout will write with. Every time you wish to change the color (examples: to highlight it and to return it to normal), use the SetConsoleTextAttribute() function.

Try compiling and executing the example program in the thread I linked for you to get an idea of how to use it and how it works.

It is good to see that you looked the function up. You'll find all the text attributes you can use there. (They are the same as the old EGA/VGA text color attributes, except that blink doesn't work in preference for intensity.)
To make life a bit simpler for myself when I use SetConsoleTextAttribute() to set the text color, I've written:

- an enum for the colors.

The enum uses the color consts which are defined in WinCon.h, e.g.

1
2
3
4
5
enum Color // inside a suitable namespace
{
    black = 0,
    white = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
...


- two little inline helper functions to do the work, which alllow be to set the text and background color separately [1]

Calling SetConsoleTextAttribute() directly is fine if you just want to set the text color and leave the background set to its default value. But if you want to change the text color without changing the background color, you need to do a little bit more work.

You need to get the current color attributes, change just the text or background color bits, and then set it. There is no GetConsoleTextAttribute, but you can get the current attributes using GetConsoleScreenBufferInfo.

Andy

[1] I have other versions of my little helper functions which work with Linux consoles (using ANSI escape codes). And an #ifdef _WIN32 to select the correct definition to compile in)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
while (n >= 1)
        {
            switch(action)
            {
                case(1):a[row-'A'+1][col] = 'X';break;
                case(2):a[row-'A'+1][col] = 'O';break;
            }
            
            col = col + 1;
            n = n - 1;
        }
        callfunction = seatingPlan();


this is part of my code, I want to set each case for one color, and the other text color all remains default white and background is default for the whole thing..
Getting color is not a problem, what matters me is changing the color of each case without changing the whole thing to that colour..

the given link I've seen the
"SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0xE4 );
cout << "\n\n\n\t\t\t\tHELLO!\n\n";"

I see how it works but it doesn't work for my case
You aren't paying attention. Here's something that will do exactly the kind of thing you want. You just have to use it correctly.

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <windows.h>
using namespace std;

struct colors_t
  {
  HANDLE hstdout;
  int    initial_colors;

  colors_t()
    {
    hstdout        = GetStdHandle( STD_OUTPUT_HANDLE );
    initial_colors = getcolors();
    }

  ~colors_t()
    {
    setcolors( initial_colors );
    }

  int getcolors() const
    {
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo( hstdout, &csbi );
    return csbi.wAttributes;
    }

  void setcolors( int color )
    {
    SetConsoleTextAttribute( hstdout, color );
    }

  void setfg( int color )
    {
    int current_colors = getcolors();
    setcolors( (current_colors & 0xF0) | (color & 0x0F) );
    }

  void setbg( int color )
    {
    int current_colors = getcolors();
    setcolors( ((color & 0x0F) << 4) | (current_colors & 0x0F) );
    }

  int getfg() const { return  getcolors()    & 0x0F; }
  int getbg() const { return (getcolors() >> 4) & 0x0F; }
  };

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 main()
  {
  colors_t colors;

  int row = 1;
  int col = 3;

  int matrix[ 5 ][ 5 ] = {
    { 0, 1, 2, 3, 4 },
    { 1, 2, 3, 4, 0 },
    { 2, 3, 4, 0, 1 },
    { 3, 4, 0, 1, 2 },
    { 4, 0, 1, 2, 3 }
    };

  cout << "row = " << row << "; col = " << col << endl;

  for (int r = 0; r < 5; r++)
    {
    for (int c = 0; c < 5; c++)
      {
      if ((r == row) && (c == col))
        colors.setfg( light_yellow );
      cout << matrix[ r ][ c ];
      colors.setcolors( colors.initial_colors );
      cout << " ";
      }
    cout << endl;
    }

  return 0;
  }

More useful info:
http://www.cplusplus.com/forum/beginner/5830/#msg25972
http://www.cplusplus.com/forum/beginner/10645/#msg49765

Good luck!
ah ok, I finally solved by using SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0xE4 ), thanks for the replies.
Topic archived. No new replies allowed.