• Articles
  • Powerful Console in Windows/VC++ (Color,
Published by
Feb 22, 2011 (last update: Feb 22, 2011)

Powerful Console in Windows/VC++ (Color, Cursor, ...)

Score: 3.9/5 (50 votes)
*****
Powerful Console in Windows/VC++ (Color, Cursor, ...)

Console.h
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// Console for Windows, Tested in Visual C++ 6
//

#ifndef __Console__
#define __Console__
#include <windows.h>
#include <stdio.h>

const char el = '\n';

class Console
{
public:
    Console(const char *title = "", int x = 0, int y = 0, int w = 100, int h = 100);
    virtual ~Console();

    inline Console & operator<<(bool);
    inline Console & operator<<(const char *);
    inline Console & operator<<(const unsigned char *);
    inline Console & operator<<(const signed char *);
    inline Console & operator<<(char);
    inline Console & operator<<(unsigned char);
    inline Console & operator<<(signed char);
    inline Console & operator<<(short);
    inline Console & operator<<(unsigned short);
    inline Console & operator<<(int);
    inline Console & operator<<(unsigned int);
    inline Console & operator<<(long);
    inline Console & operator<<(unsigned long);
    inline Console & operator<<(float);
    inline Console & operator<<(double);
    inline Console & operator<<(long double);
    inline Console & operator<<(const void *);

    void setCursor(int x, int y);
    void setColor(int textcolor, int backcolor);
    void clear(char ch = ' ');
    bool flush();
    bool allocate();
    bool setTitle(const char *title);
    bool free();
    void fillColor(int x, int y, int w, int h, char ch = 0);
    void box(int x, int y, int w, int h);
    void setCursorSize(unsigned size, bool visible);
    CONSOLE_SCREEN_BUFFER_INFO getInfo();
    bool writeActive;

private:
    inline void write(const char *buffer, int size, bool flushW = false);
    inline void write(const unsigned char ch, bool flushW = false);
    inline void write(const char *str, bool flushW = false);
    inline void writeln(const char *str, bool flushW = false);

    static HANDLE cConsole;
    CONSOLE_SCREEN_BUFFER_INFO cCSBI;
    bool busy;
    int cTextColor, cBackColor;
};

inline Console& Console::operator<<(const char *x)
{
    write(x);
    return *this;
}

inline Console& Console::operator<<(const unsigned char *x)
{
    write((const char *) x);
    return *this;
}

inline Console& Console::operator<<(const signed char *x)
{
    write((const char *) x);
    return *this;
}

inline Console& Console::operator<<(bool x)
{
    write(x ? "True" : "False");
    return *this;
}

inline Console& Console::operator<<(char x)
{
    write(x);
    return *this;
}

inline Console& Console::operator<<(unsigned char x)
{
    write(x);
    return *this;
}

inline Console& Console::operator<<(signed char x)
{
    write(x);
    return *this;
}

inline Console& Console::operator<<(short x)
{
    char a[20];
    itoa(x, a, 10);
    write(a);
    return *this;
}

inline Console& Console::operator<<(unsigned short x)
{
    char a[20];
    itoa(x, a, 10);
    write(a);
    return *this;
}

inline Console& Console::operator<<(int x)
{
    char a[50];
    itoa(x, a, 10);
    write(a);
    return *this;
}

inline Console& Console::operator<<(unsigned int x)
{
    char a[50];
    itoa(x, a, 10);
    write(a);
    return *this;
}

inline Console& Console::operator<<(long x)
{
    char a[50];
    ltoa(x, a, 10);
    write(a);
    return *this;
}

inline Console& Console::operator<<(unsigned long x)
{
    char a[50];
    _ultoa(x, a, 10);
    write(a);
    return *this;
}

inline Console& Console::operator<<(float x)
{
    char a[50];
    sprintf(a, "%f", x);
    write(a);
    return *this;
}

inline Console& Console::operator<<(double x)
{
    char a[70];
    sprintf(a, "%f", x);
    write(a);
    return *this;
}

inline Console& Console::operator<<(long double x)
{
    char a[70];
    sprintf(a, "%f", x);
    write(a);
    return *this;
}

inline Console& Console::operator<<(const void *x)
{
    return *this;
}

inline void Console::write(const char *buffer, int size, bool flushW)
{
    if (!writeActive)
        return;

    unsigned long int wr;
    WriteConsole(cConsole, buffer, size, &wr, NULL);

    if (flushW)
        flush();
}

inline void Console::writeln(const char *str, bool flushW)
{
    if (!writeActive)
        return;

    unsigned long int wr;
    WriteConsole(cConsole, str, strlen(str), &wr, NULL);
    WriteConsole(cConsole, "\n", 1, &wr, NULL);

    if (flushW)
        flush();
}

inline void Console::write(const unsigned char ch, bool flushW)
{
    if (!writeActive)
        return;

    unsigned long int wr;
    WriteConsole(cConsole, &ch, 1, &wr, NULL);

    if (flushW)
        flush();
}

inline void Console::write(const char *str, bool flushW)
{
    if (!writeActive)
        return;

    unsigned long int wr;
    WriteConsole(cConsole, str, strlen(str), &wr, NULL);

    if (flushW)
        flush();
}
#endif 



Console.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

#include "Console.h"

HANDLE Console::cConsole = NULL;

BOOL WINAPI consoleHandler(DWORD dwCtrlType)
{
    return true;
}

Console::Console(const char *title, int x, int y, int w, int h)
{
    allocate();
    if (!cConsole)
    {
        cConsole =
            CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
                                      FILE_SHARE_READ | FILE_SHARE_READ,
                                      NULL, CONSOLE_TEXTMODE_BUFFER, 0);
    }
    COORD coord = {w, h};
    SetConsoleScreenBufferSize(cConsole, coord);
    SMALL_RECT conWin = {x, y, w, h};
    SetConsoleWindowInfo(cConsole, true, &conWin);
    SetConsoleCtrlHandler(consoleHandler, true);
    getInfo();
    setTitle(title);
    SetConsoleActiveScreenBuffer(cConsole);
    writeActive = true;
    busy = false;
}

