Search code examples
winformspowershelldpi

DPI scaling of borderless Winforms window goes wrong


I have a PowerShell script with a borderless window and as more and more people use DPI scaling I tested my form accordingly.

Oddly it seems to scale (somewhat) well up to 150%, but with 175% the form itself (red) grows much more than the richtextbox (gray), as seen in the example below.

Any ideas on how to fix or prevent that?

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

$form = New-Object System.Windows.Forms.Form
$form.Size = "200,200"
$form.FormBorderStyle = "None"
$form.BackColor = "#C15959" 
$form.TopMost = $true
$form.StartPosition = "CenterScreen"

$form.AutoScalemode = "Dpi"
$form.AutoSize = $true
$form.AutoSizeMode = "GrowOnly"

$rtb = New-Object System.Windows.Forms.RichTextBox
$rtb.BorderStyle = "FixedSingle"
$rtb.BackColor = "#EDEDED"
$rtb.Anchor = "Top,Bottom,Right,Left"
$rtb.Size = "181,155"
$rtb.Location = "1,1"
$rtb.AutoSize = $true

$rtb.add_mouseclick({ $form.close()})
$form.Controls.Add($rtb)
$form.showdialog()

Form at 100% DPI scaling:

Form at 100% DPI scaling

Form at 175% DPI scaling:

enter image description here


Solution

  • The solution is not to set size but clientsize for the form itself.