2017-12-21 14 views
1

Ich entwickle derzeit eine Konsolenanwendung in C#. In dieser Anwendung muss ich die Konsolenschriftart programmatisch ändern, da ich die deutschen Umlaute (ä, ö, ü) verwende und die Standardschrift sie nicht visualisieren kann. Ich war auf der Suche durch MSlib und Stackoverflow und fand die folgenden Seiten:
[1] https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396Konsole Schrift ändern funktioniert nicht

[2] Changing font in a Console window in C#

So kopierte ich die Lösungen in meinem C# -Programm und ich kann es kompilieren w/o Fehler, aber der Code ändert nicht die Schriftart zu Lucida. Ich hoffe, dass Sie mir helfen können.

Mein Code:

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... 
      //... 
      //... 
     } 
    } 

Habe ich einen Fehler oder benötige ich weitere Dinge zu tun?

Vielen Dank für die Hilfe im Voraus.

+1

Die Standard-Console-Schriftart kann definitiv umlaute Vokale usw. anzeigen, aber es gibt ein Problem mit der Standard-Codepage - Sie könnten versuchen, 'Console.OutputEncoding' zu ändern. –

+1

Nach dem Ändern der Zeile Console.OutputEncoding = System.Text.Encoding.Unicode; es kann die ä, ö, ü und μ anzeigen, aber das ³ wird immer noch nicht richtig angezeigt. Aber ich kann damit umgehen. Danke! Aber ich würde immer noch gerne wissen, warum der Code nicht funktioniert. – Kajkrow

+0

'³' ist nicht in der Standardkonsole Schriftart vorhanden - so würde Lucida benötigen. Der Code, den du eingefügt hast, sieht ok aus, kann nichts offensichtlich falsch damit sehen –

Antwort

0

Wie Dylan sagte, alles, was ich hatte, war zu ändern "Console.OutputEncoding".

Ich kann immer noch nicht die Schriftart ändern, aber das ist kein Problem für mich.