Console::~Console()
{
    free();
    CloseHandle(cConsole);
}

void Console::setCursor(int x, int y)
{
    COORD coord = {x, y};
    SetConsoleCursorPosition(cConsole, coord);
}

void Console::setColor(int textcolor, int backcolor)
{
    WORD wAttributes = ((unsigned int) backcolor << 4) | (unsigned int) textcolor;
    SetConsoleTextAttribute(cConsole, wAttributes);
}

void Console::clear(char ch)
{
    DWORD cCharsWritten;
    DWORD size = cCSBI.dwSize.X * cCSBI.dwSize.Y;
    COORD coord = {0, 0};
    FillConsoleOutputCharacter(cConsole, ch, size, coord, &cCharsWritten),
            FillConsoleOutputAttribute(cConsole, cCSBI.wAttributes, size, coord, &cCharsWritten),
            SetConsoleCursorPosition(cConsole, coord);
}

bool Console::flush()
{
    SetConsoleActiveScreenBuffer(cConsole);
    return (FlushConsoleInputBuffer(cConsole) == TRUE) ? true : false;
}

bool Console::allocate()
{
    return (AllocConsole() == TRUE) ? true : false;
}

bool Console::free()
{
    return (FreeConsole() == TRUE) ? true : false;
}

void Console::fillColor(int x, int y, int w, int h, char ch)
{
    DWORD cCharsWritten;
    COORD coord;
    coord.X = x;
    getInfo();
    for (int i = 0; i < h; i++)
    {
        coord.Y = i + y;
        if (ch)
            FillConsoleOutputCharacter(cConsole, ch, w, coord, &cCharsWritten);
        FillConsoleOutputAttribute(cConsole, cCSBI.wAttributes, w, coord, &cCharsWritten);
    }
}

void Console::box(int x, int y, int w, int h)
{
    int k;
    if (!h && !w)
        return;
    setCursor(x, y);
    if (!h)
    {
        for (k = 0; k < w; k++)
        {
            setCursor(x + k, y);
            write(196, false);
        }

        return;
    }
    else
        if (!w)
    {
        for (k = 0; k < h; k++)
        {
            setCursor(x, y + k);
            write(179, false);
        }

        return;
    }

    write(218, false);

    for (k = 1; k < w - 1; k++)
    {
        setCursor(x + k, y);
        write(196, false);
        setCursor(x + k, y + h - 1);
        write(196, false);
    }
    write(217, false);
    for (k = 1; k < h - 1; k++)
    {
        setCursor(x, y + k);
        write(179, false);
        setCursor(x + w - 1, y + k);
        write(179, false);
    }
    setCursor(x + w - 1, y);
    write(191, false);
    setCursor(x, y + h - 1);
    write(192, false);

    flush();
}

void Console::setCursorSize(unsigned size, bool visible)
{
    CONSOLE_CURSOR_INFO cursorInfo;
    cursorInfo.bVisible = visible;
    cursorInfo.dwSize = (size < 100) ? size : 100;
    SetConsoleCursorInfo(cConsole, &cursorInfo);
}

bool Console::setTitle(const char *title)
{
    return (SetConsoleTitle(title) == TRUE) ? true : false;
}

CONSOLE_SCREEN_BUFFER_INFO Console::getInfo()
{
    GetConsoleScreenBufferInfo(cConsole, &cCSBI);
    return cCSBI;
}


Usage:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Console.h"
int main()
{
   Console console;

   console.setColor(14, 0);
   console.setCursor(23, 13);
   console << "Console" << 123 << '.';
   console.setColor(10, 0);
   console.box(0, 14, 65, 0);
   return 0;
}


Masoud
Tested in Visual C++ 6 and above