GUI scaling issue

Script:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# set form size
$Form = New-Object System.Windows.Forms.Form
$Form.width = 500
$Form.height = 500
$Form.Text = ”Install Software”

# set font
$Font = New-Object System.Drawing.Font("Verdana",10)
$Form.Font = $Font

# checkbox 7-zip
$checkbox1 = new-object System.Windows.Forms.checkbox
$checkbox1.Location = new-object System.Drawing.Size(30,30)
$checkbox1.Size = new-object System.Drawing.Size(120,20)
$checkbox1.Text = " 7-Zip"
$checkbox1.Checked = $false
$Form.Controls.Add($checkbox1)

# checkbox notepad ++
$checkbox2 = new-object System.Windows.Forms.checkbox
$checkbox2.Location = new-object System.Drawing.Size(30,50)
$checkbox2.Size = new-object System.Drawing.Size(120,20)
$checkbox2.Text = " Notepad ++"
$checkbox2.Checked = $false
$Form.Controls.Add($checkbox2)

# ok button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(130,400)
$OKButton.Size = new-object System.Drawing.Size(100,40)
$OKButton.Text = "OK"
$OKButton.Add_Click({$Form.Close()})
$form.Controls.Add($OKButton)

# cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = new-object System.Drawing.Size(255,400)
$CancelButton.Size = new-object System.Drawing.Size(100,40)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$Form.Close()})
$form.Controls.Add($CancelButton)


$OKButton.Add_Click{

if($checkbox1.Checked) {Start-Process -FilePath C:\software\7z1900-x64.exe /S}
if($checkbox2.Checked) {Start-Process -FilePath C:\software\npp.7.8.1.Installer.x64.exe /S}

}

# activate form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

But I notice that on a PC with 200% scaling the GUI looks different than 100% scaling.

100% scaling:
100

200% scaling:
200

Is there an easy way to fix it?

I’m not reproducing:

that’s at 200%:

It may be dependent on the version of windows your running or something else. I tried on Windows 5 and 7.Also this really isn’t a PS question, more of WinForm question. Automatic form scaling - Windows Forms .NET | Microsoft Learn suggests there’s an automatic scaling feature. Not sure if that could be enabled or something else at play there.

  1. Have you tried googling it? Ideally you try to make an attempt at resolving the issue yourself and share what you tried.
  2. Have you confirmed there’s not a system configuration/setting that may help?

I think this question is out of scope for the forum though.

@dotnVo

I tried to google it, yes, I tried a few things like DPI awareness:

public class DPIAware
{
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern bool SetProcessDPIAware();
}
'

[System.Windows.Forms.Application]::EnableVisualStyles()
[void] [DPIAware]::SetProcessDPIAware()

did not help.

I tried autoscale:

$form.AutoScale = $true
$form.AutoScaleMode = "Font"

did not help.

tried some commands from here:

Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
public class ProcessDPI {
    [DllImport("user32.dll", SetLastError=true)]
    public static extern bool SetProcessDPIAware();      
}
'@
$null = [ProcessDPI]::SetProcessDPIAware()
[System.Windows.Forms.Application]::EnableVisualStyles()

$form.SuspendLayout()

# rest of the code here...

$form.ResumeLayout()

# final row is running the show dialog

did not help.

Windows 10 IoT Enterprise LTSC
21H2
19044.4291
Default PowerShell.

image

I still think this is more of a winform issue, not really a PS issue. Winform is pretty darn old iirc.

That’s not say there might not be some PS code that might resolve it (which might manipulate WinForms to make it scale) but I still personally feel it’s out of scope.
Unfortunately, I don’t have a Windows 10 IOT Enterprise box, so I have no way of testing any code to confirm functionality (nor am I able to reproduce the issue at hand on my current box).

In the future, provide everything you’ve tried upfront in your post. Without doing that, we don’t know what you tried, so we will be inclined to tell you to try ‘x’ which you have tried.

