win32 set console text color

本文介绍了如何利用C++和控制台颜色API来为控制台游戏添加特色,包括使用ASCII艺术和颜色组合创建自定义RPG游戏。通过简单的代码示例,展示了如何在不同颜色背景下打印文本,从而实现个性化控制台输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Setting console text color is a great way to add spice to your console games. Using a combination of ASCII art and Console coloring you can even make your own RPG game. It's very simple to do, all you need is windows.h.

Most of the colors go from 1 to 16, and after that you get background colors mixed with foreground colors (if you wanted to experiment just cast your number as (Color)).

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

using namespace std;
HANDLE hCon;

enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };

void SetColor (Color c ) {
        if (hCon == NULL )
                hCon = GetStdHandle (STD_OUTPUT_HANDLE );
        SetConsoleTextAttribute (hCon, c );
}

int main ( ) {
  SetColor (RED );
  cout << "InfernoDevelopment.com\n";
  SetColor (DARKRED );
  cout << "Join our forums at www.infernodevelopment.com/forum\n";
  cin. get ( );
  return 0;
}

The SetColor function uses SetConsoleTextAttribute and GetStdHandle to grab the handle to your console and set the current color requested by the Color enum.

After you set the color, you can then use cout like a normal console to print the colored text.

It can really impress your friends who have probably never even seen console text color in their lives!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值