如何使用窗体调整大小来调整窗体控件的大小

时间:2019-05-20 10:29:57

标签: powershell

在此视频中,我想知道如何使用.net调整表单的大小,在powershell中如何做到这一点,有人可以在我的示例中添加必要的代码?

https://www.youtube.com/watch?v=QVi1ve5qHXA

我尝试使用Dock或achor,但不使用。

我想要实现的结果与您在视频中看到的完全一样,已插入视频以阐明我打算做的事情。 当我拖动表单的右上角,或在“最大化”窗口中使用标准按钮时,必须调整表单中的每个对象:按钮,列表框,输出框等的大小。

CLS

$width = 1120
$Height = 560

$Form = New-Object System.Windows.Forms.Form    
$form.AutoSize = $true
$Form.MaximizeBox = $true
$form.ClientSize.Width = 2240
$form.ClientSize.Height = 1120
$Form.StartPosition = "CenterScreen"

$imagelistPC = New-Object 'System.Windows.Forms.ImageList'
$listviewPC = New-Object System.Windows.Forms.ListView
$listviewPC.Location = New-Object System.Drawing.Point(20,125) 
$listviewPC.Size = New-Object System.Drawing.Size(585,180) 
$form.Controls.Add($listviewPC) 

$button1user = New-Object System.Windows.Forms.Button 
$button1user.Location = New-Object System.Drawing.Size(625,30) 
$button1user.Size = New-Object System.Drawing.Size(110,40)
$button1user.Cursor = [System.Windows.Forms.Cursors]::Hand
$button1user.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 7, [System.Drawing.FontStyle]::Regular)
$button1user.Text = "test1" 
$button1user.BackColor = "Yellow"
$button1user.Add_Click({test1}) 
$button1user.AutoSize = $true
$Form.Controls.Add($button1user)  

$button2user = New-Object System.Windows.Forms.Button 
$button2user.Location = New-Object System.Drawing.Size(625,80)
$button2user.Size = New-Object System.Drawing.Size(110,40)
$button2user.Cursor = [System.Windows.Forms.Cursors]::Hand
$button2user.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 7, [System.Drawing.FontStyle]::Regular)
$button2user.Text = "test2" 
$button2user.BackColor = "Yellow"
$button2user.Add_Click({test2}) 
$Form.Controls.Add($button2user)  

$button3user = New-Object System.Windows.Forms.Button 
$button3user.Location = New-Object System.Drawing.Size(625,130) 
$button3user.Size = New-Object System.Drawing.Size(110,40)
$button3user.Cursor = [System.Windows.Forms.Cursors]::Hand
$button3user.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 7, [System.Drawing.FontStyle]::Regular)
$button3user.Text = "test3" 
$button3user.BackColor = "Yellow"
$button3user.Add_Click({test3})
$Form.Controls.Add($button3user) 

$button1pc = New-Object System.Windows.Forms.Button 
$button1pc.Location = New-Object System.Drawing.Size(745,30) 
$button1pc.Size = New-Object System.Drawing.Size(110,40)
$button1pc.Cursor = [System.Windows.Forms.Cursors]::Hand
$button1pc.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 7, [System.Drawing.FontStyle]::Regular)
$button1pc.Text = "test A" 
$button1pc.BackColor = "Yellow"
$button1pc.Add_Click({COMPUTERCHANGEOU}) 
$Form.Controls.Add($button1pc)  

$button2pc = New-Object System.Windows.Forms.Button 
$button2pc.Location = New-Object System.Drawing.Size(745,80) 
$button2pc.Size = New-Object System.Drawing.Size(110,40)
$button2pc.Cursor = [System.Windows.Forms.Cursors]::Hand
$button2pc.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 7, [System.Drawing.FontStyle]::Regular)
$button2pc.Text = "test B" 
$button2pc.BackColor = "Yellow"
$button2pc.Add_Click({b}) 
$Form.Controls.Add($button2pc) 

$button3pc = New-Object System.Windows.Forms.Button 
$button3pc.Location = New-Object System.Drawing.Size(745,130) 
$button3pc.Size = New-Object System.Drawing.Size(110,40)
$button3pc.Cursor = [System.Windows.Forms.Cursors]::Hand
$button3pc.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 7, [System.Drawing.FontStyle]::Regular)
$button3pc.Text = "test C" 
$button3pc.BackColor = "Yellow"
$button3pc.Add_Click({c}) 
$Form.Controls.Add($button3pc) 

