C语言 如何使用 C 在控制台应用程序中更改字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15316455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:39:04  来源:igfitidea点击:

How to change font size in console application using C

c

提问by Guardian

How can I change the font size of a printed font using c?

如何使用 c 更改打印字体的字体大小?

 printf ("%c", map[x][y]);

I want to print an array larger than all the other text in the program. Is there a way to just make that statement print larger?

我想打印一个比程序中所有其他文本都大的数组。有没有办法让该语句打印得更大?

采纳答案by Celada

Although teppic's answerto use system()will work, it is rather intensively heavy-handed to call an external program just to do that. As for David RF' answer, it is hard-coded for a specific type of terminal (probably a VT100-compatible terminal type) and won't support the user's actual terminal type.

尽管teppic 的使用答案system()会起作用,但调用外部程序只是为了做到这一点是相当繁重的。至于David RF 的回答,它是针对特定类型的终端(可能是兼容 VT100 的终端类型)进行硬编码的,并且不支持用户的实际终端类型。

In C, you should use terminfo capabilities directly:

在 C 中,您应该直接使用 terminfo 功能:

#include <term.h>

/* One-time initialization near the beginning of your program */
setupterm(NULL, STDOUT_FILENO, NULL);

/* Enter bold mode */
putp(enter_bold_mode);

printf("I am bold\n");

/* Turn it off! */
putp(exit_attribute_mode);

Still, as teppicnotes, there is no support for changing the font size. That's under the user's control.

尽管如此,正如teppic 所指出的,不支持更改字体大小。这在用户的控制之下。

回答by teppic

If it's Linux (and probably other forms of Unix) you could mess around with systemto change a few terminal settings to make it stand out - though not the font size. This kind of thing would really only be suitable for simple programs, and it's obviously not portable:

如果它是 Linux(可能还有其他形式的 Unix),您可能会system改变一些终端设置以使其脱颖而出 - 尽管不是字体大小。这种东西真的只适合简单的程序,而且显然不可移植:

#include <stdio.h>
#include <stdlib.h>

[...]

printf("Normal text\n");
system("setterm -bold on");
printf("Bold text\n");
system("setterm -bold off");

Otherwise there are various terminal sequences you can send directly via printfthat will control most Unix terminal applications, e.g. \033[31mwill change the text to red in an xterm. But these sequences can vary.

否则,您可以直接发送各种终端序列printf,它们将控制大多数 Unix 终端应用程序,例如\033[31m将 xterm 中的文本更改为红色。但是这些序列可能会有所不同。

回答by David Ranieri

If you are under some unix, you can try to activate and deactivate bold text:

如果您在某个 unix 下,您可以尝试激活和停用粗体文本:

printf("3[1m%c3[0m", map[x][y]);

回答by Michael Haephrati

This code will work on Win32 applications (regardless of the subsystem used: WINDOWS or CONSOLE):

此代码将适用于 Win32 应用程序(无论使用的子系统是什么:WINDOWS 或 CONSOLE):

inline void setFontSize(int a, int b) 

{

    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx = new CONSOLE_FONT_INFOEX();

    lpConsoleCurrentFontEx->cbSize = sizeof(CONSOLE_FONT_INFOEX);

    GetCurrentConsoleFontEx(hStdOut, 0, lpConsoleCurrentFontEx);

    lpConsoleCurrentFontEx->dwFontSize.X = a;

    lpConsoleCurrentFontEx->dwFontSize.Y = b;

    SetCurrentConsoleFontEx(hStdOut, 0, lpConsoleCurrentFontEx);

}

Then just call (for example):

然后只需调用(例如):

setFontSize(20,20);