简体   繁体   English

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

[英]How to change font size in console application using C

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? 有没有办法让这个声明更大?

Although teppic's answer to use system() will work, it is rather intensively heavy-handed to call an external program just to do that. 虽然teppic对使用system() 的回答是有效的,但是为了做到这一点,调用外部程序却相当严厉。 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. 至于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 teppic notes, there is no support for changing the font size. 但是,正如teppic所说,不支持更改字体大小。 That's under the user's control. 这是在用户的控制之下。

If it's Linux (and probably other forms of Unix) you could mess around with system to change a few terminal settings to make it stand out - though not the font size. 如果它是Linux(可能还有其他形式的Unix)你可以搞乱system来改变一些终端设置,使它脱颖而出 - 虽然不是字体大小。 This kind of thing would really only be suitable for simple programs, and it's obviously not portable: 这种东西真的只适合简单的程序,而且显然不便携:

#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 printf that will control most Unix terminal applications, eg \\033[31m will change the text to red in an xterm. 否则,您可以通过printf直接发送各种终端序列,它们将控制大多数Unix终端应用程序,例如\\033[31m将在xterm中将文本更改为红色。 But these sequences can vary. 但是这些序列可以变化。

如果您使用某些unix,则可以尝试激活和取消激活粗体文本:

printf("\033[1m%c\033[0m", map[x][y]);

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM