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

closed account (DEU9GNh0)
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 


Last edited on
closed account (DEU9GNh0)
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
closed account (zb0S216C)
This isn't an article. You should of posted this in either the lounge or the general C++ programming forum.
Last edited on
closed account (DEU9GNh0)
Tested in Visual C++ 6 and above
I don't like that it's not cross-platform...
To be fair, TC did say Windows in the title.
I know, I just believe everything should be cross-platform. I almost never use a library that only works on one platform. If it doesn't work on Windows AND Linux AND BSD AND Mac OS I usually won't use it. There are exceptions (e.g. when the program I'm writing doesn't need to support other OSes or when I'm writing something simple to play around).
I know, I just believe everything should be cross-platform. I almost never use a library that only works on one platform. If it doesn't work on Windows AND Linux AND BSD AND Mac OS I usually won't use it. There are exceptions (e.g. when the program I'm writing doesn't need to support other OSes or when I'm writing something simple to play around).


It depends. If you are working for your employer and they dictate it has to be Windows only then you got to do it, isn't it ?

It is only when the project is your own or hobby can you have the luxury to make a choice. This also explain why a lot of retirees can finally have something they can call their own for which they don't have when they are still working employees.

The irony of life I guess.
That huge chunk of << overloads could be simplified by including the stringstream library.

1
2
3
4
5
6
7
8
template<typename T>
inline console& operator<<(T& rhs){
    stringstream sstream(stringstring::in|stringstream::out);
    sstream<<rhs;
    string buff;
    sstream.str(buff);
    write(buff.c_str(),buff.length(),/* ? */);
    return *this;}

For the flush you could make a flush variable in console, make an overload for a function pointer, and have a something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
inline console& operator<<(void(*func)(console&)){
    func(*this);
    return *this;}

void flush_on(console& cons){
    cons.doflush=true;}

void flush_off(console& cons){
    cons.doflush=false;}

//I'm not really sure how to return function pointers, so I'll do this
typedef void(*flush_type)(console&);

flush_type flush(bool doflush){
    if(doflush){
        return flush_on;}
    else{
        return flush_off;}}
Topic archived. No new replies allowed.