Page 1 of 1

Formatting the TitleBar text area

Posted: Thu Jan 30, 2020 2:48 pm
by owinsloe
Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.170
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.18363.0

This follows on from a Wish List post that I had for a way to left & right align two text strings in the title bar. This solution does what I want and while not perfect is possibly something that may be useful for others.

You need to be able to capture the inuse area and then work out how many spaces to insert between the two strings. I have been able to get most of this but need to allow for a 10% margin for widths that I have not been able to get (borders,icon)

Running the below code returns this..
Untitled.jpg
Untitled.jpg (103.01 KiB) Viewed 3167 times
$Product = 'cnwiniadmin'
$script:version = '1.0.0.0'

#Determine width of titlebar caption buttons
$def = @"
public enum SystemMetric
{
SM_CXSIZE = 0x0030, // 0x0030
}

[DllImport("user32.dll")]
public static extern int GetSystemMetrics(SystemMetric smIndex);
"@

Add-Type -Namespace NativeMethods -Name User32Dll -MemberDefinition $def
$CaptionButtonSize = [NativeMethods.User32Dll]::GetSystemMetrics([NativeMethods.User32Dll+SystemMetric]::SM_CXSIZE)

$Font = [System.Drawing.SystemFonts]::CaptionFont
$TotalWidth = ($MainForm.clientsize).width

$Gfx = $MainForm.CreateGraphics()
$NoPadding = [System.Windows.Forms.TextFormatFlags]::NoPadding

$Space = ' '
$CompanyText = '© ' + (Get-Date -Format 'yyyy') + ' Jade Software Corporation'
$ProductText = $Product + ' v' + $script:version
$SpaceSize = ([System.Windows.Forms.TextRenderer]::MeasureText($Gfx, $Space, $font, $MainForm.clientsize, $NoPadding)).width

$InuseArea = ($CaptionButtonSize * 3)
$InuseArea += ([System.Windows.Forms.TextRenderer]::MeasureText($Gfx, $ProductText, $font, $MainForm.clientsize, $NoPadding)).width
$InuseArea += ([System.Windows.Forms.TextRenderer]::MeasureText($Gfx, $CompanyText, $font, $MainForm.clientsize, $NoPadding)).width
$InuseArea += $SpaceSize
$InuseAreaWithPadding = $InuseArea

#Determine space padding
$i=0
do
{
$i++
$InuseAreaWithPadding += $SpaceSize
}
# Allow for padding so stop once we reach 90%
until ($InuseAreaWithPadding -ge ($TotalWidth*0.9) )

$MainForm.text = $ProductText + ' ' * $i + $CompanyText