我目前正在用c#开发一个控制台应用程序。在这个应用程序中,我需要以编程方式更改Console Font,因为我使用的是德语Umlaute(ä,ö,ü),标准字体无法显示它们。
我正在浏览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#-program中复制了解决方案,我可以编译它没有错误,但代码不会将字体更改为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...
//...
//...
}
}
我犯了错误还是需要做进一步的工作?
感谢您的帮助。
答案 0 :(得分:0)
正如Dylan所说,我所需要的只是改变" Console.OutputEncoding" 。
我仍然无法更改字体,但对我来说没问题。
//Main Program
public class Program
{
public static void Main(string[] args)
{
//Encoding for Umlaute
Console.OutputEncoding = System.Text.Encoding.Unicode;
//Code...
//...
//...
}
}