在 C/C++ 中为文本着色的最简单方法DOS?
我用turbo C++ 3.0在DOS环境下做了一个基本的贪吃蛇游戏,我自己也是个菜鸟。一段时间以来,我一直在寻找一种非常简单、也许是基本的方法来在 DOS 窗口中制作不同颜色的文本。我并不是在寻找复杂的文本着色方法。我正在编写的大多数程序都非常简单和基础,而对比程序本身大的文本进行着色的复杂代码只会令人困惑且无效。
我的问题是,在 DOS 控制台中使用 C 和 C++ 语言对文本进行着色的最简单方法是什么?
I made a basic snake game in a DOS enviroment using turbo C++ 3.0, and I'm quite a rookie myself. I've been looking for a while for a very simple and perhaps rudimentary way of making text of different colors in a DOS window. I'm not looking for complicated ways of coloring text. Most programs I'm writing are extremely simple and basic, and a complicated code to colour text that's larger than the program itself would just be confusing and ineffective.
My question is, what is the simplest way of coloring text in a DOS console in BOTH language, C and C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果启用 ansi.sys,则可以使用 ansi 转义序列。
我假设您在 Windows 上使用 DOS,因为您指的是“DOS 窗口”,因此您需要先启用 ansi.sys,然后才能使用它。
Turbo C++ 3.0 不附带 Windows 头文件或库,因此您将无法使用控制台 API。
If you enable ansi.sys, you can use ansi escape sequences.
I assume you're using DOS on Windows, as you refer to a "DOS window", so you need to enable ansi.sys before you can use it.
Turbo C++ 3.0 doesn't come with Windows headers or libraries, so you won't be able to use the Console API.
您可以使用 Turbo C/C++ 独有的(即非标准)函数
textcolor()
、textbackground()
和textattr()
与cprintf()
、cputs()
和putch()
一起使用。请参阅 IDE 帮助中的说明,它们都位于conio.h
中。You can use Turbo C/C++'s-only (that is, non-standard) functions
textcolor()
,textbackground()
andtextattr()
together withcprintf()
,cputs()
andputch()
. See their description in the IDE's help, they're all inconio.h
.您实际上使用 DOS(例如 DOSBox 或 Win 9x 上的 DOS)吗?
如果是这样,ANSI.SYS 使用起来非常简单。您只需在文本前面加上设置颜色的控制代码即可。
如果没有,(即您实际上在 Windows 上使用命令提示符),则使用 SetConsoleTextAttribute 设置前景色和背景色。
我刚刚想起来,32 位版本的 Windows 仍然支持
command.com
并且可以加载ANSI.SYS
。Are you actually using DOS (e.g. DOSBox or DOS on Win 9x)?
If so, ANSI.SYS is very straightforward to use. You just precede your text with control codes that set the colour.
If not, (i.e. you're actually using a command-prompt on Windows) then use SetConsoleTextAttribute to set the foreground and background colours.
And I just remembered, 32-bit versions of Windows still support
command.com
and this can loadANSI.SYS
.如果直接写入视频文本缓冲区,屏幕上的每个字符单元对应一对字节,一个是要显示的字符,另一个是其颜色。请参阅http://en.wikipedia.org/wiki/VGA_兼容_文本_模式
乍一看,它可能看起来是有点令人畏惧,但实际上非常简单。只是有点陌生而已。使用 ANSI 转义序列需要生成相当多的输出,而文本缓冲区每个字符一个 16 位字。
这不再重要了,但在老式硬件上,有必要写入文本缓冲区才能获得响应式更改。浏览 ANSI 界面显然花费了大量时间。
If you write directly to the video text buffer, each character cell onscreen corresponds to a pair of bytes, one is the character to display, the other is its colors. See http://en.wikipedia.org/wiki/VGA_compatible_text_mode
At first, it might seem a bit daunting, but it is actually quite straightforward. It is just a bit unfamiliar. Using the ANSI escape sequences requires generating quite a bit of output whereas the text buffer is one 16-bit word per character.
Not that it matters anymore, but on vintage era hardware it was necessary to write to the text buffer to get responsive changes. Going through the ANSI interface took a visibly significant amount of time.