-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsfont.psm1
More file actions
115 lines (86 loc) · 3.26 KB
/
psfont.psm1
File metadata and controls
115 lines (86 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#requires -Version 2.0
$STD_OUTPUT_HANDLE = -11
$source = @"
public delegate bool SetConsoleFont(
IntPtr hWnd,
uint DWORD
);
public delegate uint GetNumberOfConsoleFonts();
public delegate bool GetConsoleFontInfo(
IntPtr hWnd,
bool BOOL,
uint DWORD,
[Out] CONSOLE_FONT_INFO[] ConsoleFontInfo
);
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_FONT_INFO
{
public uint nFont;
public COORD dwFontSize;
}
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
}
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandleA(
string module
);
[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
public static extern IntPtr GetProcAddress(
IntPtr hModule,
string procName
);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(
int nStdHandle
);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetCurrentConsoleFont(
IntPtr hConsoleOutput,
bool bMaximumWindow,
out CONSOLE_FONT_INFO lpConsoleCurrentFont
);
"@
Add-Type -MemberDefinition $source -Name Console -Namespace Win32API
$_hmod = [Win32API.Console]::GetModuleHandleA("kernel32")
"SetConsoleFont", "GetNumberOfConsoleFonts", "GetConsoleFontInfo" |
% {
$param = @()
$proc = [Win32API.Console]::GetProcAddress($_hmod, $_)
$delegate = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($proc, "Win32API.Console+$_")
$delegate.Invoke.OverloadDefinitions[0] -match "^[^(]+\((.*)\)" > $null
$argtypes = $Matches[1] -split ", " |
? { $_ } |
% {
'[{0}] ${1}' -f ($_ -split " ");
$param += "$" + ($_ -split " ")[-1]
}
$argtypes = $argtypes -join ", "
$param = $param -join ", "
iex @"
function $_($argtypes){
`$$_ = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($proc, 'Win32API.Console+$_')
`$$_.Invoke( $param )
}
"@
}
$_hConsoleScreen = [Win32API.Console]::GetStdHandle($STD_OUTPUT_HANDLE)
function Get-ConsoleFontInfo()
{
$_FontsNum = GetNumberOfConsoleFonts
$_ConsoleFonts = New-Object Win32API.Console+CONSOLE_FONT_INFO[] $_FontsNum
GetConsoleFontInfo $_hConsoleScreen $false $_FontsNum $_ConsoleFonts > $null
$_ConsoleFonts | select @{l="nFont";e={$_ConsoleFonts.Count-$_.nFont-1}}, @{l="dwFontSizeX";e={$_.dwFontSize.X}}, @{l="dwFontSizeY";e={$_.dwFontSize.Y}} | sort nFont
}
$_DefaultFont = New-Object Win32API.Console+CONSOLE_FONT_INFO
[Win32API.Console]::GetCurrentConsoleFont($_hConsoleScreen, $true, [ref]$_DefaultFont)
function Set-ConsoleFont ([Uint32]$DWORD=$_DefaultFont.nFont, [IntPtr]$hWnd=$_hConsoleScreen)
{
$flag = SetConsoleFont $hWnd $DWORD
if ( !$flag ) { throw "Illegal font index number. Check correct number using 'Get-ConsoleFontInfo'." }
}
Export-ModuleMember -Variable _DefaultFont, _hConsoleScreen `
-Function Set-ConsoleFont, Get-ConsoleFontInfo