Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Determine Terminal Font Size

$
0
0

Hi Scripting Guys and Gals!

I've got a Powershell script that I drop into my Powershell Profile so I can dynamically scale the Powershell window size to match that of my display. It's one thing about Powershell (and previously cmd.exe) that drives me a bit nuts, not being able to easily/automatically adjust the window size to match my display size.

The thing is I can't find a way to determine the size of the default Powershell window font. I need to know the width to do this accurately every time. As it stands I hardcode it as being 8 pixels wide as this seems to be the most common screen font wdith (by default). Although 7 pixels is also pretty common. It would be much better if I could ascertain this programmatically! Help!

# Get resolution of primary screen and set Powershell window size correspondingly

# Load the System Windows Forms assembly
Add-Type -AssemblyName System.Windows.Forms
$DisplayDimensions = [System.Windows.Forms.Screen]::PrimaryScreen
$RawDisplayWidth = $DisplayDimensions.WorkingArea.Width
$DisplayHeight = $DisplayDimensions.WorkingArea.Height

# Get width of the Windows window frame the Powershell window will run in
$WindowFrameWidth = [System.Windows.Forms.SystemInformation]::FrameBorderSize.Width
# Get width of the Windows horizontal scrollbar for the Powershell Window
$HorizontalScrollBarWidth = [System.Windows.Forms.SystemInformation]::HorizontalScrollBarThumbWidth

# Now set the display width based on the above metrics. Make sure you always round DOWN ([decimal]::floor method)
$DisplayWidth = [decimal]::floor(($RawDisplayWidth - ($WindowFrameWidth + $HorizontalScrollBarWidth))/8)

# I couldn't find a way to ascertain the screen font size (which determines console character width)
# So I am making the unreliable (but generally correct) assumption that it is the default 8x12.
# I could spend more time but cost/benefit ratio startio to get out of kilter. If you find it, add it please!

$CustomPowershell = (Get-Host).UI.RawUI
$CustomPowershell.WindowTitle = "My Resized Window"

$NewWindowWidth = $DisplayWidth
$NewWindowHeight = ($DisplayHeight/12)-5

$BufferSize = $CustomPowershell.BufferSize
$BufferSize.Width = $NewWindowWidth
$BufferSize.Height = 1000
$CustomPowershell.BufferSize = $BufferSize

$WindowSize = $CustomPowershell.WindowSize
$WindowSize.Width = $NewWindowWidth
$WindowSize.Height = $NewWindowHeight
$CustomPowershell.WindowSize = $WindowSize
$CustomPowershell.WindowPosition.X = 0
$CustomPowershell.WindowPosition.Y = 0

Thanks and regards,

Phil


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>