c#在不同版本的Windows之间显示unicode卡套装时出现问题
使用c#.net 4.7.1,我正在尝试制作一款游戏机应用程序Blackjack游戏,但在使用不同版本的Windows将卡片套装显示到游戏机输出时遇到了问题。对于Windows 7,这是正确显示套装的c#在不同版本的Windows之间显示unicode卡套装时出现问题,c#,unicode,console-application,C#,Unicode,Console Application,使用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] + " "
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;
但这就使得套装无法在我的Windows7电脑上正确显示。有谁能帮助我,无论程序运行在什么Windows操作系统上,如何正确显示这些卡片套装?提前感谢。如果您希望它在控制台中可靠工作,那么以下是我的解决方案:
static void Main(string[] args)
{
Console.WriteLine("D, C, H, S");
Console.ReadLine();
}
以下是两个其他选项:
- 检查windows版本并使用
♠代码>,
♣代码>,
♥代码>,
♦代码>和
李>编码.UTF8
- Gui应用程序
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("♠, ♣, ♥, ♦");
Console.ReadLine();
很可能是console应用程序中的字体
发件人:
请注意,成功地将Unicode字符显示到控制台需要以下条件:
控制台必须使用TrueType字体(如Lucida console或ConsoleAs)来显示字符
您可以在控制台
应用程序属性中更改字体:
感谢@tymtam对我所面临问题的见解。我研究了改变控制台字体作为解决方案的可能性。我找到了一篇文章,它展示了如何通过编程将控制台字体更改为
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();
....
}
有两件事需要注意
使用System.Runtime.InteropServices代码>
Project>Properties>Build
屏幕中的Allow unsafe code
复选框,如下所示:在进行这些更改后,该程序在Windows 7和Windows 10上运行,并按照我的要求显示卡片套装。正如我之前所说,我无法访问安装了其他版本Windows的机器,因此我只能说,这肯定是在Windows 7和Windows 10上运行的。无法抑制安装Windows的冲动。除了……我想不出别的什么。。如果是Windows 10,您可以将控制台配置为使用
System.Text.Encoding.UTF8
,否则保持原样..@BagusTesa,我考虑过这一点,但有一点可能仍然是个问题,那就是我不知道这个应用程序可能运行在其他版本的Windows上。我可以解释Windows7和Windows10,但是其他版本的Windows呢?另外,在查看该前景时,我看到有人发布,安装的Service Pack也会影响它的显示方式。我只能访问Windows 7计算机和Windows 10计算机,因此无法在其他版本的Windows上进行测试,更不用说不同的Service Pack了。谢谢你的建议。