C++中能否单独修改控制台内部分文本字号而非整个窗口?
Absolutely! But it’s important to note that console text formatting isn’t part of the standard C++ library—it depends entirely on the operating system and terminal you’re using. Here’s how to pull it off on the two main platforms:
Windows (Using Win32 API)
Windows provides direct access to console settings via its Win32 API. You can temporarily change the font size, print your targeted text, then revert back to the original settings. Here’s a working example:
#include <windows.h> #include <iostream> int main() { // Grab the console output handle HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_FONT_INFOEX fontInfo; fontInfo.cbSize = sizeof(CONSOLE_FONT_INFOEX); // Save the original font configuration to revert later GetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo); CONSOLE_FONT_INFOEX originalFont = fontInfo; // Increase the font height (Y-axis) for "Name:" fontInfo.dwFontSize.Y = 24; // Adjust this value for bigger/smaller text // Leave X as 0 to let Windows auto-calculate matching width SetCurrentConsoleFontEx(consoleHandle, FALSE, &fontInfo); std::cout << "Name:"; // Switch back to the original font size for "Jordan" SetCurrentConsoleFontEx(consoleHandle, FALSE, &originalFont); std::cout << " Jordan" << std::endl; return 0; }
Notes for Windows:
- The
dwFontSize.Yvalue represents the font height in pixels. SettingdwFontSize.Xto 0 lets the system automatically pick a proportional width. - Most Windows compilers (like MSVC) link against
user32.libautomatically, but if you run into issues, add-luser32to your compile command. - Elevated permissions are rarely needed for basic font size changes, but some restricted environments might require them.
Linux/Unix (Using ANSI Escape Codes)
Most modern Linux/Unix terminals (xterm, GNOME Terminal, Konsole, etc.) support ANSI escape sequences to adjust font sizes. The exact sequence varies slightly by terminal, but this works for most:
#include <iostream> int main() { // Set font size to 24pt for "Name:" std::cout << "\e]50;size=24\a"; std::cout << "Name:"; // Revert to default font size (adjust 12 to match your terminal's default) std::cout << "\e]50;size=12\a"; std::cout << " Jordan" << std::endl; return 0; }
Notes for Linux/Unix:
- The sequence
\e]50;size=<N>\atells the terminal to set the font size to<N>points. Some terminals use pixels instead, so you may need to experiment with values. - Minimal or older terminals (like basic
tty) might ignore this sequence—test with your target terminal first.
Cross-Platform Alternative
If you need a solution that works across Windows and Linux without writing platform-specific code, consider third-party libraries like ncurses (for Unix-like systems) or Qt’s console widgets. These tools abstract away platform differences and let you style text more flexibly.
Just remember: standard C++ cout/cin streams only handle raw character data—they don’t support built-in text styling. Any formatting requires platform-specific APIs or third-party tools.
内容的提问来源于stack exchange,提问作者Jim M

