【问题标题】:Changing Background and text colour in a Windows console, c++在 Windows 控制台中更改背景和文本颜色,C++
【发布时间】:2014-08-20 10:06:15
【问题描述】:

我已使用以下代码更改控制台的颜色。但它也会突出显示文本。我不希望我的文本被突出显示并且想要白色背景。

    using namespace std;
#include<conio.h>
#include<stdlib.h>
#include <iostream>
#include <windows.h>
using namespace std;

//returns the current attributes
WORD GetConsoleTextAttribute (HANDLE hCon)
{
  CONSOLE_SCREEN_BUFFER_INFO con_info;
  GetConsoleScreenBufferInfo(hCon, &con_info);

  return con_info.wAttributes;
}

int main (void)
{
  system("color F1");

  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  const int saved_colors = GetConsoleTextAttribute(hConsole);



  SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
  cout << "This text should be blue" << endl;
  SetConsoleTextAttribute(hConsole, saved_colors);
  SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
  cout << "This text should be green" << endl;
  SetConsoleTextAttribute(hConsole, saved_colors);
  getch();
  return 0;
}

【问题讨论】:

    标签: c++ windows-console


    【解决方案1】:

    自己制作白色背景

    WORD whiteBack = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY | whiteBack);
    cout << "This text should be blue" << endl;
    SetConsoleTextAttribute(hConsole, saved_colors);
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY | whiteBack);
    cout << "This text should be green" << endl;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-01
      • 2011-09-21
      • 2012-01-07
      • 2014-01-10
      • 2020-10-06
      • 2018-12-25
      • 1970-01-01
      相关资源
      最近更新 更多