C#在运行时修改控制台字体、字体大小?
我正在创建一个控制台,为了确保我的游戏显示正确,我想在运行时更改控制台字体和字体大小 我对编程和c#非常在行,所以我希望这可以用我或其他人可以轻松实现的方式来解释 此列表列出了CONSOLE\u FONT\u INFOEX结构的完整语法:C#在运行时修改控制台字体、字体大小?,c#,fonts,runtime,console-application,font-size,C#,Fonts,Runtime,Console Application,Font Size,我正在创建一个控制台,为了确保我的游戏显示正确,我想在运行时更改控制台字体和字体大小 我对编程和c#非常在行,所以我希望这可以用我或其他人可以轻松实现的方式来解释 此列表列出了CONSOLE\u FONT\u INFOEX结构的完整语法: typedef struct _CONSOLE_FONT_INFOEX { ULONG cbSize; DWORD nFont; COORD dwFontSize; UINT FontFamily; UINT FontWeight;
typedef struct _CONSOLE_FONT_INFOEX {
ULONG cbSize;
DWORD nFont;
COORD dwFontSize;
UINT FontFamily;
UINT FontWeight;
WCHAR FaceName[LF_FACESIZE];
} CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
具体来说,我想在运行时将控制台字体更改为NSimSum,并将字体大小更改为32
编辑1:我可以解释一下如何使用发帖吗。我不明白函数需要在什么上下文中。我尝试了Console.SetCurrentConsoleFontEx
,但vs没有给我任何选择
编辑2:论坛帖子似乎详细地描述了一个改变字体大小的简单方法,但是它是针对C++ + < /p>的吗?
void setFontSize(int FontSize)
{
CONSOLE_FONT_INFOEX info = {0};
info.cbSize = sizeof(info);
info.dwFontSize.Y = FontSize; // leave X as zero
info.FontWeight = FW_NORMAL;
wcscpy(info.FaceName, L"Lucida Console");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), NULL, &info);
}
你试过这个吗
using System;
using System.Runtime.InteropServices;
public class Example
{
[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 unsafe void Main()
{
string fontName = "Lucida Console";
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);
bool tt = false;
// First determine whether there's already a TrueType font.
if (GetCurrentConsoleFontEx(hnd, false, ref info)) {
tt = (info.FontFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE;
if (tt) {
Console.WriteLine("The console already is using a TrueType font.");
return;
}
// 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);
}
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct COORD
{
internal short X;
internal short Y;
internal COORD(short x, short y)
{
X = x;
Y = y;
}
}
[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];
}
}
有关更多详细信息,请参阅您可能会研究的另一种选择是创建winforms应用程序并使用
RichTextBox
。我想这取决于你需要依赖多少内置的控制台功能
请注意,使用了一些自定义函数可以更轻松地编写彩色文本
您可以尝试以下方法:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
RichTextBox console = new RichTextBox();
private void Form1_Load(object sender, EventArgs e)
{
console.Size = this.ClientSize;
console.Top = 0;
console.Left = 0;
console.BackColor = Color.Black;
console.ForeColor = Color.White;
console.WordWrap = false;
console.Font = new Font("Consolas", 12);
this.Controls.Add(console);
this.Resize += Form1_Resize;
DrawDiagram();
}
private void DrawDiagram()
{
WriteLine("The djinni speaks. \"I am in your debt. I will grant one wish!\"--More--\n");
Dot(7);
Diamond(2);
WriteLine("....╔═══════╗..╔═╗");
Dot(8);
Diamond(2);
WriteLine("...║..|....║..║.╠══════╦══════╗");
Dot(9);
Diamond(2);
Write("..║.|.....║..║.║ ║.");
Write('&', Color.DarkRed);
Dot(4);
WriteLine("║");
}
private void Dot(int qty)
{
Write('.', qty);
}
private void WriteLine(string text)
{
Write($"{text}\n");
}
private void Diamond(int qty)
{
Write('♦', qty, Color.Blue);
}
private void Write(char character, Color color)
{
Write(character, 1, color);
}
private void Write(char character, int qty)
{
Write(character, qty, Color.White);
}
private void Write(char character, int qty, Color color)
{
Write(new string(character, qty), color);
}
private void Write(string text)
{
Write(text, Color.White);
}
private void Write(string text, Color color)
{
var originalColor = console.SelectionColor;
console.SelectionColor = color;
console.AppendText(text);
console.SelectionColor = originalColor;
}
private void Form1_Resize(object sender, EventArgs e)
{
console.Size = this.ClientSize;
}
}
输出
中可能存在的重复没有解释如何实现代码,这正是我在努力解决字体类型问题的地方。字体大小在帖子中没有回答。我认为我的帖子应该被删除,因为它不清楚,并且缺少我发现的新信息。相关答案是msdn的复制粘贴,解释为“您是否尝试过此功能”,另一个答案仅适用于WinFrom。我正计划写一篇新的帖子,感谢海报帮我写这篇模糊的帖子。没有更简单的方法吗?有几行代码我只能用来更改字体和字号吗?在上面的代码中,是这一行:
console.font=新字体(“Consolas”,20)代码>。但是在普通的控制台里我什么都不知道。我不知道winfrom是什么,直到我用谷歌搜索它,我决定在控制台中实现这一点,我假设代码在控制台中不起作用?如果我无法解决如何从控制台设置字体/大小,我将切换到winforms,因此感谢您的回答谢谢Rufus L这是一个非常有创意的回答。在所有代码中,有几行代码我只能用来更改字体和字体大小吗?我很抱歉,但我不明白它的大部分内容是如何要求的。您需要从主方法中筛选代码。rest是完全必需的,请尝试运行它并调试它,它将帮助您不运行,但了解它是如何工作的。我不知道如何在我的代码中实现这一点,我想我在试图更改字体大小时有些不知所措。有点令人震惊的是,没有控制台。SetCurrentConsoleFontSize(32),但我想如果它是那么容易,我就不会被卡住。这给了我一个在线路上使用受保护内存的例外@SetCurrentConsoleFontEx@.You必须在构建中启用“不安全”代码(这很重要,应该在回答中提及)。在Visual Studio中,这是在“项目属性生成”选项卡中完成的(在“解决方案资源管理器”>“属性”中右键单击该项目)。您可以在Main方法中设置它(像往常一样),其余的都是互操作的东西,您只需要将它们复制到您的项目中。