Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Determine current fontname in use

Aug
2,799
144
A PowerShell .ps1 script to determine the current fontname in use;
Code:
if (-not ('Win32test.ConsoleTest' -as [type])) {
    $defConsoleTest = @'
using System.Runtime.InteropServices;
using System;

namespace Win32test
{
    public static class ConsoleTest
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        extern static bool GetCurrentConsoleFontEx(
            IntPtr hConsoleOutput,
            bool bMaximumWindow,
            ref CONSOLE_FONT_INFOEX lpConsoleCurrentFont);

        private enum StdHandle
        {
            OutputHandle = -11
        }

        [DllImport("kernel32")]
        private static extern IntPtr GetStdHandle(StdHandle index);

        public static string GetFontInfo()
        {
            CONSOLE_FONT_INFOEX ConsoleFontInfo = new CONSOLE_FONT_INFOEX();
            ConsoleFontInfo.cbSize = (uint)Marshal.SizeOf(ConsoleFontInfo);

            GetCurrentConsoleFontEx(GetStdHandle(StdHandle.OutputHandle), false, ref ConsoleFontInfo);

            return string.Format("FaceName: {0}, FontFamily: {1}, FontWeight: {2}, FontSize: {3}x{4}",
                ConsoleFontInfo.FaceName,
                ConsoleFontInfo.FontFamily,
                ConsoleFontInfo.FontWeight,
                ConsoleFontInfo.dwFontSize.X,
                ConsoleFontInfo.dwFontSize.Y);
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct COORD
        {
            public short X;
            public short Y;
            public COORD(short x, short y) { X = x; Y = y; }
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        private struct CONSOLE_FONT_INFOEX
        {
            public uint cbSize;
            public uint nFont;
            public COORD dwFontSize;
            public int FontFamily;
            public int FontWeight;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string FaceName;
        }
    }
}
'@
    Add-Type -TypeDefinition $defConsoleTest
}

[Win32test.ConsoleTest]::GetFontInfo()

Output from TCC;
Code:
R:\>pshell fontname.ps1
FaceName: IBM 3270, FontFamily: 54, FontWeight: 700, FontSize: 16x34

Output from PowerShell 5.1 in Windows Terminal;
Code:
PS R:\> .\FontName.ps1
FaceName: Consolas, FontFamily: 4, FontWeight: 0, FontSize: 0x16

Output from TCC in Windows Terminal;
Code:
R:\>pshell FontName.ps1
FaceName: Consolas, FontFamily: 4, FontWeight: 0, FontSize: 0x16

Joe
 
In TCMD, this works. Read about TakeCommandIPC in the help's "Plugins". Supposedly, the "TCFONT" command takes 0, 1, or 2 (for name, height, weight) but I couldn't figure out how to do that with @WINAPI.

Code:
v:\> echo %@winapi[d:\tc36\takecmd.dll,?TakeCommandIPC@@YAHPEA_W0@Z,TCFONT,buffer]
Consolas
 
Back
Top