AutoScaleMode Enum (System.Windows.Forms) | Microsoft Learn

You might try setting autoscalmode to ‘DPI’ or one of the other options. May be worth just trying all of the settings.

High DPI support - Windows Forms .NET Framework | Microsoft Learn

Also… last suggestion - consider just dropping the GUI. The only time I’ve ever considered a GUI if the script was for ‘end users’, which is almost never the case.

Side note:

Also, you probably should check your code for errors.

$Form.Text = ”Install Software”

I doubt that’s correct. Think you are copying/pasting this from something (maybe AI). This should be

$Form.Text = "Install Software"

notice the quotes (and also the leading quote is backwards).

also you got leading spaces on your text properties.

I’ll leave it open to see if anyone else has input or is able to assist. unfortunately, without a way to test it, I’m not sure I have much more to add.

Thank you, I now tried all 4 options, none helped.

I actually need a GUI, this specific script is just a short down version of like 20 software, how else would you give users the option to choose what to install? a command input inside powershell? so like they would need to type 1 for 7zip and 2 for notepad++ (but now imagine you have 20 of these), there’s not really other choice but to use a GUI.

You can ignore those errors, I just copy-pasted this code just to demonstrate and illustrate my problem to fix on my own code.

Thank you.

Making form code adaptive to different display resolutions is a real chore. I know - Ive been through it!
However, my case is different in that I develop GUIs using Powershell Studio. I dont know if this link will help but by following their guidelines I was eventually able to get things to scale correctly.
high dpi

If you do not tell us what your intent is, we do not know. I think there was another post where we had this discussion. You never told us, until I mentioned to consider dropping the GUI, that this was for ‘end’ users. For all we know, you were doing it for yourself, or you were doing it for hand rolling sytems. Please refrain from making assumptions that we know how you are utilizing the script. This is precisely why I said ‘consider’ dropping the GUI. If this was for other ‘IT’ people or help desk, I’d actually encourage you to completely drop it. You can parameterize scripts and provide options via tab completion relatively easy. However, for end users, I do understand the ‘need’ for the GUI. Just a reminder , we know nothing of your environment :slight_smile: .

BTW: If it were me, I’d be managing software with a different product altogether for end users, such as Intune or SCCM. Optional software would be ‘made available’ via the Software Center. There are other options out there too. While you can do a lot with PowerShell, it’s not always the best solution for the job.

You cannot assume we know what you have and haven’t tried or what your situation is. It’s up to you to provide that information :slight_smile:

Onc e more ,we can’t know to ignore that. We’re just providing direction/FYI to help you avoid issues.

If you do want us to continue helping you in the future, please follow the advice provided.

I almost provided this resource myself, but the issue is most of the advice sorta rely on you using PS studio, and other advice is well, what I already provided unfortunately. They had some wrapper functions that I thought might help here (ConvertTo-ScaledPixel specifically), but I didn’t see them posted anywhere, though I didn’t google it to see if someone maybe made it public.

2 Likes

Uhm, this is interesting…
On PowerShell ISE - I have this issue.
On PowerShell - I don’t have this issue.
Then… I guess I can close this topic?
If someone can explain the logic behind this that would be awesome.

You said standard powershell but you’re actually using ISE :frowning: I have to again…remind you that you need to provide accurate information.

Powershell-based GUI behaves differently when run from PS command prompt and PS ISE - Microsoft Q&A

Differences between the ISE and PowerShell console - PowerShell Team (microsoft.com)

so you’re not alone, and per a 15 year old post by MS - they aren’t the same.

The official stance by many (and dare I say, this forum) is to stop using it and move to Visual studio Code with the PowerShell Extension. It’s much better. I don’t think it’s worth the effort to figure out ‘why’. It is what it is, and ISE will not be updated, so it’s best to accept it unfortunately.

Since your users willl likely just be running your script (double clicking it), this is how you should always test your script for functionality. Please do not use ISE.

2 Likes

Thank you!
I will use Visual Studio Code from now on.