java控制台光标_设置控制台(命令行)窗口 光标位置,及前背景颜色

这是一个关于如何在C++中改变控制台输出的颜色和移动光标的示例程序。通过定义不同的颜色常量,可以设置文字的前景色和背景色,并使用`SetConsoleTextAttribute`函数来改变颜色。同时,`MoveCursorTo`函数用于调整光标位置,方便在控制台上进行特定位置的输出。
摘要由CSDN通过智能技术生成

#include "stdafx.h"

#include

#include

/*

#define FOREGROUND_BLUE 0x0001 // text color contains blue.

#define FOREGROUND_GREEN 0x0002 // text color contains green.

#define FOREGROUND_RED 0x0004 // text color contains red.

#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.

#define BACKGROUND_BLUE 0x0010 // background color contains blue.

#define BACKGROUND_GREEN 0x0020 // background color contains green.

#define BACKGROUND_RED 0x0040 // background color contains red.

#define BACKGROUND_INTENSITY 0x0080 // background color is intensified.

*/

//更改当前输出的颜色(前景色/背景色)

void ColorPrintf(WORD cl,char* str)

{

static HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

//WORD wOldColorAttrs;

//CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

//First save the current color information

//GetConsoleScreenBufferInfo(h, &csbiInfo);

//wOldColorAttrs = csbiInfo.wAttributes;

//Set the new color information

SetConsoleTextAttribute ( h, cl );

printf ( str);

//Restore the original colors

//SetConsoleTextAttribute ( h, wOldColorAttrs);

SetConsoleTextAttribute(h, FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);

}

//移动输入光标位置

void MoveCursorTo(int x,int y)

{

static HANDLE m=GetStdHandle(STD_OUTPUT_HANDLE);

COORD cp={x,y};

SetConsoleCursorPosition(m,cp);

}

int main ( void )

{

char st[10];

ColorPrintf (FOREGROUND_BLUE | FOREGROUND_INTENSITY, "This is a color test\n" );

for (int j=0;j<255;j+=16)

{

for (int i=0;i<16;i++)

{

sprintf(st,"%02x ",j+i);

ColorPrintf(j+i,st);

}

printf("\n");

}

//printf("\n\n");

//MoveCursorTo( 1, 9 );

//ColorPrintf(0x0083,"This is a test\n");

return 0;

}

设置控制台(命令行)窗口 光标位置,及前背景颜色

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值