gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:17:08 24 4
gpt4 key购买 nike

我有一个用 C# 编写的程序,它使用光栅字体中不可用的字符。所以我想将字体更改为 Lucida Console。

为了以编程方式更改控制台字体,我将 SetCurrentConsoleFontEx() 与这段代码结合使用(来源:MSDN Console Class),但我在调用 SetCurrentConsoleFontEx() 时遇到了 System.AccessViolationException。

谁能帮帮我?

感谢您的帮助。

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 调用的方式存在两个问题。

首先,documentation对于 SetCurrentConsoleFontEx 说:

lpConsoleCurrentFontEx

A pointer to a CONSOLE_FONT_INFOEX structure that contains the font information.

所以第三个参数需要传引用:

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

你需要像这样调用方法:

SetCurrentConsoleFontEx(hnd, false, ref newInfo);

其次,CONSOLE_FONT_INFO_EX 结构中的FaceName 字段是Unicode 字符数组。我必须指定 CharSet 才能让它工作:

[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# - 在 C# 的控制台窗口中更改字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20631634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com