0

I'm new to PowerShell and now I got to forms.

And I have a problem that I cannot solve in any way.

I made a form with buttons, textboxes and labels. I have set a fixed size for each item.

Form opens normally on most PCs

Normal Form

But on some, it opens like this:

Bad Form

And I don't understand how to fix it at all. I spent a whole day on this. I have already used

$objForm.AutoScaleMode = 'dpi'
$objForm.AutoScaleDimensions = '6, 13'

But that doesn't help.

Please, tell me how to make the form look normal for everyone.

Thanks!

UPD: I made sure that this effect is achieved by changing the scale to 125% But I never found a solution.

P.S. Sorry for bad English :-)

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

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Computer Connect"
$objForm.Size = New-Object System.Drawing.Size(280,350) 
$objForm.StartPosition = "CenterScreen"
$objForm.BackColor = "Lightblue"
$objForm.KeyPreview = $True
$objForm.Topmost = $True
$objForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$objForm.Add_Shown({$objForm.Activate()})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Down") 
    {
        $objListbox.SelectedIndex = $objListbox.SelectedIndex + 1
    }
    })

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Up") 
    { 
        $objListbox.SelectedIndex = $objListbox.SelectedIndex - 1
    }})

    #BUTTON DW ADM
$HistoryRange = New-Object System.Windows.Forms.Listbox
$RCButton = New-Object System.Windows.Forms.Button
$RCButton.Location = New-Object System.Drawing.Size(10,160)
$RCButton.Size = New-Object System.Drawing.Size(75,23)
$RCButton.Text = "DW ADM"
$objForm.Controls.Add($RCButton) 


#BUTTON DameWare
$DWButton = New-Object System.Windows.Forms.Button
$DWButton.Location = New-Object System.Drawing.Size(100,160)
$DWButton.Size = New-Object System.Drawing.Size(75,23)
$DWButton.Text = "DameWare"
$objForm.Controls.Add($DWButton)

##-------VDI BUTTON-
$VDIButton = New-Object System.Windows.Forms.Button
$VDIButton.Location = New-Object System.Drawing.Size(190,240)
$VDIButton.Size = New-Object System.Drawing.Size(75,23)
$VDIButton.Text = "VDI"
$objForm.Controls.Add($VDIButton)
#-----------------------------------------------------------------------
$VDIInput = New-Object System.Windows.Forms.TextBox
$VDIInput.Location = New-Object System.Drawing.Size(10,295) 
$VDIInput.Size = New-Object System.Drawing.Size(255) 
$objForm.Controls.Add($VDIInput) 
#-----------------------------------------------------------------------
#BUTTON LOCK.RU
$InfoButton = New-Object System.Windows.Forms.Button
$InfoButton.Location = New-Object System.Drawing.Size(190,160)
$InfoButton.Size = New-Object System.Drawing.Size(75,23)
$InfoButton.Text = "lock.ru"
$objForm.Controls.Add($InfoButton)
##-------HELP BUTTON-
$HELPButton = New-Object System.Windows.Forms.Button
$HELPButton.Location = New-Object System.Drawing.Size(115, 190)
$HELPButton.Size = New-Object System.Drawing.Size(75, 23)
$HELPButton.Text = "HELP"
$objForm.Controls.Add($HELPButton)
#LOGGED ON 
$Info = New-Object System.Windows.Forms.Button
$Info.Location = New-Object System.Drawing.Size(190,215)
$Info.Size = New-Object System.Drawing.Size(75,23)
$Info.Text = "Logged On machines"
$objForm.Controls.Add($Info)

#PING LABEL
$IPlabel1 = New-Object System.Windows.Forms.Label
$IPlabel1.Location = New-Object System.Drawing.Size(100,193)
$IPlabel1.Size = New-Object System.Drawing.Size(80,15)
$IPlabel1.Text = ""
$objForm.Controls.Add($IPlabel1) 

$IPLabel = New-Object System.Windows.Forms.Label
$IPLabel.Location = New-Object System.Drawing.Size(10,193)
$IPLabel.Size = New-Object System.Drawing.Size(80,15) 
$IPLabel.Text = "IP-адрес:"
$objForm.Controls.Add($IPLabel)

$MACLabel = New-Object System.Windows.Forms.Label
$MACLabel.Location = New-Object System.Drawing.Size(10,215)
$MACLabel.Size = New-Object System.Drawing.Size(80,15) 
$MACLabel.Text = "MAC-адрес:"
$objForm.Controls.Add($MACLabel) 

$MACaddress = New-Object System.Windows.Forms.label
$MACaddress.Location = New-Object System.Drawing.Size(100,215)
$MACaddress.Size = New-Object System.Drawing.Size(80,15)
$objForm.Controls.Add($MACaddress)  

