使用PowerShell的高DPI屏幕的GUI样本
您知道PowerShell在Microsoft的官方信息中实现的GUI屏幕的示例代码吗? 我找不到它,自己创建了示例代码。 但是我认为这还不够。我想进行更智能的编码。
mysampe代码
例如, ,其中一个示例代码在这里 (基于自定义输入框的示例代码创建一个自定义输入框):
#this sample is Based on is microsoft sample thit is Creating a Custom Input Box
#URL:https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.2
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
#For High DPI, We Set AutoScaleDimensions and AutoScaleMode
#The reason SizeF is set to 96 is that the standard Windows resolution for the display is 96.
#Maybe you shuld try that All object resets Drawing.Point And Drawing.Size
#In some cases, review the Drawing.Point and Drawing.Size of all objects, depending on the resolution of your display.
$form.AutoScaleDimensions = New-Object System.Drawing.SizeF(96, 96)
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,220)
$okButton.Size = New-Object System.Drawing.Size(75,50)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,220)
$cancelButton.Size = New-Object System.Drawing.Size(200,50)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(800,40)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,100)
$textBox.Size = New-Object System.Drawing.Size(800,20)
#For High DPI, We Set Font Size
#In Japanese, "Yu Gothic UI" is the best font type. However, this font is for Japan only. Therefore, the default font definition is undefined.
#$Font = New-Object System.Drawing.Font("Yu Gothic UI",45,([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$Font = New-Object System.Drawing.Font("",45,([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$textBox.Font = $Font
$form.Controls.Add($textBox)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $textBox.Text
$x
}
我的自定义点
- 设置自动卡列型限度和autoscalemode
#For High DPI, We Set AutoScaleDimensions and AutoScaleMode
#The reason SizeF is set to 96 is that the standard Windows resolution for the display is 96.
#Maybe you shuld try that All object resets Drawing.Point And Drawing.Size
#In some cases, review the Drawing.Point and Drawing.Size of all objects, depending on the resolution of your display.
$form.AutoScaleDimensions = New-Object System.Drawing.SizeF(96, 96)
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
- 设置某些零件的字体大小(ex:extextbox)
#For High DPI, We Set Font Size for
([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$Font = New-Object System.Drawing.Font("",45,([System.Drawing.FontStyle]::Regular),
$textBox.Font = $Font
- 试用和错误(重置draws.point and drawid。点对于所有对象)
要提高
我不想重置绘图和绘图尺寸的点。 我想为所有显示类型使用相同的值。
Link
- Other code is in the Github:https://github.com/papanda925/PowerShell_HiDPI_GUI_Sample
- Tips my homepage 日语中: https://papandand925.com/?p=2124
在
Do you know the sample code of the GUI screen for high DPI implemented by PowerShell in the official information of Microsoft?
I couldn't find it and created the sample code myself.
But I don't think this is enough. I want to make smarter coding.
mysample codes
For example, one of the sample codes is here
(Based on the sample code of the custom input box Creating a Custom Input Box) :
#this sample is Based on is microsoft sample thit is Creating a Custom Input Box
#URL:https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.2
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
#For High DPI, We Set AutoScaleDimensions and AutoScaleMode
#The reason SizeF is set to 96 is that the standard Windows resolution for the display is 96.
#Maybe you shuld try that All object resets Drawing.Point And Drawing.Size
#In some cases, review the Drawing.Point and Drawing.Size of all objects, depending on the resolution of your display.
$form.AutoScaleDimensions = New-Object System.Drawing.SizeF(96, 96)
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,220)
$okButton.Size = New-Object System.Drawing.Size(75,50)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,220)
$cancelButton.Size = New-Object System.Drawing.Size(200,50)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(800,40)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,100)
$textBox.Size = New-Object System.Drawing.Size(800,20)
#For High DPI, We Set Font Size
#In Japanese, "Yu Gothic UI" is the best font type. However, this font is for Japan only. Therefore, the default font definition is undefined.
#$Font = New-Object System.Drawing.Font("Yu Gothic UI",45,([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$Font = New-Object System.Drawing.Font("",45,([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$textBox.Font = $Font
$form.Controls.Add($textBox)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $textBox.Text
$x
}
my custom points
- Set AutoScaleDimensions and AutoScaleMode
#For High DPI, We Set AutoScaleDimensions and AutoScaleMode
#The reason SizeF is set to 96 is that the standard Windows resolution for the display is 96.
#Maybe you shuld try that All object resets Drawing.Point And Drawing.Size
#In some cases, review the Drawing.Point and Drawing.Size of all objects, depending on the resolution of your display.
$form.AutoScaleDimensions = New-Object System.Drawing.SizeF(96, 96)
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Dpi
- Set Font Size for Some parts (ex:textbox)
#For High DPI, We Set Font Size for
([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$Font = New-Object System.Drawing.Font("",45,([System.Drawing.FontStyle]::Regular),
$textBox.Font = $Font
- Trial and error (resetting Drawing.Point and Drawing.Size for all objects)
Points to improve
I don't want to reset Drawing.Point and Drawing.Size.
I want to use the same value for all display types.
Link
- Other code is in the Github:https://github.com/papanda925/PowerShell_HiDPI_GUI_Sample
- Tips my homepage in japanese:https://papanda925.com/?p=2124
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我在最后一个Powershell Gui中发现的工作。在脚本开始时,请调用
setProcessDpiaware()
告诉OS您的GUI是DPI-WAW的,并且不会尝试自行扩展它,从而导致文本模糊。这是本机API,因此您需要P/Invoke:在启用视觉样式之后(与DPI缩放无关,让您的GUI看起来更现代):
具有
[System.Windows.Forms.autoscaScalemode] :: DPI
工作,调用$ form.suspendlayout()
在创建表单和$ form.Resumelayout()
后,立即创建并定位所有控件后。这很重要,以便所有控件将根据DPI的正确尺度定位和尺寸。调用
suppendlayout()
set$ form.autoscaledimensions
和$ form.autoscalemode
在更改表单大小之前。也许订单并不重要,但是在我的代码中,它是这样起作用的。而不是设置
$ form.size
是外表格大小,set$ form.clientsize
,因此您不需要知道框架和字幕大小,这取决于OS版本和用户样式设置。This is what I have found working in my last PowerShell GUI. At the start of your script call
SetProcessDPIAware()
to tell the OS that your GUI is DPI-aware and it doesn't try to scale it on its own which results in blurry text. This is a native API, so you need P/Invoke:After that enable visual styles (propably unrelated to DPI scaling, lets your GUI look more modern):
To have
[System.Windows.Forms.AutoScaleMode]::Dpi
work, call$form.SuspendLayout()
immediately after creating the form and$form.ResumeLayout()
when you have created and positioned all controls. This is important so that all controls will be positioned and sized with correct scaling according to DPI.Call
SuspendLayout()
set$form.AutoScaleDimensions
and$form.AutoScaleMode
before changing form size. Maybe the order isn't important, but in my code it works this way.Instead of setting
$form.Size
which is the outer form size, set$form.ClientSize
so you don't need to know frame and caption size, which depends on OS version and user style settings.谢谢您的回答。我尝试重制示例代码
我不需要知道框架和字幕大小,这取决于OS版本和用户样式设置!
我要添加代码来解决文本框的细长问题。
使用文本框的大字体大小。
对于文本框,是否为每个对象的字体大小调整智能方式?
如果可能的话,我还想删除使用字体尺寸的调整大小。
Thank you for your answer. I tryed to remake a sample code
I don't need to know frame and caption size, which depends on OS version and user style settings!
I'm adding code to solve the problem that the textbox is elongated.
using the big font size of the textbox.
For text boxes, is tuning the font size for each object a smart way?
If possible, I also want to remove the resizing process that uses the font size.