$outputBox = New-Object System.Windows.Forms.TextBox 
$outputBox.Location = New-Object System.Drawing.Size(20,320) 
$outputBox.Size = New-Object System.Drawing.Size(585,180) 
$outputBox.MultiLine = $True 
$outputBox.ReadOnly = $True
$outputBox.Font = New-Object System.Drawing.Font("Calibri",11,[System.drawing.FontStyle]::Bold)
$outputBox.ForeColor = [Drawing.Color]::Green
$outputBox.ScrollBars = "Vertical" 
$Form.Controls.Add($outputBox) 

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

2 个答案:

答案 0 :(得分:0)

我不会为您编写代码,因为这会花费很多时间和精力,但是我可以举个例子。

首先让我们制作一些表单元素并将其锚定:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '500,230'
$Form.text = "Some title"
$Form.StartPosition = 'CenterScreen'

$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Point(200,150)
$Button1.Size = New-Object System.Drawing.Size(100,50)
$Button1.Text = "START"
$Button1.Anchor = [System.Windows.Forms.AnchorStyles]::Top `
-bor [System.Windows.Forms.AnchorStyles]::Bottom `
-bor [System.Windows.Forms.AnchorStyles]::Left `
-bor [System.Windows.Forms.AnchorStyles]::Right

$ListBox1 = New-Object system.Windows.Forms.ListBox
$ListBox1.text = "listBox"
$ListBox1.location = New-Object System.Drawing.Point(50,50)
$ListBox1.size = New-Object System.Drawing.Size(50,50)
$ListBox1.Anchor = [System.Windows.Forms.AnchorStyles]::Top `
-bor [System.Windows.Forms.AnchorStyles]::Bottom `
-bor [System.Windows.Forms.AnchorStyles]::Left `
-bor [System.Windows.Forms.AnchorStyles]::Right

$form.Controls.AddRange(@($Button1, $ListBox1))
$Form.ShowDialog()

您现在可以看到,元素正在调整大小。尝试注释掉顶部和底部锚点,看看会发生什么。

但是,该解决方案有很大的缺陷。如果要调整它们的大小,元素将开始重叠。您可以使用(例如)Panel Class解决此问题,因此我们可以创建不同种类的布局。我将用TableLayoutPanel向您展示一些示例代码:

$tableLayoutPanel1 = New-Object System.Windows.Forms.TableLayoutPanel
$tableLayoutPanel1.RowCount = 2 #how many rows
$tableLayoutPanel1.ColumnCount = 2 #how many columns
$tableLayoutPanel1.Controls.Add($Button1, 0, 0) #choose where to place button
$tableLayoutPanel1.Controls.Add($ListBox1, 1, 1) #choose where to place listbox
$tableLayoutPanel1.Dock = [System.Windows.Forms.DockStyle]::Fill #choose style

#make rows the same size
$tableLayoutPanel1.RowStyles.Add((new-object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 50)))
$tableLayoutPanel1.RowStyles.Add((new-object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 50)))
#make columns the same size
$tableLayoutPanel1.ColumnStyles.Add((new-object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent,50)))
$tableLayoutPanel1.ColumnStyles.Add((new-object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent,50)))

$Form.controls.AddRange(@($tableLayoutPanel1)) #don't add button and listbox here because they're already added by tablelayoutpanel
$Form.ShowDialog()

如果要合并列或行,可以尝试:

$tableLayoutPanel1.SetColumnSpan($Button1,2)

或:

$tableLayoutPanel1.SetRowSpan($Button1,2)

附加说明:您可以删除具有表单元素大小和位置值的行,因为如果您使用TableLayoutPanel,行将不会发生。您可以找到我的示例here的工作代码。

答案 1 :(得分:0)

根据您的视频。您正在以设计器模式而不是正在运行的应用程序调整表单面板上各个嵌入式表单元素的大小。

这是拖放设计器所允许的。没有用于PowerShell的内置drang拖放设计器。您可以从VSCode MarketPlace购买PowerShell Pro Tools或使用PoshGui并从那里保存和使用/重新编写代码的VSCode扩展。

您似乎在说,您希望用户能够调整正在运行的应用程序上的那些按钮或其他对象的大小。如果我在这里不正确,请更正我,但是,如果是这样,那不是真的,我曾经在任何应用程序中使用任何语言看到过。另外,我不确定会解决什么用例(在运行时根据元素调整大小)。

确定大小,并根据表单大小自动调整元素大小,这就是nemze显示的内容。