$pingLabel = New-Object System.Windows.Forms.Label
$PingLabel.Location = New-Object System.Drawing.Size(10,233)
$PingLabel.Size = New-Object System.Drawing.Size(170,60) 
$PingLabel.Text = "Ping"
$PingLabel.AutoSize = $True
$objForm.Controls.Add($PingLabel) 

#Button to UNLOCK
$UnlockButton = New-Object System.Windows.Forms.Button
$UnlockButton.Location = New-Object System.Drawing.Size(190,190)
$UnlockButton.Size = New-Object System.Drawing.Size(75,23)
$UnlockButton.Text = "UNLOCK"
$objForm.Controls.Add($UnlockButton)

#LABEL ПОИСК
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,18) 
$objLabel.Size = New-Object System.Drawing.Size(40,20) 
$objLabel.Text = "Поиск:"
$objForm.Controls.Add($objLabel) 

#BUTTON Clear
$ClearButton = New-Object System.Windows.Forms.Button
$ClearButton.Location = New-Object System.Drawing.Size(172,12)
$ClearButton.Size = New-Object System.Drawing.Size(40,23)
$ClearButton.Text = "Clear"
$objForm.Controls.Add($ClearButton)

#BUTTON HISTORY
$HistoryButton = New-Object System.Windows.Forms.Button
$HistoryButton.Location = New-Object System.Drawing.Size(214,12)
$HistoryButton.Size = New-Object System.Drawing.Size(50,23)
$HistoryButton.Text = "History"
$objForm.Controls.Add($HistoryButton)

#TEXT BOX
$objInput = New-Object System.Windows.Forms.TextBox
$objInput.Location = New-Object System.Drawing.Size(50,15) 
$objInput.Size = New-Object System.Drawing.Size(115) 
$objForm.Controls.Add($objInput) 

#LIST-BOX
$objListbox = New-Object System.Windows.Forms.Listbox 
$objListbox.Location = New-Object System.Drawing.Size(10,43) 
$objListbox.Size = New-Object System.Drawing.Size(253,20)
$objListbox.Height = 120
$objForm.Controls.Add($objListbox)

