我有这段代码:
Sub drawborder()
Dim objGraphics As Graphics
objGraphics = Me.CreateGraphics
objGraphics.Clear(System.Drawing.SystemColors.Control)
objGraphics.DrawRectangle(System.Drawing.Pens.Red, picShowPicture.Left - 1, picShowPicture.Top - 1, picShowPicture.Width + 1, picShowPicture.Height + 1)
objGraphics.Dispose()
borderstatus.Text = "Border Drawn"
End Sub
这会在PictureBox周围绘制一个边框。现在我想用另一个按钮再次删除它,但我似乎无法使它工作。
答案 0 :(得分:2)
请勿使用CreateGraphics
,这只是一个临时图纸,当您最小化表格或将其移出屏幕等时将被删除。
尝试持有变量,然后使表单无效:
Private _ShowBorder As Boolean
Public Sub New()
InitializeComponent()
Me.DoubleBuffered = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_ShowBorder = Not _ShowBorder
Me.Invalidate()
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
e.Graphics.Clear(Me.BackColor)
If _ShowBorder Then
e.Graphics.DrawRectangle(Pens.Red, PictureBox1.Left - 1, PictureBox1.Top - 1, PictureBox1.Width + 1, PictureBox1.Height + 1)
End If
MyBase.OnPaint(e)
End Sub
答案 1 :(得分:1)
使您的图形对象全局化。然后你可以调用objGraphics.Clear(...),它应该清除任何绘制图形的屏幕。 例如:
Dim objGraphics as Grahpics
Public Sub Form1_Load(...) Handles Form1.Load
objGraphics = Me.CreateGraphics()
End Sub
Public Sub DrawBorder()
objGraphics.Clear(System.Drawing.SystemColors.Control)
objGraphics.DrawRectangle(System.Drawing.Pens.Red, picShowPicture.Left - 1, picShowPicture.Top - 1, picShowPicture.Width + 1, picShowPicture.Height + 1)
borderstatus.Text = "Border Drawn"
End Sub
Public Sub ClearScreen()
objGraphics.Clear(System.Drawing.SystemColors.Control)
borderstatus.Text = "No Border"
End Sub