【问题标题】:possible to get/set console font size in c# .net?可以在 c# .net 中获取/设置控制台字体大小吗?
【发布时间】:2011-09-27 03:02:21
【问题描述】:

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

【问题讨论】:

  • 真的不应该留给用户选项吗?当然,用户始终可以通过应用程序上下文菜单对其进行更改。
  • 为什么要明确要求一篇文章?不过,我真的找不到任何关于此事的信息。
  • 我没有明确要求文章,问题是;是否“可以在 c# .net 中更改控制台字体大小?”
  • @Chris,在您最初的问题中,您问了Anyone see an article for this?。在我的书中,这算是明确要求一篇文章。不过,现在改写就好了。
  • 您可能应该使用控制台的宽度来帮助您决定如何格式化您的表格,而不是试图弄乱字体大小或窗口大小。只是我的 2 美分。

标签: c# .net interop console


【解决方案1】:

也许this的文章可以帮到你

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

    }
}

这里是如何使用它为控制台列出真正的字体,

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

关键函数:SetConsoleFontGetConsoleFontInfoGetNumberOfConsoleFonts。它们没有记录,因此使用风险自负。

【讨论】:

  • 未记录的功能很有趣!不过说真的,很好的答案。
  • 或许,与其指向另一个页面,不如总结一下为什么它会有所帮助?
  • 虽然这在理论上可以回答这个问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
  • 始终欢迎提供潜在解决方案的链接,但请add context around the link,以便您的其他用户知道它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。考虑到仅仅是指向外部站点的链接Why and how are some answers deleted? 的一个可能原因。
  • 对我来说链接断开,也不适合 Mac/Linux :I
【解决方案2】:

this 线程中,我找到了一个更优雅的解决方案,现在可以完美运行。

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

【讨论】:

  • 这是一个绝妙的解决方案,按预期工作!
【解决方案3】:

控制台不支持在运行时更改字体大小。可以在on MSDN 找到修改当前控制台窗口设置的可用方法列表。我的理解是,这是因为:

  1. 控制台不是富文本界面,这意味着它不能显示多种字体或字体大小。
  2. 正如 Noldorin 所说,这应该由用户决定,例如有视力问题的人可能会选择大字体。

【讨论】:

  • 真正的程序员想方设法
  • 如果用户改变了字体大小,即。使它更小,因为他们有一个非常大的显示器,因为他们的眼睛是普通人眼睛的两倍,然后我的计算会受到影响,并且在设置控制台全屏时(即在 win7 上使用自定义缓冲区大小最大化)它如果用户篡改了默认配置,则不会相同。
  • @Chris,看起来Console 实际上包含一个设置最大窗口大小的选项。
  • 啊,我遇到了这个问题,但它似乎正在工作并且现在使用不同的字体大小看起来相同:Console.SetWindowSize(Console.LargestWindowWidth - 3, Console.LargestWindowHeight - 1); int hWnd = Process.GetCurrentProcess().MainWindowHandle.ToInt32(); Console.SetBufferSize(Console.LargestWindowWidth - 3, Console.LargestWindowHeight - 1);移动(); ShowWindow(hWnd, SW_MAXIMIZE);
【解决方案4】:

运行应用程序 (Ctrl + F5) 后,右键单击控制台的标题(应显示类似 C:Windows\system32\cmd.exe 的内容)并选择属性。选择“字体”选项卡,您会看到调整大小的选项。

【讨论】:

  • OP 正在寻求一种方法来以编程方式设置正面尺寸,因此您的答案无法解决需求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
  • 2016-03-28
  • 1970-01-01
  • 2012-01-22
  • 2016-06-20
  • 2013-10-28
相关资源
最近更新 更多