possible to get/set console font size in c# .net?(可以在 c# .net 中获取/设置控制台字体大小吗?)
问题描述
我看到了有关更改控制台真实类型字体、控制台颜色 (rgb) 的帖子,但没有看到有关设置或获取控制台字体大小的帖子.原因=网格输出到控制台,网格有很多列,更适合较小的字体,想知道是否可以在运行时更改而不是允许默认或配置的字体优先/覆盖继承.
I have seen posts on changing console true type font, console colors (rgb) but nothing on setting or getting the console font size. edit: reason = grid is output to console, grid has many columns, fits better in smaller font, wondering if possible to change at runtime rather than allowing default or configured fonts to take priority / override inheritance.
推荐答案
也许 这篇文章可以帮到你
ConsoleHelper.cs
ConsoleHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace ConsoleExtender {
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ConsoleFont {
public uint Index;
public short SizeX, SizeY;
}
public static class ConsoleHelper {
[DllImport("kernel32")]
public static extern bool SetConsoleIcon(IntPtr hIcon);
public static bool SetConsoleIcon(Icon icon) {
return SetConsoleIcon(icon.Handle);
}
[DllImport("kernel32")]
private extern static bool SetConsoleFont(IntPtr hOutput, uint index);
private enum StdHandle {
OutputHandle = -11
}
[DllImport("kernel32")]
private static extern IntPtr GetStdHandle(StdHandle index);
public static bool SetConsoleFont(uint index) {
return SetConsoleFont(GetStdHandle(StdHandle.OutputHandle), index);
}
[DllImport("kernel32")]
private static extern bool GetConsoleFontInfo(IntPtr hOutput, [MarshalAs(UnmanagedType.Bool)]bool bMaximize,
uint count, [MarshalAs(UnmanagedType.LPArray), Out] ConsoleFont[] fonts);
[DllImport("kernel32")]
private static extern uint GetNumberOfConsoleFonts();
public static uint ConsoleFontsCount {
get {
return GetNumberOfConsoleFonts();
}
}
public static ConsoleFont[] ConsoleFonts {
get {
ConsoleFont[] fonts = new ConsoleFont[GetNumberOfConsoleFonts()];
if(fonts.Length > 0)
GetConsoleFontInfo(GetStdHandle(StdHandle.OutputHandle), false, (uint)fonts.Length, fonts);
return fonts;
}
}
}
}
这里是如何使用它为控制台列出真正的字体,
Here is how to use it to list true type fonts for console,
static void Main(string[] args) {
var fonts = ConsoleHelper.ConsoleFonts;
for(int f = 0; f < fonts.Length; f++)
Console.WriteLine("{0}: X={1}, Y={2}",
fonts[f].Index, fonts[f].SizeX, fonts[f].SizeY);
ConsoleHelper.SetConsoleFont(5);
ConsoleHelper.SetConsoleIcon(SystemIcons.Information);
}
关键函数:SetConsoleFont、GetConsoleFontInfo 和 GetNumberOfConsoleFonts.它们没有记录,因此使用风险自负.
Crucial functions: SetConsoleFont, GetConsoleFontInfo and GetNumberOfConsoleFonts. They're undocumented, so use at your own risk.
这篇关于可以在 c# .net 中获取/设置控制台字体大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:可以在 c# .net 中获取/设置控制台字体大小吗?
基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
