zoukankan      html  css  js  c++  java
  • 设置控制台(命令行)窗口 光标位置,及前背景颜色

    #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
    " );
      
    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("
    ");
    }
    
      //printf("
    
    ");
      //MoveCursorTo( 1, 9 );
      //ColorPrintf(0x0083,"This is a test
    ");
      return 0;
    }
    

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

  • 相关阅读:
    C++中整型变量的存储大小和范围
    A1038 Recover the Smallest Number (30 分)
    A1067 Sort with Swap(0, i) (25 分)
    A1037 Magic Coupon (25 分)
    A1033 To Fill or Not to Fill (25 分)
    A1070 Mooncake (25 分)
    js 获取控件
    C#代码对SQL数据库添加表或者视图
    JS 动态操作表格
    jQuery取得下拉框选择的文本与值
  • 原文地址:https://www.cnblogs.com/lzpong/p/3955889.html
Copyright © 2011-2022 走看看