小明思考

高性能服务器端计算
posts - 70, comments - 428, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Colorful life:让控制台五颜六色

Posted on 2005-12-05 10:49 小明 阅读(901) 评论(0)  编辑 收藏 引用 所属分类: Win32
当我回想起来我刚刚学习C语言,Turbo C2.0提供的丰富的函数,可以让枯燥的文本界面,显示出花花绿绿的文字界面。在windows时代,这些函数都不在标准库中。不过WINAPI可以帮你实现。

#include <windows.h>
#include 
<string>
#include 
<ctime>

enum Colors
{
    BLACK        
= 0,
    BLUE         
= 1,
    DARK_GREEN   
= 2,
    LIGHT_BLUE   
= 3,
    RED          
= 4,
    PURPLE       
= 5,
    ORANGE       
= 6,
    GREY         
= 7,
    DARKER_GREY  
= 8,
    MEDIUM_BLUE  
= 9,
    LIGHT_GREEN  
= 10,
    TEAL         
= 11,
    RED_ORANGE   
= 12,
    LIGHT_PURPLE 
= 13,
    YELLOW       
= 14,
    WHITE        
= 15
};

void set_cursor(short x, short y)
{
    COORD point 
= {x, y};
    ::SetConsoleCursorPosition(::GetStdHandle(STD_OUTPUT_HANDLE), point);
}

void set_color(unsigned short color)
{
    ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
}

void delay(unsigned int delay)
{
    ::Sleep(delay);
}

void set_title(std::string title)
{
    ::SetConsoleTitle(title.c_str());
}

void show_cursor(bool show, int size = 25)
{
    CONSOLE_CURSOR_INFO cci;
    
if (size <= 0) size = 1;
    
if (size > 100) size = 100;
    cci.dwSize 
= size;
    cci.bVisible 
= show;
    ::SetConsoleCursorInfo(::GetStdHandle(STD_OUTPUT_HANDLE), 
&cci);
}

void clear_screen()
{
    system(
"cls");
}

代码很简单,不用多作解释了

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理