【问题标题】:Size Windows Forms Powershell大小 Windows 窗体 Powershell
【发布时间】:2020-10-06 22:57:38
【问题描述】:

我是 PowerShell 新手,现在我开始接触表单了。

我有一个无法以任何方式解决的问题。

我制作了一个带有按钮、文本框和标签的表单。 我为每个项目设置了固定大小。

表单在大多数 PC 上都能正常打开

但在某些情况下,它会像这样打开:

而且我根本不明白如何解决它。我花了一整天的时间在这上面。 我已经用过

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

但这无济于事。

请告诉我如何让每个人的表单看起来都正常。

谢谢!

UPD:我确保通过将比例更改为 125% 来实现此效果 但我一直没有找到解决办法。

附: 抱歉英语不好:-)

[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()

【问题讨论】:

  • 有些机器使用缩放字体、不同的 Dpi 等。您的表单设置为缩放为字体。 AutoScaleMode = Font(默认设置)。我确定您可以将 app.manifest 添加到 PowerShell 应用程序,但我不记得那是什么了。如果你这样做,应用这个:How to configure an app to run correctly on a machine with a high DPI setting
  • 也许你用$objForm.Font = New-Object System.Drawing.Font($objForm.Font.FontFamily,8.25,[System.Drawing.FontStyle]::Regular)强制字体大小?
  • @Theo 感谢您的建议,但不幸的是它没有帮助。我检查了有问题的电脑,字体设置为 8.25。在我看来,这里真正的问题在于缩放和差异 DPI。如果你仔细看,你会发现甚至关闭表单的按钮都比我的大。我找不到解决这个问题的方法

标签: winforms powershell


【解决方案1】:

我最终找到了解决方案。它不像我预期的那么优雅,但它确实有效。

我必须从注册表中获取 AppliedDPI,计算设置的比例比标准比例大多少倍,然后根据需要简单地增加所有表单元素。

如果用户在工作时更改比例,它不起作用。

但这不太可能,我的目标是为 125%、150% 等大规模用户提供正常视图。

 #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)
}
##################################################

【讨论】:

    猜你喜欢
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 2013-04-30
    • 2011-10-04
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多