Check out this guide. Ich würde eine benutzerdefinierte Manipulator machen, damit ich so etwas wie tun könnte:
std::cout << "standard text" << setcolour(red) << "red text" << std::endl;
Here ‚s eine kleine Anleitung, wie Sie Ihren eigenen Manipulator zu implementieren.
Ein kurzes Codebeispiel:
#include <iostream>
#include <windows.h>
#include <iomanip>
using namespace std;
enum colour { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
struct setcolour
{
colour _c;
HANDLE _console_handle;
setcolour(colour c, HANDLE console_handle)
: _c(c), _console_handle(0)
{
_console_handle = console_handle;
}
};
// We could use a template here, making it more generic. Wide streams won't
// work with this version.
basic_ostream<char> &operator<<(basic_ostream<char> &s, const setcolour &ref)
{
SetConsoleTextAttribute(ref._console_handle, ref._c);
return s;
}
int main(int argc, char *argv[])
{
HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);
cout << "standard text" << setcolour(RED, chandle) << " red text" << endl;
cin.get();
}
Dieser Code funktioniert nicht wirklich, Ihre vorherige Version, die Sie gerade ersetzt haben aber tat. Ill Doppelscheck ich habe es richtig, obwohl –
@Brock Woolf: Ja, tut mir leid. Ich hatte ein Problem beim Kopieren des GRIFFS. – Skurmedel
Wunderbar, das funktioniert perfekt. Vielen Dank! : D –