[转载]更改windows控制台输出的字体颜色(前景色、背景色)和输出位置

#include <iostream>
#include <windows.h>
namespace wincsl
{
    using namespace std;

    enum clr{ FB = FOREGROUND_BLUE,
              FG = FOREGROUND_GREEN,
              FR = FOREGROUND_RED,
              FI = FOREGROUND_INTENSITY,
              BB = BACKGROUND_BLUE,
              BG = BACKGROUND_GREEN,
              BR = BACKGROUND_RED,
              BI = BACKGROUND_INTENSITY };

    class color
    {
    public:
        explicit color( WORD wAttributes = getcurrentvalue_() ) : wAttributes_(wAttributes)
        {
        }
        WORD getvalue( void ) const
        {
            return wAttributes_;
        }
    private:
        static WORD getcurrentvalue_( void )
        {
            CONSOLE_SCREEN_BUFFER_INFO csbi;
            ::GetConsoleScreenBufferInfo( ::GetStdHandle(STD_OUTPUT_HANDLE), &csbi );
            return csbi.wAttributes;
        }
        WORD wAttributes_;
    };
    const color setcolor( WORD wAttributes )
    {
        ::SetConsoleTextAttribute( ::GetStdHandle(STD_OUTPUT_HANDLE), wAttributes );
        return color(wAttributes);
    }
    const color setcolor( color clrAttributes )
    {
        return setcolor( clrAttributes.getvalue() );
    }
    ostream& operator<<( ostream& os, const color& wc )
    {
        return os;
    };
    istream& operator>>( istream& os, const color& wc )
    {
        return os;
    };

    class position
    {
    public:
        position( SHORT row, SHORT col ) : row_(row), col_(col)
        {
        }
        position( const position& pos = getcurrentvalue_() ) : row_(pos.row_), col_(pos.col_)
        {
        }
        SHORT getrow( void ) const
        {
            return row_;
        }
        SHORT getcol( void ) const
        {
            return col_;
        }
    private:
        static const position getcurrentvalue_( void )
        {
            CONSOLE_SCREEN_BUFFER_INFO csbi;
            ::GetConsoleScreenBufferInfo( ::GetStdHandle(STD_OUTPUT_HANDLE), &csbi );
            return position( csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y );
        }
        SHORT row_, col_;
    };
    const position setpos( SHORT row, SHORT col )
    {
        COORD coord = { col, row };
        ::SetConsoleCursorPosition( ::GetStdHandle(STD_OUTPUT_HANDLE), coord );
        return position( row, col );
    }
    const position setpos( position pos )
    {
        return setpos( pos.getrow(), pos.getcol() );
    }
    ostream& operator<<( ostream& os, const position& wc )
    {
        return os;
    };
    istream& operator>>( istream& os, const position& wc )
    {
        return os;
    };
}

#include <string>
int main( void )
{
    using namespace std;
    using namespace wincsl;

    color oldcolor;

    cout << setpos(1,15) << setcolor(FB|BG) << "输入一段文字吧" << endl;

    string s;
    cin >> setpos(5,10) >> setcolor(FG|BB) >> s;
    position curpos;
    cout << setcolor(oldcolor) << "*这里是" << curpos.getrow() << "行" << curpos.getcol() << "列" << endl;

    setcolor( oldcolor );
}
posted on 2009-12-23 20:33  心随风飘  阅读(1064)  评论(0)    收藏  举报