如何在Windows中以粗体和斜体设置C / C ++控制台文本?
我正在Windows控制台模式下开发Visual C ++应用程序,我想将控制台文本设置为Bold和Italic形式。我使用SetConsoleTextAttribute函数和系统(COLOR XX)函数更改了控制台文本颜色和背景颜色。但是没有Win32 API函数可以将文本模式更改为BOLD,Italic或Stroke。请提及在Windows环境中的C / C ++编程边界内实现它的任何功能或机制?谢谢。
我尝试了什么:
我在挣扎使用SetConsoleTextAttribute WinAPI函数
Hi,
I am developing an Visual C++ application in windows console mode, I want to set my console text in to Bold and Italic form. I changed console text color and background color using SetConsoleTextAttribute function and system("COLOR XX") functions. But there is no Win32 API function to changed text mode in to BOLD, Italic or Stroke. Please mention any function or mechanism to achieve it within C/C++ programming boundary in Windows enviornment? Thank you.
What I have tried:
I was struggling with SetConsoleTextAttribute WinAPI function
对于粗体,您可以使用 SetConsoleTextAttribute设置 FOREGROUND_INTENSITY
code>。
但是不支持斜体和删除(除了更改影响所有字符的控制台字体)。
在旧的MS-DOS时代,这是使用ANSI转义序列实现的。这些现在回到了Windows 10.
如果您使用的是实际的Windows 10,请参阅控制台虚拟终端序列(Windows) [ ^ ]。
但不幸的是斜体( ESC [3m
)和通过( ESC [9m
)不支持。
如果你真的需要这些模式,你必须使用其他软件,如 GitHub - adoxa / ansicon:处理Windows控制台程序的ANSI转义序列。 [ ^ 检查它是否支持斜体并通过后(我没有检查过上面的内容)。
For bold you might setFOREGROUND_INTENSITY
withSetConsoleTextAttribute
.
But there is no support for italic and strike (besides changing the console font which affects all characters).
In the old MS-DOS days such was realised using ANSI escape sequences. These are now back with Windows 10.
If you use an actual Windows 10, see Console Virtual Terminal Sequences (Windows)[^].
But unfortunately italic (ESC[3m
) and strike through (ESC[9m
) are not supported.
If you really need these modes you must probably use additional software like GitHub - adoxa/ansicon: Process ANSI escape sequences for Windows console programs.[^] after checking if it supports italic and strike through (I have not checked it for the above).
你不能。控制台不支持粗体或斜体文本。
You can't. The console doesn't support "bold" or "italic" text.
对于粗体,您可以使用此功能
#include< iostream>
std :: ostream& bold_on(std :: ostream& os)
{
return os<< \ e [1m;
}
std :: ostream& bold_off(std :: ostream& os)
{
return os<< \ [[0m;
}
int main()
{
std :: cout<< bold_on<< bold<< bold_off<< 非粗体<<的std :: ENDL;
}
For bold you can use this function
#include <iostream>
std::ostream& bold_on(std::ostream& os)
{
return os << "\e[1m";
}
std::ostream& bold_off(std::ostream& os)
{
return os << "\e[0m";
}
int main()
{
std::cout << bold_on << "bold" << bold_off << " non-bold" << std::endl;
}