[void] $objForm.ShowDialog()
  • 2
    Some machines use scaled Fonts, different Dpi etc. Your Form is set to scale to Font. `AutoScaleMode = Font` (it's the default). I'm sure you can add the `app.manifest` to a PowerShell app, but I don't remember what that is. If you do, apply this: [How to configure an app to run correctly on a machine with a high DPI setting](https://stackoverflow.com/a/13228495/7444103) – Jimi Oct 06 '20 at 23:54
  • Perhaps if you force the font size with `$objForm.Font = New-Object System.Drawing.Font($objForm.Font.FontFamily,8.25,[System.Drawing.FontStyle]::Regular)` ? – Theo Oct 07 '20 at 14:51
  • @Theo Thanks for the advice, but unfortunately it doesn't help. I checked on problem PCs, the font is set to 8.25. As it seems to me, the real problem here is in the scaling and difference DPI. If you look closely, you can see that even the button of the close form is larger than mine. And I could not find a solution to this problem – Aleksandr Filippov Oct 07 '20 at 16:04

1 Answers1

1

I ended up finding a solution. It's not as elegant as I expected, but it works.

I had to get AppliedDPI from the registry, calculate how many times the set scale is larger than the standard one, and simply increase all form elements as many times as needed.

it doesn't work if the user changes the scale while working.

But this is unlikely and my goal was to provide a normal view for users with large scales of 125%, 150%, etc.

 #scaling
##################################################
$DPISetting = (Get-ItemProperty 'HKCU:\Control Panel\Desktop\WindowMetrics' -Name AppliedDPI).AppliedDPI
$dpiKoef = $DPISetting / 96

if ($dpiKoef -gt 1){
$objForm.AutoSize = $true
$objFormWidth = ($objForm.ClientSize).Width * $dpiKoef
$objFormHeight = ($objForm.ClientSize).Height * $dpiKoef
$objForm.ClientSize = New-Object System.Drawing.Size($objFormWidth,$objFormHeight)

$RCButtonWidth = ($RCButton.ClientSize).Width * $dpiKoef
$RCButtonHeight = ($RCButton.ClientSize).Height * $dpiKoef
$RCButton.ClientSize = New-Object System.Drawing.Size($RCButtonWidth,$RCButtonHeight)

$DWButtonWidth = ($DWButton.ClientSize).Width * $dpiKoef
$DWButtonHeight = ($DWButton.ClientSize).Height * $dpiKoef
$DWButton.ClientSize = New-Object System.Drawing.Size($DWButtonWidth,$DWButtonHeight)

$VDIButtonWidth = ($VDIButton.ClientSize).Width * $dpiKoef
$VDIButtonHeight = ($VDIButton.ClientSize).Height * $dpiKoef
$VDIButton.ClientSize = New-Object System.Drawing.Size($VDIButtonWidth,$VDIButtonHeight)

$VDIInputWidth = ($VDIInput.ClientSize).Width * $dpiKoef
$VDIInputHeight = ($VDIInput.ClientSize).Height * $dpiKoef
$VDIInput.ClientSize = New-Object System.Drawing.Size($VDIInputWidth,$VDIInputHeight)

$InfoButtonWidth = ($InfoButton.ClientSize).Width * $dpiKoef
$InfoButtonHeight = ($InfoButton.ClientSize).Height * $dpiKoef
$InfoButton.ClientSize = New-Object System.Drawing.Size($InfoButtonWidth,$InfoButtonHeight)

$HELPButtonWidth = ($HELPButton.ClientSize).Width * $dpiKoef
$HELPButtonHeight = ($HELPButton.ClientSize).Height * $dpiKoef
$HELPButton.ClientSize = New-Object System.Drawing.Size($HELPButtonWidth,$HELPButtonHeight)

$InfoWidth = ($Info.ClientSize).Width * $dpiKoef
$InfoHeight = ($Info.ClientSize).Height * $dpiKoef
$Info.ClientSize = New-Object System.Drawing.Size($InfoWidth,$InfoHeight)

$IPlabel1Width = ($IPlabel1.ClientSize).Width * $dpiKoef
$IPlabel1Height = ($IPlabel1.ClientSize).Height * $dpiKoef
$IPlabel1.ClientSize = New-Object System.Drawing.Size($IPlabel1Width,$IPlabel1Height)

$IPLabelWidth = ($IPLabel.ClientSize).Width * $dpiKoef
$IPLabelHeight = ($IPLabel.ClientSize).Height * $dpiKoef
$IPLabel.ClientSize = New-Object System.Drawing.Size($IPLabelWidth,$IPLabelHeight)

$MACaddressWidth = ($MACaddress.ClientSize).Width * $dpiKoef
$MACaddressHeight = ($MACaddress.ClientSize).Height * $dpiKoef
$MACaddress.ClientSize = New-Object System.Drawing.Size($MACaddressWidth,$MACaddressHeight)

$MACLabelWidth = ($MACLabel.ClientSize).Width * $dpiKoef
$MACLabelHeight = ($MACLabel.ClientSize).Height * $dpiKoef
$MACLabel.ClientSize = New-Object System.Drawing.Size($MACLabelWidth,$MACLabelHeight)

$pingLabelWidth = ($pingLabel.ClientSize).Width * $dpiKoef
$pingLabelHeight = ($pingLabel.ClientSize).Height * $dpiKoef
$pingLabel.ClientSize = New-Object System.Drawing.Size($pingLabelWidth,$pingLabelHeight)

$UnlockButtonWidth = ($UnlockButton.ClientSize).Width * $dpiKoef
$UnlockButtonHeight = ($UnlockButton.ClientSize).Height * $dpiKoef
$UnlockButton.ClientSize = New-Object System.Drawing.Size($UnlockButtonWidth,$UnlockButtonHeight)

$objLabelWidth = ($objLabel.ClientSize).Width * $dpiKoef
$objLabelHeight = ($objLabel.ClientSize).Height * $dpiKoef
$objLabel.ClientSize = New-Object System.Drawing.Size($objLabelWidth,$objLabelHeight)

$ClearButtonWidth = ($ClearButton.ClientSize).Width * $dpiKoef
$ClearButtonHeight = ($ClearButton.ClientSize).Height * $dpiKoef
$ClearButton.ClientSize = New-Object System.Drawing.Size($ClearButtonWidth,$ClearButtonHeight)

$HistoryButtonWidth = ($HistoryButton.ClientSize).Width * $dpiKoef
$HistoryButtonHeight = ($HistoryButton.ClientSize).Height * $dpiKoef
$HistoryButton.ClientSize = New-Object System.Drawing.Size($HistoryButtonWidth,$HistoryButtonHeight)

$objInputWidth = ($objInput.Size).Width * $dpiKoef
$objInputHeight = ($objInput.Size).Height * $dpiKoef
$objInput.ClientSize = New-Object System.Drawing.Size($objInputWidth,$objInputHeight)

$objListboxWidth = ($objListbox.ClientSize).Width * $dpiKoef
$objListboxHeight = ($objListbox.ClientSize).Height * $dpiKoef
$objListbox.ClientSize = New-Object System.Drawing.Size($objListboxWidth,$objListboxHeight)

$RCButtonX = ($RCButton.Location).X * $dpiKoef
$RCButtonY = ($RCButton.Location).Y * $dpiKoef
$RCButton.Location = New-Object System.Drawing.Size($RCButtonX,$RCButtonY)

$DWButtonX = ($DWButton.Location).X * $dpiKoef
$DWButtonY = ($DWButton.Location).Y * $dpiKoef
$DWButton.Location = New-Object System.Drawing.Size($DWButtonX,$DWButtonY)

$VDIButtonX = ($VDIButton.Location).X * $dpiKoef
$VDIButtonY = ($VDIButton.Location).Y * $dpiKoef
$VDIButton.Location = New-Object System.Drawing.Size($VDIButtonX,$VDIButtonY)

$VDIInputX = ($VDIInput.Location).X * $dpiKoef
$VDIInputY = ($VDIInput.Location).Y * $dpiKoef
$VDIInput.Location = New-Object System.Drawing.Size($VDIInputX,$VDIInputY)

$InfoButtonX = ($InfoButton.Location).X * $dpiKoef
$InfoButtonY = ($InfoButton.Location).Y * $dpiKoef
$InfoButton.Location = New-Object System.Drawing.Size($InfoButtonX,$InfoButtonY)

$HELPButtonX = ($HELPButton.Location).X * $dpiKoef
$HELPButtonY = ($HELPButton.Location).Y * $dpiKoef
$HELPButton.Location = New-Object System.Drawing.Size($HELPButtonX,$HELPButtonY)

$InfoX = ($Info.Location).X * $dpiKoef
$InfoY = ($Info.Location).Y * $dpiKoef
$Info.Location = New-Object System.Drawing.Size($InfoX,$InfoY)

$IPlabel1X = ($IPlabel1.Location).X * $dpiKoef
$IPlabel1Y = ($IPlabel1.Location).Y * $dpiKoef
$IPlabel1.Location = New-Object System.Drawing.Size($IPlabel1X,$IPlabel1Y)

$IPLabelX = ($IPLabel.Location).X * $dpiKoef
$IPLabelY = ($IPLabel.Location).Y * $dpiKoef
$IPLabel.Location = New-Object System.Drawing.Size($IPLabelX,$IPLabelY)

$MACaddressX = ($MACaddress.Location).X * $dpiKoef
$MACaddressY = ($MACaddress.Location).Y * $dpiKoef
$MACaddress.Location = New-Object System.Drawing.Size($MACaddressX,$MACaddressY)

$MACLabelX = ($MACLabel.Location).X * $dpiKoef
$MACLabelY = ($MACLabel.Location).Y * $dpiKoef
$MACLabel.Location = New-Object System.Drawing.Size($MACLabelX,$MACLabelY)

$pingLabelX = ($pingLabel.Location).X * $dpiKoef
$pingLabelY = ($pingLabel.Location).Y * $dpiKoef
$pingLabel.Location = New-Object System.Drawing.Size($pingLabelX,$pingLabelY)

$UnlockButtonX = ($UnlockButton.Location).X * $dpiKoef
$UnlockButtonY = ($UnlockButton.Location).Y * $dpiKoef
$UnlockButton.Location = New-Object System.Drawing.Size($UnlockButtonX,$UnlockButtonY)

$objLabelX = ($objLabel.Location).X * $dpiKoef
$objLabelY = ($objLabel.Location).Y * $dpiKoef
$objLabel.Location = New-Object System.Drawing.Size($objLabelX,$objLabelY)

$ClearButtonX = ($ClearButton.Location).X * $dpiKoef
$ClearButtonY = ($ClearButton.Location).Y * $dpiKoef
$ClearButton.Location = New-Object System.Drawing.Size($ClearButtonX,$ClearButtonY)

$HistoryButtonX = ($HistoryButton.Location).X * $dpiKoef
$HistoryButtonY = ($HistoryButton.Location).Y * $dpiKoef
$HistoryButton.Location = New-Object System.Drawing.Size($HistoryButtonX,$HistoryButtonY)

$objInputX = ($objInput.Location).X * $dpiKoef
$objInputY = ($objInput.Location).Y * $dpiKoef
$objInput.Location = New-Object System.Drawing.Size($objInputX,$objInputY)

$objListboxX = ($objListbox.Location).X * $dpiKoef
$objListboxY = ($objListbox.Location).Y * $dpiKoef
$objListbox.Location = New-Object System.Drawing.Size($objListboxX,$objListboxY)
}
##################################################