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 に答える