尝试使用powershell来显示带有按钮的表单 按钮将执行测试。这是工作。 如果测试失败,按钮旁边的圆圈将为红色 vs绿色。这可以吗?如果是这样,FillEllipse是正确的方式吗? 我需要创建一个新表单吗?
#Load the GDI+ and WinForms Assemblies
`enter code here`[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Create pen and brush objects
$myBrush = new-object Drawing.SolidBrush green
$mypen = new-object Drawing.Pen black
# Create a Rectangle object for use when drawing rectangle
#$rect = new-object Drawing.Rectangle 10, 10, 180, 180
# Create a Form
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(600,600)
$form.text = "Network Connectivity Test"
#$form = New-Object Windows.Forms.Form
# Get the form's graphics object
$formGraphics = $form.createGraphics()
function do-interface {
#
}
#$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Ethernet 1"
#$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Size(75,50)
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Form.Controls.Add($Label)
# Define the paint handler
$form.add_paint(
{
$formGraphics.FillEllipse($myBrush, 350, 80, 15, 15 ) # draw an ellipse using rectangle object
#$formGraphics.FillEllipse($myBrush, $rect) # draw an ellipse using rectangle object
}
)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(30,30)
$Button.Size = New-Object System.Drawing.Size(90,40)
$Button.Text = "Interfaces"
$Button.Add_Click({do-interface})
$form.Controls.Add($Button)
$form.Add_Shown({$form.Activate()})
[void] $form.ShowDialog()
答案 0 :(得分:1)
只需将FillEllipse添加到 do-interface 方法
即可$myBrush = new-object Drawing.SolidBrush ("green", "red")[$testFailed]
$formGraphics.FillEllipse($myBrush, 350, 80, 15, 15 )
以下代码将返回绿色,如果 $ testFailed $ false 或 0 且红色< / strong>如果是 $ true 或 1
("green", "red")[$testFailed]
此外,您不需要在add_paint中填充椭圆。所以整个例子看起来像这样:
#Load the GDI+ and WinForms Assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Create pen and brush objects
$mypen = new-object Drawing.Pen black
# Create a Rectangle object for use when drawing rectangle
#$rect = new-object Drawing.Rectangle 10, 10, 180, 180
# Create a Form
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(600,600)
$form.text = "Network Connectivity Test"
#$form = New-Object Windows.Forms.Form
# Get the form's graphics object
$formGraphics = $form.createGraphics()
function do-interface {
$testFailed = Get-Random -Maximum 2
Write-Host $testFailed
$myBrush = new-object Drawing.SolidBrush ("green", "red")[$testFailed]
$formGraphics.FillEllipse($myBrush, 350, 80, 15, 15 ) # draw an ellipse using rectangle object
}
#$Label = New-Object System.Windows.Forms.Label
#$Label.Text = "Ethernet 1"
#$Label.AutoSize = $true
#$Label.Location = New-Object System.Drawing.Size(75,50)
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
#$Form.Controls.Add($Label)
# Define the paint handler
$form.add_paint(
{
#delete this method, if you need it
#$formGraphics.FillEllipse($myBrush, $rect) # draw an ellipse using rectangle object
}
)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(30,30)
$Button.Size = New-Object System.Drawing.Size(90,40)
$Button.Text = "Interfaces"
$Button.Add_Click({do-interface})
$form.Controls.Add($Button)
$form.Add_Shown({$form.Activate()})
[void] $form.ShowDialog()