我試圖找出一種方法來清除 Windows 表單圖形中的圖形。我查看了一些使用 Graphics.clear() 呼叫的 C# 示例,但我似乎無法弄清楚我在這里做錯了什么。我什至嘗試使用 $form.visiblity ,它似乎確實重置了圖形,但是當我在回圈中運行它(繪制多條線)時,它會創建一個全新的不會關閉的表單視窗。這是我正在嘗試做的一個示例:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Create a Form
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(600,600)
$form.text = "Line Draw Test"
# Get the form's graphics object
$formGraphics = $form.createGraphics()
function New-LineFromOrigin {
Param ($form)
$width = $form.Width
$height = $form.Height
$greenBrush = new-object Drawing.SolidBrush ("green")
$formGraphics.FillEllipse($greenBrush, $width/2, $height/2, 5, 5 ) # draw an ellipse using rectangle object
$formGraphics.DrawLine($greenBrush, $width/2, $height/2, 0, 0 ) # 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 = "Draw line"
$Button.Add_Click(
{New-LineFromOrigin -form $form}
)
$form.Controls.Add($Button)
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(200,30)
$Button2.Size = New-Object System.Drawing.Size(90,40)
$Button2.Text = "Clear"
$Button2.Add_Click(
{$form.graphics.clear()}
)
$form.Controls.Add($Button2)
$form.Add_Shown({$form.Activate()})
[void] $form.ShowDialog()
uj5u.com熱心網友回復:
有幾種方法可以清除圖形。
使表格無效
$form.Invalidate()
清除圖形并重置顏色
# Clear with a new color on the background
$formGraphics.Clear([System.Drawing.Color]::White)
# Or clear with the form's current background color
$formGraphics.Clear($form.BackColor)
請記住,您實際上并未清除/洗掉任何內容;只是在表單上重新繪制一些東西來替換現有的圖形/繪圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327023.html
