【问题标题】:c# trouble displaying unicode card suits between different versions of Windowsc# 在不同版本的 Windows 之间显示 unicode 卡套装时出现问题
【发布时间】:2018-12-17 02:51:06
【问题描述】:

使用 c# .net 4.7.1,我正在尝试制作控制台应用程序 Blackjack 游戏,但在使用不同版本的 Windows 时无法将卡片套装显示到控制台输出。对于 Windows 7,这是正确显示西装的Main 方法:

static void Main(string[] args)
{
    string[] Suits = new string[] { "♠", "♣", "♥", "♦" };
    Methods.Print(Suits[0] + " " + Suits[1] + " " + Suits[2] + " " + Suits[3]);
    Console.ReadLine();
    ....
}

西装按我的意愿显示,如下所示:

但如果我在我的 Windows 10 机器上使用 Main 方法运行我的程序,它们会显示如下:

我发现,如果我在我的 Windows 10 机器上的 Main 方法中包含这一行,那么西装就会按照我的意愿显示:

Console.OutputEncoding = System.Text.Encoding.UTF8;

但这使得西装无法在我的 Windows 7 机器上正确显示。无论程序在什么 Windows 操作系统上运行,任何人都可以帮助我如何让这些卡片套装正确显示吗?提前致谢。

【问题讨论】:

  • 抑制不住发this的冲动。除了do some probing to check the windows version,我想不出别的了。如果是Windows 10,您可以将控制台配置为使用System.Text.Encoding.UTF8,否则保持原样..
  • @BagusTesa ,我想过这一点,但可能仍然存在问题的一件事是我不知道这个应用程序可能在哪些其他版本的 Windows 上运行。我可以考虑 Windows 7 和 Windows 10,但其他版本的 Windows 呢?此外,在查看该前景时,我看到有人发布说安装的 Service Pack 也会影响它的显示方式。我只能访问 Windows 7 机器和 Windows 10 机器,因此我无法在其他版本的 Windows 上对其进行测试,更不用说不同的 Service Pack。不过感谢您的建议。

标签: c# unicode console-application


【解决方案1】:

如果您希望它在控制台中可靠地工作,那么这是我的解决方案:

static void Main(string[] args)
{
    Console.WriteLine("D, C, H, S");
    Console.ReadLine();
}

这里有 2 个其他选项:

  • 检查Windows版本并使用♠、♣、♥、♦和Encoding.UTF8测试所有场景;
  • GUI 应用程序。

Windows 7

Windows7 控制台的问题和

Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("♠, ♣, ♥, ♦");
Console.ReadLine();

很可能是控制台应用程序中的字体。

来自Console.OutputEncoding Property:

请注意,成功向控制台显示 Unicode 字符需要满足以下条件:

控制台必须使用 TrueType 字体,例如 Lucida Console 或 Consolas,才能显示字符。

您可以在Console 应用的属性中更改字体:

【讨论】:

    【解决方案2】:

    感谢@tymtam 对我遇到的问题的见解。我研究了更改控制台字体作为解决方案的可能性。我发现this 文章展示了如何以编程方式将控制台字体更改为Lucida Console,这是一种真正的字体。这是该链接中我的格式化代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    
    namespace BlackJack
    {
        class BlackJack
        {       
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool SetCurrentConsoleFontEx(IntPtr consoleOutput, bool maximumWindow, ref CONSOLE_FONT_INFO_EX consoleCurrentFontEx);
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern IntPtr GetStdHandle(int dwType);
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern int SetConsoleFont(IntPtr hOut, uint dwFontNum);
    
            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);
    
            [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;
                }
            }
    
            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);
                    }
                }
            }
    
            static void Main(string[] args)
            {            
                Console.OutputEncoding = System.Text.Encoding.UTF8;
                SetConsoleFont();
                ....
            }
    

    注意两点

    1. 我必须添加这个 using 语句才能使其工作:

      using System.Runtime.InteropServices;

    2. 我必须选中位于Project>Properties>Build 屏幕中的Allow unsafe code 复选框,如下所示:

    进行这些更改后,该程序可以在 Windows 7 和 Windows 10 上运行,并按照我的意愿显示卡片套装。就像我之前说的,我无法访问装有其他 Windows 版本的机器,所以我只能说它肯定可以在 Windows 7 和 Windows 10 上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      相关资源
      最近更新 更多