[How-To] Get or set the console font size and font name (PowerShell hybrid)
Posted: 31 Jul 2021 17:22
Carlos, penpen, me, and probably others have been working for a long time to do this.
Latest attempts that I recall can be found in this thread: viewtopic.php?f=3&t=5617
I'd like to offer a pure script solution that should work for supported ttf fonts in console windows on Vista onwards. For raster fonts use font name Terminal. The code implements the %setfont% macro and shows how to use it. All font updates are volatile and only effective in the current console window. Default settings are untouched.
Magic numbers used:
$hOut=[WApi]::CreateFile('CONOUT$',-1073741824,2,[IntPtr]::Zero,3,0,[IntPtr]::Zero);
-1073741824 is for GENERIC_READ | GENERIC_WRITE
2 is for FILE_SHARE_WRITE
3 is for OPEN_EXISTING
$fInf.objSize=84;
84 is the size of the FontInfo structure in bytes
Steffen
Latest attempts that I recall can be found in this thread: viewtopic.php?f=3&t=5617
I'd like to offer a pure script solution that should work for supported ttf fonts in console windows on Vista onwards. For raster fonts use font name Terminal. The code implements the %setfont% macro and shows how to use it. All font updates are volatile and only effective in the current console window. Default settings are untouched.
Code: Select all
@echo off &setlocal
:: initialize the %setfont% macro
call :init_setfont
echo - current font:
%setfont%
pause
echo - cache the current font in a temporary file
:: the parentheses are critical for the redirection
:: capturing in a FOR /F loop doesn't work and thus, the temporary file is necessary in this case
>"%temp%\setfont.tmp~" (%setfont%)
pause
echo - 14 (same font name)
%setfont% 14
pause
echo - 20 Lucida Console
%setfont% 20 Lucida Console
pause
echo - (same font size) Consolas
%setfont% 0 Consolas
pause
echo - 16 Consolas
%setfont% 16 Consolas
pause
echo - reset
for /f "usebackq delims=" %%i in ("%temp%\setfont.tmp~") do %setfont% %%i
del "%temp%\setfont.tmp~"
pause
exit /b
:init_setfont
:: - BRIEF -
:: Get or set the console font size and font name.
:: - SYNTAX -
:: %setfont% [fontSize [fontName]]
:: fontSize Size of the font. (Can be 0 to preserve the size.)
:: fontName Name of the font. (Can be omitted to preserve the name.)
:: - EXAMPLES -
:: Output the current console font size and font name:
:: %setfont%
:: Set the console font size to 14 and the font name to Lucida Console:
:: %setfont% 14 Lucida Console
setlocal DisableDelayedExpansion
set setfont=for /l %%# in (1 1 2) do if %%#==2 (^
%=% for /f "tokens=1,2*" %%- in ("? ^^!arg^^!") do endlocal^&powershell.exe -nop -ep Bypass -c ^"Add-Type '^
%===% using System;^
%===% using System.Runtime.InteropServices;^
%===% [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)] public struct FontInfo{^
%=====% public int objSize;^
%=====% public int nFont;^
%=====% public short fontSizeX;^
%=====% public short fontSizeY;^
%=====% public int fontFamily;^
%=====% public int fontWeight;^
%=====% [MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)] public string faceName;}^
%===% public class WApi{^
%=====% [DllImport(\"kernel32.dll\")] public static extern IntPtr CreateFile(string name,int acc,int share,IntPtr sec,int how,int flags,IntPtr tmplt);^
%=====% [DllImport(\"kernel32.dll\")] public static extern void GetCurrentConsoleFontEx(IntPtr hOut,int maxWnd,ref FontInfo info);^
%=====% [DllImport(\"kernel32.dll\")] public static extern void SetCurrentConsoleFontEx(IntPtr hOut,int maxWnd,ref FontInfo info);^
%=====% [DllImport(\"kernel32.dll\")] public static extern void CloseHandle(IntPtr handle);}';^
%=% $hOut=[WApi]::CreateFile('CONOUT$',-1073741824,2,[IntPtr]::Zero,3,0,[IntPtr]::Zero);^
%=% $fInf=New-Object FontInfo;^
%=% $fInf.objSize=84;^
%=% [WApi]::GetCurrentConsoleFontEx($hOut,0,[ref]$fInf);^
%=% If('%%~.'){^
%===% $fInf.nFont=0; $fInf.fontSizeX=0; $fInf.fontFamily=0; $fInf.fontWeight=0;^
%===% If([Int16]'%%~.' -gt 0){$fInf.fontSizeY=[Int16]'%%~.'}^
%===% If('%%~/'){$fInf.faceName='%%~/'}^
%===% [WApi]::SetCurrentConsoleFontEx($hOut,0,[ref]$fInf);}^
%=% Else{(''+$fInf.fontSizeY+' '+$fInf.faceName)}^
%=% [WApi]::CloseHandle($hOut);^") else setlocal EnableDelayedExpansion^&set arg=
endlocal &set "setfont=%setfont%"
if !!# neq # set "setfont=%setfont:^^!=!%"
exit /b
$hOut=[WApi]::CreateFile('CONOUT$',-1073741824,2,[IntPtr]::Zero,3,0,[IntPtr]::Zero);
-1073741824 is for GENERIC_READ | GENERIC_WRITE
2 is for FILE_SHARE_WRITE
3 is for OPEN_EXISTING
$fInf.objSize=84;
84 is the size of the FontInfo structure in bytes
Steffen