首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在不同版本的c#之间显示unicode卡时出现c#故障

在不同版本的c#之间显示unicode卡时出现c#故障
EN

Stack Overflow用户
提问于 2018-12-17 02:51:06
回答 2查看 427关注 0票数 0

使用c# .net 4.7.1,我试图制作一个控制台应用程序Blackjack游戏,而且我在用不同版本的Windows向控制台输出显示卡片套装时遇到了困难。对于Windows 7,这是正确显示套装的Main方法:

代码语言:javascript
运行
复制
static void Main(string[] args)
{
    string[] Suits = new string[] { "♠", "♣", "♥", "♦" };
    Methods.Print(Suits[0] + " " + Suits[1] + " " + Suits[2] + " " + Suits[3]);
    Console.ReadLine();
    ....
}

西服以我想要的方式展示,就像这样:

但是,如果我在我的Windows 10机器上运行带有此Main方法的程序,它们将显示如下:

我发现,如果我在我的Windows10机器上的Main方法中包括了这一行,那么这些西装就会显示出我想要它们显示的样子:

代码语言:javascript
运行
复制
Console.OutputEncoding = System.Text.Encoding.UTF8;

但这使得西装不能在我的Windows 7电脑上正确显示。有谁能帮我解决我如何让这些卡套装正确显示,无论程序运行在什么Windows操作系统?提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-17 04:16:48

感谢@tymtam对我所拥有的问题的洞察力。我研究了改变控制台字体作为解决方案的可能性。我找到了一篇文章,它展示了如何以编程方式将控制台字体更改为Lucida Console,这是一种真正的字体。以下是该链接中的格式化代码:

代码语言:javascript
运行
复制
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 System.Runtime.InteropServices;
  2. 我必须选中位于Allow unsafe code屏幕中的Project>Properties>Build复选框,如下所示:

在做了这些改变后,程序在Windows 7和Windows 10上运行,它会按照我的要求显示卡片。就像我之前说过的,我无法访问Windows上有其他版本Windows的机器,所以我只能说这是在Windows 7和Windows 10上运行的。

票数 0
EN

Stack Overflow用户

发布于 2018-12-17 03:28:58

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

代码语言:javascript
运行
复制
static void Main(string[] args)
{
    Console.WriteLine("D, C, H, S");
    Console.ReadLine();
}

以下是其他两种选择:

  • 检查windows版本并使用Encoding.UTF8测试所有场景;
  • Gui应用程序。

Windows 7

Windows7控制台和

代码语言:javascript
运行
复制
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("♠, ♣, ♥, ♦");
Console.ReadLine();

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

来自Console.OutputEncoding性质

注意,成功地向控制台显示Unicode字符需要以下条件: 控制台必须使用TrueType字体(如Lucida或Consolas )来显示字符。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53808432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档