conio.h没有包含textcolor()函数?

4

我一直在研究如何在我用C语言编写的DOS程序中使用颜色。有人告诉我conio.htextcolor()函数,但是当我在我的代码中使用它时,编译器/链接器会抛出错误,说我对该函数的引用未定义。

conio.h实际上是否有这个函数,还是我被误导了?


5
那是一个 Borland 函数,不属于 C 标准,并且是在加下划线前缀成为法律规定之前编写的。你必须闯入博物馆或使用 SetConsoleTextAttribute()。 - Hans Passant
1
它们都不支持。前往博物馆或挖掘Ralph的中断列表。 - Hans Passant
2
是的,我的错误,我已经17年没有编写任何16位代码了。我也没有玩过LP唱片那么久了,忘记了OJ辛普森做了什么,还有那个小傻瓜泥变成了石头。请原谅一个老人变得越来越虚弱。 - Hans Passant
好的,我还是会尝试使用那个函数。 - phillid
从来没有听说过Ralf的init列表(我想我比你们这些人新一点),所以我检查了一下,下载并提取了档案。哦,我的天啊:))我感觉这个家伙说:“嘿,互联网还不是一个永无止境的知识库、参考资料、示例、如何操作和堆栈溢出(眨眼),所以我就自己创建一个……在一个地方……好的……听起来可行,让我们做吧。”而我们这些孩子今天却抱怨手头有那么多丰富的资源。 - bolov
显示剩余6条评论
3个回答

2
不,conio.h库没有定义textcolor函数。下面是一种可能定义该函数的方式(包括windows.h库):
void textcolor (int color)
{
    static int __BACKGROUND;

    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;


    GetConsoleScreenBufferInfo(h, &csbiInfo);

    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
                             color + (__BACKGROUND << 4));
}

0

这是一个Turbo C/C++编译器函数。它在conio.h头文件中,但仅当您使用Turbo C/C++编译器时才可用。

请参阅Turbo C/C++ Compiler 2.0 docs的第384页,其中写道:

Function Selects new character color in text mode.

Syntax

#include <conio.h>
void textcolor(int newcolor); 

Prototype in conio.h

Remarks: textcolor selects the foreground character color. The foreground color of all characters subsequently written by the console output functions will be the color given by newcolor. You can give the color using a symbolic constant defined in conio.h. If you use these constants, you must include conio.h.

This function does not affect any characters currently on the screen, but only those displayed using direct console output (such as cprintf) after textcolor has been called.

[...]

另请参阅:在Turbo C++中为文本着色


-1

请查看textcolor库库,它可能正好满足您的需求。

以下是一个示例,展示如何使用它:

#include<stdio.h>
#include<conio.h>
main()
{
   textcolor(RED);
   cprintf("C programming");

   getch();
   return 0;
}

“检查一下”,你写的代码有问题,因为textcolor没有包含在conio.h中。 - serge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接