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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "stdafx.h"
 
#include <stdio.h>
#include <windows.h>
 
/*
#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;
}

 

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

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。 原文链接:https://www.cnblogs.com/lzpong/p/3955889.html
posted @   _Ong  阅读(2141)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示