Discrepancy in Windows Form Appearance between PowerShell ISE and PowerShell

Swahela Mulla 95 Reputation points
2024-04-17T14:51:12.7466667+00:00

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"

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,592 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 54,811 Reputation points
    2024-04-17T15:23:51.24+00:00

    It looks fine to me but there could be other things going on. Firstly ISE is for Powershell <= 5 but what version of Powershell are you running it as when you have the issue? I tried your code and it displayed just fine in a regular PS 7 shell.

    On the other side of the fence I don't know your requirements but displaying a UX in a CLI based system seems like a bad idea. Confirmations are already supported in the CLI directly so having the user click a button vs either scripting it or confirming with the keyboard seems like a better approach. If for some reason you really needed a confirmation then Winforms is still overkill when you can just use MessageBox.Show which would also solve your problem. If you really, really need a UX for a CLI system AND that script will never be called by any other process that might expect it to be automatable then your code should work. It looks like you actually copied it from a series of old articles on getting winforms to work in PS.


  2. Prabhakaran Setti 0 Reputation points
    2024-07-21T08:43:44.75+00:00

    add the below code at the top of your code, it'll become fine.

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -TypeDefinition '
    public class DPIAware
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool SetProcessDPIAware();
    }
    '
    
    [System.Windows.Forms.Application]::EnableVisualStyles()
    [void] [DPIAware]::SetProcessDPIAware()
    #reset of your code................
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.