增强的控制台控制函数

    最近看到很多C语言初学者抱怨在TC中喜欢的gotoxy(int x, int y) 函数到VC或者其它编译器中用不了了,这样一来如果想在控制台程序中进行简单的屏幕输出控制就不好办了.典型的应用就是那个"臭名昭著"的"学生成绩管理系统",在输出文本菜单或者输入信息时必须采用自上而下的"流式布局"(注:借用这个词而已-:),很不方便.我到MSDN中搜索了一下,原来在Win32的控制台中也有相应的办法实现光标定位的功能,只是没有留下直接可用的接口函数.稍稍努力一下,就可以实现例如光标定位,改变文字颜色,改变背景颜色,清屏等控制台增强函数.代码如下:


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




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值