/*********************************************************
** File: pcc32.h
** Author: HUST.RedOC
** Email: redoc@foxmail.com
** Notes: Some functions about Win32 Console Control.
** Licence: Copyleft. Enjoy it Just for fun.
** Date: 2008-12-17 00:28:39
***********************************************************/
#ifndef PCC32_H_INCLUDED
#define PCC32_H_INCLUDED
#include <stdlib.h>
#include <windows.h>
#define true 1
#define false 0
typedef unsigned char uint8;
typedef unsigned int uint32;
typedef uint8 bool;
/* 颜色定义 */
typedef enum _PCCOLOR{
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
}PCCOLOR;
/* 延时,以毫秒计 */
void delayMS(uint32 d);
/* 清除文字 */
void clearText(void);
/* 设置文本颜色,0~15 */
int setTextColor(uint8 fColor);
/* 获取文本颜色,0~15 */
PCCOLOR getTextColor(void);
/* 设置文本背景颜色,0~15 */
int setBackColor(uint8 bColor);
/* 获取文本背景颜色,0~15 */
PCCOLOR getBackColor(void);
/* 设置文本及其背景颜色,0~15*/
int setColors(uint8 fColor, uint8 bColor);
/* 设置/取消前/背景颜色的交换解析*/
int setSwapColors(bool b);
/* 设定/取消文字的下划线 */
int setUnderLine(bool b);
/* 获取控制台文本行的最大长度[默认为80]*/
uint8 getLineWidth(void);
/* 获取光标的横坐标[列数] */
uint8 getCursorX(void);
/* 获取光标的纵坐标[行数] */
uint8 getCursorY(void);
/* 屏幕光标定位,x为列(横),y为行(纵)*/
int gotoTextPos(uint8 x, uint8 y);
/* 设置光标的可见性 */
int setCursorVisible(bool b);
/* 设置光标的(厚度)尺寸,1-100*/
int setCursorSize(uint8 s);
/* 获取控制台的标题字符串 */
int getConsoleTitle(char *title, uint8 len);
/* 设置控制台的标题字符串 */
int setConsoleTitle(char *title);
#endif // PCC32_H_INCLUDED
/* - - - - - - - - - - - - - - - 华丽的文件分隔线 - - - - - - - - - - - - - - - */
/**********************************************************
* File: pcc32.h *
* Author: HUST.RedOC *
* Email: redoc@foxmail.com *
* Notes: Some functions about Win32 Console Control. *
* Licence: Copyleft. Enjoy it Just for fun. *
* Date: 2008-12-17 00:28:39 *
**********************************************************/
#include "pcc32.h"
void delayMS(uint32 d)
{
Sleep(d);
return ;
}
void clearText(void)
{
system("cls");
return ;
}
int setTextColor(uint8 fColor)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(hd, &csbInfo);
return SetConsoleTextAttribute(hd, fColor | (csbInfo.wAttributes&~0x0F));
}
PCCOLOR getTextColor(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo);
return (PCCOLOR)(csbInfo.wAttributes&0x0F);
}
int setBackColor(uint8 bColor)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(hd, &csbInfo);
return SetConsoleTextAttribute(hd, (bColor <<4) | (csbInfo.wAttributes&~0xF0));
}
PCCOLOR getBackColor(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo);
return (PCCOLOR)((csbInfo.wAttributes&0xF0) >>4);
}
int setColors(uint8 fColor, uint8 bColor)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(hd, &csbInfo);
return SetConsoleTextAttribute(hd, fColor | (bColor <<4) | (csbInfo.wAttributes&~0xFF));
}
int setSwapColors(bool b)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(hd, &csbInfo);
if (!!b)
return SetConsoleTextAttribute(hd, csbInfo.wAttributes |0x4000);
else
return SetConsoleTextAttribute(hd, csbInfo.wAttributes & ~0x4000);
}
int setUnderLine(bool b)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(hd, &csbInfo);
if (!!b)
return SetConsoleTextAttribute(hd, csbInfo.wAttributes |0x8000);
else
return SetConsoleTextAttribute(hd, csbInfo.wAttributes & ~0x8000);
}
uint8 getLineWidth(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo);
return csbInfo.dwSize.X;
}
uint8 getCursorX(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo);
return csbInfo.dwCursorPosition.X;
}
uint8 getCursorY(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbInfo);
return csbInfo.dwCursorPosition.Y;
}
int gotoTextPos(uint8 x, uint8 y)
{
COORD cd;
cd.X = x;
cd.Y = y;
return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cd);
}
int setCursorVisible(bool b)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ccInfo;
GetConsoleCursorInfo(hd, &ccInfo);
ccInfo.bVisible = !!b;
return SetConsoleCursorInfo(hd, &ccInfo);
}
int setCursorSize(uint8 s)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ccInfo;
GetConsoleCursorInfo(hd, &ccInfo);
ccInfo.dwSize = s;
return SetConsoleCursorInfo(hd, &ccInfo);
}
int getConsoleTitle(char *title, uint8 len)
{
return GetConsoleTitle(title, len);
}
int setConsoleTitle(char *title)
{
return SetConsoleTitle(title);
}
//End of pcc32.c
/* - - - - - - - - - - - - - - - 华丽的文件分隔线 - - - - - - - - - - - - - - - */
上面有两个文件,一个头文件,一个C源文件.使用的时候把它们都放在你和编译器都可以找到的地方,然后包含这个头文件就可以了.下面再给出一个示例:
#include <stdio.h>
#include <stdlib.h>
#include <pcc32.h>
int main()
{
int i;
PCCOLOR tc = RED, bc = BLUE;
clearText();
for (i = 0; i < 16; i++)
{
gotoTextPos(i*2, i);
setColors(i, 15 - 0);
printf("%2d: Hello world!\n", i);
}
setColors(tc, bc);
setUnderLine(true);
puts("Test...I have an UnderLine.");
setUnderLine(false);
puts("Test...Now it's None.");
gotoTextPos(40, 20);
puts("Test...I'm at (40, 50).");
printf("MaxLineWidth: %d\n", getLineWidth());
setColors(PURPLE, LIGHT_GREEN);
setSwapColors(true);
puts("Reverse.");
printf("TextColor: %d\tBackColor:%d\n", getTextColor(), getBackColor());
setSwapColors(false);
puts("Recover.");
printf("TextColor: %d\tBackColor:%d\n", getTextColor(), getBackColor());
setCursorVisible(false);
puts("Now the cursor is NONE.");
getchar();
setCursorVisible(true);
setCursorSize(50);
puts("Now the cursor is a little fat with size 50...");
delayMS(5000);
return 0;
}