2013-02-12 54 views
2

删除以前创建的边界我有这样的代码:试图在Visual Basic

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 

,这周围绘制一个图片框的边框。现在我想用另一个按钮来删除它,但我似乎无法使其工作。

回答

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

使您的图形对象为全局。然后你可以调用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