Reputation: 69
I'm in the process of creating a rougelike and to assure my game displays correctly I am wanting to change the console font and font size at runtime.
I am very noob to programming and c# so I'm hoping this can be explained in a way I or anyone else can easily implement.
This resource list the full syntax of the CONSOLE_FONT_INFOEX structure:
typedef struct _CONSOLE_FONT_INFOEX {
ULONG cbSize;
DWORD nFont;
COORD dwFontSize;
UINT FontFamily;
UINT FontWeight;
WCHAR FaceName[LF_FACESIZE];
} CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
Specifically I'm wanting to change the console font to NSimSum and font size to 32 at runtime.
EDIT 1: Could I have it explained how to use the SetCurrentConsoleFontEx function from this post. I don't understand what context the function needs to be in. I tried Console.SetCurrentConsoleFontEx
but vs gave me no options.
EDIT 2: this forum post seems to detail a simple method of changing font size but is it specific to c++?
void setFontSize(int FontSize)
{
CONSOLE_FONT_INFOEX info = {0};
info.cbSize = sizeof(info);
info.dwFontSize.Y = FontSize; // leave X as zero
info.FontWeight = FW_NORMAL;
wcscpy(info.FaceName, L"Lucida Console");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), NULL, &info);
}
Upvotes: 4
Views: 23794
Reputation: 37020
Another alternative that you might look into is to create a winforms app and use a RichTextBox
instead. I guess it depends on how much you need to rely on any built-in console features.
Notice the use of some custom functions to make writing colored text easier.
You can try out something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
RichTextBox console = new RichTextBox();
private void Form1_Load(object sender, EventArgs e)
{
console.Size = this.ClientSize;
console.Top = 0;
console.Left = 0;
console.BackColor = Color.Black;
console.ForeColor = Color.White;
console.WordWrap = false;
console.Font = new Font("Consolas", 12);
this.Controls.Add(console);
this.Resize += Form1_Resize;
DrawDiagram();
}
private void DrawDiagram()
{
WriteLine("The djinni speaks. \"I am in your debt. I will grant one wish!\"--More--\n");
Dot(7);
Diamond(2);
WriteLine("....╔═══════╗..╔═╗");
Dot(8);
Diamond(2);
WriteLine("...║..|....║..║.╠══════╦══════╗");
Dot(9);
Diamond(2);
Write("..║.|.....║..║.║ ║.");
Write('&', Color.DarkRed);
Dot(4);
WriteLine("║");
}
private void Dot(int qty)
{
Write('.', qty);
}
private void WriteLine(string text)
{
Write($"{text}\n");
}
private void Diamond(int qty)
{
Write('♦', qty, Color.Blue);
}
private void Write(char character, Color color)
{
Write(character, 1, color);
}
private void Write(char character, int qty)
{
Write(character, qty, Color.White);
}
private void Write(char character, int qty, Color color)
{
Write(new string(character, qty), color);
}
private void Write(string text)
{
Write(text, Color.White);
}
private void Write(string text, Color color)
{
var originalColor = console.SelectionColor;
console.SelectionColor = color;
console.AppendText(text);
console.SelectionColor = originalColor;
}
private void Form1_Resize(object sender, EventArgs e)
{
console.Size = this.ClientSize;
}
}
Output
Upvotes: 3
Reputation: 2927
Have you tried this
using System;
using System.Runtime.InteropServices;
public class Example
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool GetCurrentConsoleFontEx(
IntPtr consoleOutput,
bool maximumWindow,
ref CONSOLE_FONT_INFO_EX lpConsoleCurrentFontEx);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetCurrentConsoleFontEx(
IntPtr consoleOutput,
bool maximumWindow,
CONSOLE_FONT_INFO_EX consoleCurrentFontEx);
private const int STD_OUTPUT_HANDLE = -11;
private const int TMPF_TRUETYPE = 4;
private const int LF_FACESIZE = 32;
private static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
public static unsafe void Main()
{
string fontName = "Lucida Console";
IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE);
if (hnd != INVALID_HANDLE_VALUE) {
CONSOLE_FONT_INFO_EX info = new CONSOLE_FONT_INFO_EX();
info.cbSize = (uint) Marshal.SizeOf(info);
bool tt = false;
// First determine whether there's already a TrueType font.
if (GetCurrentConsoleFontEx(hnd, false, ref info)) {
tt = (info.FontFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE;
if (tt) {
Console.WriteLine("The console already is using a TrueType font.");
return;
}
// Set console font to Lucida Console.
CONSOLE_FONT_INFO_EX newInfo = new CONSOLE_FONT_INFO_EX();
newInfo.cbSize = (uint) Marshal.SizeOf(newInfo);
newInfo.FontFamily = TMPF_TRUETYPE;
IntPtr ptr = new IntPtr(newInfo.FaceName);
Marshal.Copy(fontName.ToCharArray(), 0, ptr, fontName.Length);
// Get some settings from current font.
newInfo.dwFontSize = new COORD(info.dwFontSize.X, info.dwFontSize.Y);
newInfo.FontWeight = info.FontWeight;
SetCurrentConsoleFontEx(hnd, false, newInfo);
}
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct COORD
{
internal short X;
internal short Y;
internal COORD(short x, short y)
{
X = x;
Y = y;
}
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct CONSOLE_FONT_INFO_EX
{
internal uint cbSize;
internal uint nFont;
internal COORD dwFontSize;
internal int FontFamily;
internal int FontWeight;
internal fixed char FaceName[LF_FACESIZE];
}
}
Refer https://msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx for more details
Upvotes: 0