在C#中的控制台窗口中更改字体

在C#中的控制台窗口中更改字体

本文介绍了在C#中的控制台窗口中更改字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,C#编写的,使用的光栅字体没有的字符。所以我想改变字体为Lucida Console

I have a program, written in C#, that uses characters not available in Raster fonts. So I want to change font to Lucida Console.

要编程的方法修改控制台的字体,我用SetCurrentConsoleFontEx()与此代码(来源:的),但我得到了电话SetCurrentConsoleFontEx的System.AccessViolationException()。

To change Console font programatically, I use SetCurrentConsoleFontEx() with this code (Source: MSDN Console Class) but I got an System.AccessViolationException on call SetCurrentConsoleFontEx().

谁能帮助我?

感谢的对你有所帮助。

using System;
using System.Linq;
using System.Runtime.InteropServices;


namespace ConsoleExtender
{
  public static class ConsoleHelper
  {
      [StructLayout(LayoutKind.Sequential)]
      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,
             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, newInfo);
            }
          }
      }
  }
}


推荐答案

有两个问题你定义的API调用的方式。

There's two problems with the way you've defined those API calls.

首先,的为 SetCurrentConsoleFontEx 说:

lpConsoleCurrentFontEx

A 指针 的包含该字体信息的 CONSOLE_FONT_INFOEX 结构。

所以,第三个参数需要通过引用传递:

So the third parameter needs to be passed by reference:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetCurrentConsoleFontEx(
    IntPtr consoleOutput,
    bool maximumWindow,
    ref CONSOLE_FONT_INFO_EX consoleCurrentFontEx);

和需要调用这样的方法:

and you need to call the method like this:

SetCurrentConsoleFontEx(hnd, false, ref newInfo);



其次,在面名字段 CONSOLE_FONT_INFO_EX 结构是的的Unicode 的字符数组。我必须指定字符集来得到它的工作:

Secondly, the FaceName field in the CONSOLE_FONT_INFO_EX structure is an array of Unicode characters. I had to specify the CharSet to get it to work:

[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];
}

这篇关于在C#中的控制台窗口中更改字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:10