如何使控制台的输出带上颜色呢?

verlongan 2005-11-28 09:57:04
如printf("aaa", RGB(ff,00,ff));
当然,我知道printf是做不了这事的,想知道有没有实现这方法的函数?
...全文
365 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Beover1984 2005-11-28
  • 打赏
  • 举报
回复
那个只是TC中用,现在的C++已经没有那方面的函数了
xiaohuliwi 2005-11-28
  • 打赏
  • 举报
回复
c中graphic.h中有函数,设置字体的颜色,将字画在屏幕上
fz97532 2005-11-28
  • 打赏
  • 举报
回复
http://www.fm201.com/framework-1.htm
看看这个
上有
csucdl 2005-11-28
  • 打赏
  • 举报
回复
system("color 1b");
snowbirdfly 2005-11-28
  • 打赏
  • 举报
回复
这个是MSDN上面的例子:
#include <windows.h>

void NewLine(void);
void ScrollScreenBuffer(HANDLE, INT);

HANDLE hStdout, hStdin;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

void main(void)
{
LPSTR lpszPrompt1 = "Type a line and press Enter, or q to quit: ";
LPSTR lpszPrompt2 = "Type any key, or q to quit: ";
CHAR chBuffer[256];
DWORD cRead, cWritten, fdwMode, fdwOldMode;
WORD wOldColorAttrs;

// Get handles to STDIN and STDOUT.

hStdin = GetStdHandle(STD_INPUT_HANDLE);
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "GetStdHandle", "Console Error", MB_OK);
return;
}

// Save the current text colors.

if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
{
MessageBox(NULL, "GetConsoleScreenBufferInfo", "Console Error", MB_OK);
return;
}

wOldColorAttrs = csbiInfo.wAttributes;

// Set the text attributes to draw red text on black background.

if (! SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY))
{
MessageBox(NULL, "SetConsoleTextAttribute", "Console Error", MB_OK);
return;
}

// Write to STDOUT and read from STDIN by using the default
// modes. Input is echoed automatically, and ReadFile
// does not return until a carriage return is typed.
//
// The default input modes are line, processed, and echo.
// The default output modes are processed and wrap at EOL.

while (1)
{
if (! WriteFile(
hStdout, // output handle
lpszPrompt1, // prompt string
lstrlen(lpszPrompt1), // string length
&cWritten, // bytes written
NULL) ) // not overlapped
{
MessageBox(NULL, "WriteFile", "Console Error", MB_OK);
return;
}

if (! ReadFile(
hStdin, // input handle
chBuffer, // buffer to read into
255, // size of buffer
&cRead, // actual bytes read
NULL) ) // not overlapped
break;
if (chBuffer[0] == 'q') break;
}

// Turn off the line input mode, and echo the input mode.

if (! GetConsoleMode(hStdin, &fdwOldMode))
{
MessageBox(NULL, "GetConsoleMode", "Console Error", MB_OK);
return;
}

fdwMode = fdwOldMode &
~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
if (! SetConsoleMode(hStdin, fdwMode))
{
MessageBox(NULL, "SetConsoleMode", "Console Error", MB_OK);
return;
}

// Without line and echo input modes, ReadFile returns
// when any input is available. Carriage returns must
// be handled, and WriteFile is used to echo input.

NewLine();

while (1)
{
if (! WriteFile(
hStdout, // output handle
lpszPrompt2, // prompt string
lstrlen(lpszPrompt2), // string length
&cWritten, // bytes written
NULL) ) // not overlapped
{
MessageBox(NULL, "WriteFile", "Console Error", MB_OK);
return;
}

if (! ReadFile(hStdin, chBuffer, 1, &cRead, NULL))
break;
if (chBuffer[0] == '\r')
NewLine();
else if (! WriteFile(hStdout, chBuffer, cRead,
&cWritten, NULL)) break;
else
NewLine();
if (chBuffer[0] == 'q') break;
}

// Restore the original console mode.

SetConsoleMode(hStdin, fdwOldMode);

// Restore the original text colors.

SetConsoleTextAttribute(hStdout, wOldColorAttrs);
}

// The NewLine function handles carriage returns when the processed
// input mode is disabled. It gets the current cursor position
// and resets it to the first cell of the next row.

void NewLine(void)
{
if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
{
MessageBox(NULL, "GetConsoleScreenBufferInfo", "Console Error", MB_OK);
return;
}

csbiInfo.dwCursorPosition.X = 0;

// If it is the last line in the screen buffer, scroll
// the buffer up.

if ((csbiInfo.dwSize.Y-1) == csbiInfo.dwCursorPosition.Y)
{
ScrollScreenBuffer(hStdout, 1);
}

// Otherwise, advance the cursor to the next line.

else csbiInfo.dwCursorPosition.Y += 1;

if (! SetConsoleCursorPosition(hStdout,
csbiInfo.dwCursorPosition))
{
MessageBox(NULL, "SetConsoleCursorPosition", "Console Error", MB_OK);
return;
}
}

void ScrollScreenBuffer(HANDLE h, INT x)
{
SMALL_RECT srctScrollRect, srctClipRect;
CHAR_INFO chiFill;
COORD coordDest;

srctScrollRect.Left = 0;
srctScrollRect.Top = 1;
srctScrollRect.Right = csbiInfo.dwSize.X - x;
srctScrollRect.Bottom = csbiInfo.dwSize.Y - x;

// The destination for the scroll rectangle is one row up.

coordDest.X = 0;
coordDest.Y = 0;

// The clipping rectangle is the same as the scrolling rectangle.
// The destination row is left unchanged.

srctClipRect = srctScrollRect;

// Set the fill character and attributes.

chiFill.Attributes = FOREGROUND_RED|FOREGROUND_INTENSITY;
chiFill.Char.AsciiChar = ' ';

// Scroll up one line.

ScrollConsoleScreenBuffer(
h, // screen buffer handle
&srctScrollRect, // scrolling rectangle
&srctClipRect, // clipping rectangle
coordDest, // top left destination cell
&chiFill); // fill character and color
}
snowbirdfly 2005-11-28
  • 打赏
  • 举报
回复
#include <iostream>
#include <windows.h> // WinApi header

using namespace std; // std::cout, std::cin

int main()
{
HANDLE hConsole;
int k;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
// pick the colorattribute k you want
SetConsoleTextAttribute(hConsole, k);
cout << k << " I want to be nice today!" << endl;
}

cin.get(); // wait
return 0;
}
楼主编译一下看看效果如何~~~
kinker 2005-11-28
  • 打赏
  • 举报
回复
找一个图形库,比如TC中的graphics.h

c/C++标准里面是没有任何的图形库的,图形库都是别的厂商开发的。

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