C#修改控制台字体,运行时字体大小? [英] C# modify console font, font size at runtime?

查看:341
本文介绍了C#修改控制台字体,运行时字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建


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

解决方案

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

这篇关于C#修改控制台字体,运行时字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