退房this guide。我會做一個自定義的操縱,所以我可以做這樣的事情:
std::cout << "standard text" << setcolour(red) << "red text" << std::endl;
Here的關於如何實現自己的手小導。
一個快速的代碼示例:
#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();
}
此代碼實際上並不工作,但您剛更換的以前的版本卻沒有。生病雙重檢查,雖然我有,但 – 2009-05-22 19:41:55