在屏幕指定位置打印指定颜色字体
//根据用户的输入不同而在屏幕的指定位置打印指定颜色字体 //例如:用户输入:”12,34 RED Hello!“,就将字符串”Hello!“打印到屏幕坐坐标的12,34处,并将字体设为红色
#include <stdio.h> #include<window.h> void writeChar(int High,int wide,char* pszChar ,WORD wArr){ //设置光标属性 CONSOLB_CURSOR_INFO cci; cci.dwSize = 1; cci.bVisible = FALSE; // 不显示光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci); //设置坐标 COORD loc; loc.X = Wide * 2 ; //x的值是Wide的2倍 loc.Y = High; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wArr); SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc); printf(pszChar); //打印时需要注意有些特殊字符占两个字节 } int main(){ int nPosX; int nPosY; WORD wArr = FOREGROUND_RED; char strColour [ 10 ] ; char strContent [ 20 ]; scanf_s(”%d,%d %s%s“,&nPosX,&nPosY,strColour,10,strContent,20);
if (0 ==strcmp(strColour,L”BLUE“)){ wArr == FOREGROUND_BLUE; } else if (0 ==strcmp(strColour,L”GREEN“)){ wArr = FOREGROUND_GREEN; } else if ( 0 ==strcmp(strColour,L“RED”)==0){ wArr = FOREGROUND_RED; } writeChar( nPosY,nPosX, strContent,wArr); }