我看到过有关更改控制台真实类型字体和控制台颜色(RGB)的帖子,但没有关于设置或获取控制台字体大小的内容。
我想要更改字体大小的原因是因为网格被打印到控制台上,而且该网格有许多列,所以使用较小的字体更适合。我想知道是否可以在运行时更改字体大小,而不是允许默认或配置的字体优先/覆盖继承。
我看到过有关更改控制台真实类型字体和控制台颜色(RGB)的帖子,但没有关于设置或获取控制台字体大小的内容。
我想要更改字体大小的原因是因为网格被打印到控制台上,而且该网格有许多列,所以使用较小的字体更适合。我想知道是否可以在运行时更改字体大小,而不是允许默认或配置的字体优先/覆盖继承。
也许这篇文章可以帮助你。
ConsoleHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace ConsoleExtender {
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ConsoleFont {
public uint Index;
public short SizeX, SizeY;
}
public static class ConsoleHelper {
[DllImport("kernel32")]
public static extern bool SetConsoleIcon(IntPtr hIcon);
public static bool SetConsoleIcon(Icon icon) {
return SetConsoleIcon(icon.Handle);
}
[DllImport("kernel32")]
private extern static bool SetConsoleFont(IntPtr hOutput, uint index);
private enum StdHandle {
OutputHandle = -11
}
[DllImport("kernel32")]
private static extern IntPtr GetStdHandle(StdHandle index);
public static bool SetConsoleFont(uint index) {
return SetConsoleFont(GetStdHandle(StdHandle.OutputHandle), index);
}
[DllImport("kernel32")]
private static extern bool GetConsoleFontInfo(IntPtr hOutput, [MarshalAs(UnmanagedType.Bool)]bool bMaximize,
uint count, [MarshalAs(UnmanagedType.LPArray), Out] ConsoleFont[] fonts);
[DllImport("kernel32")]
private static extern uint GetNumberOfConsoleFonts();
public static uint ConsoleFontsCount {
get {
return GetNumberOfConsoleFonts();
}
}
public static ConsoleFont[] ConsoleFonts {
get {
ConsoleFont[] fonts = new ConsoleFont[GetNumberOfConsoleFonts()];
if(fonts.Length > 0)
GetConsoleFontInfo(GetStdHandle(StdHandle.OutputHandle), false, (uint)fonts.Length, fonts);
return fonts;
}
}
}
}
以下是如何使用它来列出控制台的TrueType字体:
static void Main(string[] args) {
var fonts = ConsoleHelper.ConsoleFonts;
for(int f = 0; f < fonts.Length; f++)
Console.WriteLine("{0}: X={1}, Y={2}",
fonts[f].Index, fonts[f].SizeX, fonts[f].SizeY);
ConsoleHelper.SetConsoleFont(5);
ConsoleHelper.SetConsoleIcon(SystemIcons.Information);
}
关键函数:SetConsoleFont
,GetConsoleFontInfo
和GetNumberOfConsoleFonts
。它们没有文档支持,因此使用时需谨慎。
在这个线程中,我发现了一个更加优雅的解决方案,现在已经完美地运行。
ConsoleHelper.cs:
using System;
using System.Runtime.InteropServices;
public static class ConsoleHelper
{
private const int FixedWidthTrueType = 54;
private const int StandardOutputHandle = -11;
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr GetStdHandle(int nStdHandle);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);
private static readonly IntPtr ConsoleOutputHandle = GetStdHandle(StandardOutputHandle);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct FontInfo
{
internal int cbSize;
internal int FontIndex;
internal short FontWidth;
public short FontSize;
public int FontFamily;
public int FontWeight;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
//[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.wc, SizeConst = 32)]
public string FontName;
}
public static FontInfo[] SetCurrentFont(string font, short fontSize = 0)
{
Console.WriteLine("Set Current Font: " + font);
FontInfo before = new FontInfo
{
cbSize = Marshal.SizeOf<FontInfo>()
};
if (GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref before))
{
FontInfo set = new FontInfo
{
cbSize = Marshal.SizeOf<FontInfo>(),
FontIndex = 0,
FontFamily = FixedWidthTrueType,
FontName = font,
FontWeight = 400,
FontSize = fontSize > 0 ? fontSize : before.FontSize
};
// Get some settings from current font.
if (!SetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref set))
{
var ex = Marshal.GetLastWin32Error();
Console.WriteLine("Set error " + ex);
throw new System.ComponentModel.Win32Exception(ex);
}
FontInfo after = new FontInfo
{
cbSize = Marshal.SizeOf<FontInfo>()
};
GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref after);
return new[] { before, set, after };
}
else
{
var er = Marshal.GetLastWin32Error();
Console.WriteLine("Get error " + er);
throw new System.ComponentModel.Win32Exception(er);
}
}
}
这样您就可以只需执行以下操作:
ConsoleHelper.SetCurrentFont("Consolas", 10);
C:Windows\system32\cmd.exe
的内容),然后选择属性。选择“字体”选项卡,您将看到调整字体大小的选项。控制台不支持在运行时更改字体大小。可以在MSDN上找到修改当前控制台窗口设置的可用方法列表。 我的理解是这样的:
Console
实际上包含了设置最大窗口大小的选项。 - Teo Klestrup Röijezon