Guardian
Guardian

Reputation: 105

How to change font size in console application using C

How can I change the font size of a printed font using 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?

Upvotes: 8

Views: 42350

Answers (4)

Michael Haephrati
Michael Haephrati

Reputation: 4215

This code will work on Win32 applications (regardless of the subsystem used: WINDOWS or 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);

Upvotes: 0

Celada
Celada

Reputation: 22251

Although teppic's answer to 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.

In C, you should use terminfo capabilities directly:

#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. That's under the user's control.

Upvotes: 6

teppic
teppic

Reputation: 8195

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. 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, e.g. \033[31m will change the text to red in an xterm. But these sequences can vary.

Upvotes: 1

David Ranieri
David Ranieri

Reputation: 41017

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

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

Upvotes: 1

Related Questions