可以在c#.net中获取/设置控制台字体大小?

Chr*_*ris 13 .net c# console interop

我已经看到有关更改控制台真实类型字体,控制台颜色(rgb)的帖子,但没有设置或获取控制台字体大小.编辑:reason = grid输出到控制台,grid有很多列,更适合更小的字体,想知道是否可以在运行时更改,而不是允许默认或配置字体采用优先级/覆盖继承.

jam*_*ond 7

也许这篇文章可以帮到你

关键功能:SetConsoleFont,GetConsoleFontInfoGetNumberOfConsoleFonts.它们没有证件,因此请自担风险.

  • 也许,您可以总结一下为什么有用,而不是指向另一个页面? (27认同)
  • 虽然这在理论上可以回答这个问题,[但最好](http://meta.stackexchange.com/q/8259)在这里包含答案的基本部分,并提供参考链接. (9认同)
  • 我们总是欢迎指向潜在解决方案的链接,但请[在链接周围添加上下文](http://meta.stackoverflow.com/a/8259/169503),以便您的其他用户能够了解它是什么以及它为什么那里.如果目标站点无法访问或永久脱机,请始终引用重要链接的最相关部分.考虑到只是一个链接到外部网站_可能是[为什么以及如何删除某些答案?](http://stackoverflow.com/help/deleted-answers). (3认同)
  • 未公开的功能很有趣!认真地说,很好的答案。 (2认同)
  • 对我来说链接已损坏,而且对 Mac/Linux 也不友好:I (2认同)
  • 链接已损坏:网络存档:https://web.archive.org/web/20090808235410/http://blogs.microsoft.co.il/blogs/pavely/archive/2009/07/23/changing-console-fonts .aspx (2认同)

小智 7

这个线程中,我找到了一个更优雅的解决方案,现在可以正常工作了。

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样你就可以这样做:

ConsoleHelper.SetCurrentFont("Consolas", 10);
Run Code Online (Sandbox Code Playgroud)