【问题标题】:Console Font changing doesn't work控制台字体更改不起作用
【发布时间】:2018-01-08 17:25:41
【问题描述】:

我目前正在用 c# 开发一个控制台应用程序。在此应用程序中,我需要以编程方式更改控制台字体,因为我使用的是德语变音符号 (ä,ö,ü),而标准字体无法将它们可视化。 我正在查看 MSlib 和 stackoverflow 并找到以下页面:
[1]https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

[2]Changing font in a Console window in C#

所以我在我的 c# 程序中复制了解决方案,我可以编译它而没有错误,但代码不会将字体更改为 Lucida。我希望你能帮助我。

我的代码:

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Main
{
//From: https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
//From: https://stackoverflow.com/questions/20631634/changing-font-in-a-console-window-in-c-sharp
//ConsoleFont changing
    public class ConsoleHelper
    {
      [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        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];
        }

      [StructLayout(LayoutKind.Sequential)]
      internal struct COORD
      {
          internal short X;
          internal short Y;

          internal COORD(short x, short y)
          {
              X = x;
              Y = y;
          }
      }
      [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,
        ref 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 void SetConsoleFont(string fontName = "Lucida Console") 
      {
          unsafe
          {
            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);

                // 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, ref newInfo);
            }
          }
      }
    }

//Main Program
    public class Program
    {
        public static void Main(string[] args)
        {
            //Encoding for Umlaute
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            //Test in Color
            Console.BackgroundColor = ConsoleColor.Red;
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Test");
            //Reset Console Color
            Console.ResetColor();
            //Title
            Console.Title = "Test";
            //Setting Font for UTF-8-Encoding
            ConsoleHelper.SetConsoleFont();
            //Main Code...
            //...
            //...
        }
    }

是我做错了还是需要做进一步的事情?

提前感谢您的帮助。

【问题讨论】:

  • 标准控制台字体肯定可以显示元音变音等,但默认代码页存在问题 - 您可以尝试更改Console.OutputEncoding
  • 改行后改为 Console.OutputEncoding = System.Text.Encoding.Unicode;它可以显示 ä,ö,ü 和 µ,但 ³ 仍然无法正确显示。但我能应付。谢谢你!但我仍然很想知道,为什么代码不起作用。
  • ³ 不存在于标准控制台字体中 - 因此需要 Lucida。您粘贴的代码看起来没问题,看不出有什么明显的问题
  • 但是为什么它不在程序开始时改变字体呢?我的电话错了吗? ConsoleHelper.SetConsoleFont();

标签: c# fonts console


【解决方案1】:

正如 Dylan 所说,我所要做的就是改变 “Console.OutputEncoding”

我仍然无法更改字体,但这对我来说没问题。

//Main Program
public class Program
{
    public static void Main(string[] args)
    {
        //Encoding for Umlaute
        Console.OutputEncoding = System.Text.Encoding.Unicode;
        //Code...
        //...
        //...
    }
}

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 2011-04-05
    • 2016-05-24
    • 2014-07-06
    • 2012-04-20
    • 2012-07-31
    • 1970-01-01
    • 2018-04-24
    • 2018-09-10
    相关资源
    最近更新 更多