zoukankan      html  css  js  c++  java
  • VC++中控制控件台字体颜色(转)

     

    #include "windows.h"
    #include 
    <iostream>
    #include 
    <string>
    #include 
    "head.h"

    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;
    }
    ;


    int _tmain(int argc, _TCHAR* argv[])
    {
        
    using namespace std;
        color oldcolor;
        cout 
    << setcolor(FG) << "输入一段文字吧" << endl;
        
    //cout << setpos(1,15) << setcolor(FB) << "输入一段文字吧" << endl;

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

        setcolor( oldcolor );
        
    static int a;
        cout
    <<a<<endl;
        getchar();
        
        
    return 0;
    }

  • 相关阅读:
    作业29——制作首页的显示列表。
    作业28——发布功能完成。
    作业27——登录之后更新导航
    作业26——完成登录功能,用session记住用户名
    作业25——完成注册功能
    作业24——通过用户模型,对数据库进行增删改查操作。
    作业23——连接mysql数据库,创建用户模型
    作业22——管理信息系统的开发与管理
    作业——21加载静态文件,父模板的继承和扩展
    值类型与引用类型区别
  • 原文地址:https://www.cnblogs.com/myopq/p/581548.html
Copyright © 2011-2022 走看看