有人说这个

int main()
{
printf("控制台下默认文字背景颜色:\n");
printf("MoreWindows\n");
printf("-----------------------------------\n\n");

SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE);
printf("控制台下红字蓝底:\n");
printf("MoreWindows\n");
printf("-----------------------------------\n\n");

SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
printf("还原为控制台下默认文字背景颜色:\n");
printf("MoreWindows\n");
printf("-----------------------------------\n\n");
return 0;
}

但是有错误

>1.c(3): warning C4013: “printf”未定义;假设外部返回 int
1>1.c(7): warning C4013: “SetConsoleColor”未定义;假设外部返回 int
1>1.c(7): error C2065: “FOREGROUND_RED”: 未声明的标识符
1>1.c(7): error C2065: “FOREGROUND_INTENSITY”: 未声明的标识符
1>1.c(7): error C2065: “BACKGROUND_BLUE”: 未声明的标识符
[code=C/C++][/code]1>1.c(12): error C2065: “FOREGROUND_RED”: 未声明的标识符
1>1.c(12): error C2065: “FOREGROUND_GREEN”: 未声明的标识符
1>1.c(12): error C2065: “FOREGROUND_BLUE”: 未声明的标识

1个回答

#include <iostream>
#include <windows.h>



bool SetConsoleColor(WORD forceGroundColor, WORD backGroundColor)

{

  HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  if (handle == 0)
  return false;

  BOOL ret = SetConsoleTextAttribute(handle, forceGroundColor | backGroundColor);
  return(ret == TRUE);
}

bool SetConsoleColor(WORD Color)
{
  HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  if (handle == 0)
  return false;

  BOOL ret = SetConsoleTextAttribute(handle, Color);
  return(ret == TRUE);
}

int main()
{
printf("控制台下默认文字背景颜色:\n");
printf("MoreWindows\n");
printf("-----------------------------------\n\n");

SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE);
printf("控制台下红字蓝底:\n");
printf("MoreWindows\n");
printf("-----------------------------------\n\n");

SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);   
printf("还原为控制台下默认文字背景颜色:\n");
printf("MoreWindows\n");
printf("-----------------------------------\n\n");
return 0;
}