Formatting the TitleBar text area

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
owinsloe
Posts: 92
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Tue Jul 10, 2012 10:37 am

Formatting the TitleBar text area

Post 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 1425 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