Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取当前控制台字体信息_C#_.net_Image_Console Application_Viewer - Fatal编程技术网

C# 获取当前控制台字体信息

C# 获取当前控制台字体信息,c#,.net,image,console-application,viewer,C#,.net,Image,Console Application,Viewer,我正在用C#NET为控制台编写一个图像查看器。我的问题是控制台字体字符不是正方形。我将它们视为像素,这会在屏幕上绘制时拉伸图像 我想通过宽度,高度等属性,以某种方式读取当前使用字体的字体信息 我找到了,但它似乎只是列出了所有当前可用的字体 我使用了以下代码: [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct ConsoleFont { public uint Index; public sh

我正在用C#NET为控制台编写一个图像查看器。我的问题是控制台字体字符不是正方形。我将它们视为像素,这会在屏幕上绘制时拉伸图像

我想通过
宽度
高度
等属性,以某种方式读取当前使用字体的字体信息

我找到了,但它似乎只是列出了所有当前可用的字体

我使用了以下代码:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ConsoleFont
{
        public uint Index;
        public short SizeX, SizeY;
}

[DllImport("kernel32")]
private static extern bool GetConsoleFontInfo(IntPtr hOutput, [MarshalAs(UnmanagedType.Bool)]bool bMaximize, uint count, [MarshalAs(UnmanagedType.LPArray), Out] ConsoleFont[] fonts);
这不会返回当前控制台窗口中使用的特定字体

我仍然希望使用类似于
ConsoleFont
struct的东西来存储字体属性。但是,
GetConsoleFontInfo(…)
并不像上面所说的那样做


如果有人知道怎么做,请告诉我:)

正确的解决方案是实现以下几行:

        const int STD_OUTPUT_HANDLE = -11;
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr GetStdHandle(int nStdHandle);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public class CONSOLE_FONT_INFO_EX
        {
            private int cbSize;
            public CONSOLE_FONT_INFO_EX()
            {
                cbSize = Marshal.SizeOf(typeof(CONSOLE_FONT_INFO_EX));
            }
            public int FontIndex;
            public COORD dwFontSize;
            public int FontFamily;
            public int FontWeight;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string FaceName;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct COORD
        {
            public short X;
            public short Y;

            public COORD(short X, short Y)
            {
                this.X = X;
                this.Y = Y;
            }
        };

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        extern static bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool bMaximumWindow, [In, Out] CONSOLE_FONT_INFO_EX lpConsoleCurrentFont);
然后只需阅读当前控制台字体信息,如:

CONSOLE_FONT_INFO_EX currentFont = new CONSOLE_FONT_INFO_EX();
GetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), false, currentFont);

// currentFont does now contain all the information about font size, width and height etc... 

避免使用未记录的函数。