2017-12-21 18 views
1

私は現在、C#でコンソールアプリケーションを開発中です。このアプリケーションでは、ドイツ語のUmlaute(ä、ö、ü)を使用しているため、標準フォントでは表示できないため、プログラムでConsole Fontを変更する必要があります。 [2] Changing font in a Console window in C#コンソールフォントの変更が機能しません

は、だから私は私のC#の-programにソリューションをコピーして、私はW/O、それをコンパイルすることができます[1] https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396


:私はMSlibとstackoverflowのを見て、次のページを発見された エラーが発生しますが、コードはフォントを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... 
      //... 
      //... 
     } 
    } 

は私がミスをしたのか、私はさらに物事が行われる必要がありますか?

ご協力ありがとうございます。

+1

標準のコンソールフォントではウムラウトの母音などが表示されることがありますが、デフォルトのコードページに問題があります。「Console.OutputEncoding」を変更してみてください。 –

+1

行をConsole.OutputEncoding = System.Text.Encoding.Unicodeに変更した後。 ä、ö、ü、μを表示できますが、³はまだ正しく表示されません。しかし、私はそれを処理することができます。ありがとうございました!しかし、まだ私は知りたいです、なぜコードは動作しません。 – Kajkrow

+0

'³'は標準コンソールフォントには存在しないので、Lucidaが必要です。あなたがペーストしたコードはOKだと思われますが、明らかにそれが間違っているものは何も見えません –

答えて

0

ディランが言ったように、私がdaに持っていたものはすべて"Console.OutputEncoding"に変更されました。

まだフォントを変更することはできませんが、それは問題ではありません。

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