console台应用程序---周星星写的
#include <windows.h>
namespace wincsl
{
using namespace std;
//把需要用到的一些"颜色"做成一个"颜色表",使用enum实现(简单轻便)
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 );
return 0;
}
=======================================================
把在这个小程序中用到的一些WINDOWS操作系统中的WIN32API和一些class(或者struct)的信息给出来,如下:
MSDN2001中可以查到
Character Attributes
Character attributes can be divided into two classes: color and DBCS. The following attributes are defined in the Wincon.h header file.
| Attribute | Meaning |
|---|---|
| FOREGROUND_BLUE | Text color contains blue. |
| FOREGROUND_GREEN | Text color contains green. |
| FOREGROUND_RED | Text color contains red. |
| FOREGROUND_INTENSITY | Text color is intensified. |
| BACKGROUND_BLUE | Background color contains blue. |
| BACKGROUND_GREEN | Background color contains green. |
| BACKGROUND_RED | Background color contains red. |
| BACKGROUND_INTENSITY | Background color is intensified. |
| COMMON_LVB_LEADING_BYTE | Leading byte. |
| COMMON_LVB_TRAILING_BYTE | Trailing byte. |
| COMMON_LVB_GRID_HORIZONTAL | Top horizontal. |
| COMMON_LVB_GRID_LVERTICAL | Left vertical. |
| COMMON_LVB_GRID_RVERTICAL | Right vertical. |
| COMMON_LVB_REVERSE_VIDEO | Reverse foreground and background attributes. |
| COMMON_LVB_UNDERSCORE | Underscore. |
The foreground attributes specify the text color. The background attributes specify the color used to fill the cell's background. The other attributes are used with DBCS.
An application can combine the foreground and background constants to achieve different colors. For example, the following combination results in bright cyan text on a blue background.
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE
If no background constant is specified, the background is black, and if no foreground constant is specified, the text is black. For example, the following combination produces black text on a white background.
BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED
Each screen buffer character cell stores the color attributes for the colors used in drawing the foreground (text) and background of that cell. An application can set the color data for each character cell individually, storing the data in the Attributes member of the CHAR_INFO structure for each cell. The current text attributes of each screen buffer are used for characters subsequently written or echoed by the high-level functions.
An application can use GetConsoleScreenBufferInfo to determine the current text attributes of a screen buffer and the SetConsoleTextAttribute function to set the character attributes. Changing a screen buffer's attributes does not affect the display of characters previously written. These text attributes do not affect characters written by the low-level console I/O functions (such as the WriteConsoleOutput or WriteConsoleOutputCharacter function), which either explicitly specify the attributes for each cell that is written or leave the attributes unchanged.
浙公网安备 33010602011771号