Hello Everyone,
I've encountered an issue with the appearance of a Windows Form created using PowerShell. When running the script within PowerShell ISE, the form displays correctly with clear fonts and the expected size. However, when running the same script in a regular PowerShell console, the form's appearance is different; the font appears unclear, and the form size seems to have increased.
Is there any reason for this inconsistency in appearance between PowerShell ISE and PowerShell? How can I ensure consistent appearance across both environments? Any insights or solutions would be greatly appreciated. Thank you!
Here's the sample code I'm using:
function Show-ConfirmationForm {
param (
[string]$LabelText,
[string]$Button1Text,
[string]$Button2Text
)
Add-Type -AssemblyName System.Windows.Forms
# Create the form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Wizard"
$form.Size = New-Object System.Drawing.Size(600, 350)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = "FixedDialog"
$form.MaximizeBox = $false
# Add an icon to the form
$form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon([System.Windows.Forms.Application]::ExecutablePath)
# Creating a panel for the header
$Header = New-Object System.Windows.Forms.Panel
$Header.BackColor = [System.Drawing.Color]::FromArgb(0, 69, 120)
$Header.Height = 60
$Header.Width = $form.Width
$Header.Dock = [System.Windows.Forms.DockStyle]::Top
$form.Controls.Add($Header)
# Adding label to header
$headerLabel = New-Object System.Windows.Forms.Label
$headerLabel.Text = $LabelText
$headerLabel.ForeColor = [System.Drawing.Color]::White
$headerLabel.AutoSize = $true
$headerLabel.Location = New-Object System.Drawing.Point(130, 10)
$headerLabel.Font = New-Object System.Drawing.Font("Calibri", 12)
$headerLabel.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
$Header.Controls.Add($headerLabel)
# Create the first button
$button1 = New-Object System.Windows.Forms.Button
$button1.Location = New-Object System.Drawing.Point(130, 200)
$button1.Size = New-Object System.Drawing.Size(150, 40)
$button1.Text = $Button1Text
$button1.BackColor = [System.Drawing.Color]::FromArgb(0, 69, 120)
$button1.ForeColor = [System.Drawing.Color]::White
$button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.Controls.Add($button1)
# Create the second button
$button2 = New-Object System.Windows.Forms.Button
$button2.Location = New-Object System.Drawing.Point(330, 200)
$button2.Size = New-Object System.Drawing.Size(150, 40)
$button2.Text = $Button2Text
$button2.BackColor = [System.Drawing.Color]::FromArgb(0, 69, 120)
$button2.ForeColor = [System.Drawing.Color]::White
$button2.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.Controls.Add($button2)
# Show the form as a dialog box and capture the result
$result = $form.ShowDialog()
# Return the result
return $result
}
Show-ConfirmationForm -LabelText "Do you want to clear disk?" -Button1Text "Clear Now" -Button2Text "Later"