2

我有这个代码:

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 周围绘制边框。现在我想使用另一个按钮再次删除它,但我似乎无法让它工作。

4

2 回答 2

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
于 2013-02-12T22:35:15.653 回答
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
于 2013-02-12T22:26:51.300 回答