2013-02-12 85 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