I am trying to draw random patterns in a picture box that has a black panel over the surface hiding the drawing below. But when I make the panel disappear (panel.visible = false), the graphics in the picture box UNDER the panel are also erased. In the code below (most of which was grabbed from a VB10 book) the process works if I put the for...next loop creating the ellipses in the Private Sub picCanvas_MouseMove. The random ellipse patterns persist when the Hide Panel Button is clicked. But if I create the ellipse patterns in the Private Sub MakeGraphics_Click (click a button on the form), the ellipse patterns under the panel are erased when the panel.visible = false.
In my program I do not need the mouse down, etc events to draw squiggly lines, I only need to create the graphics when a form button is clicked.
I am probably missing something with representing the bmp where the images are stored but I am not sure what.
Thanks for any help!!!
Gary V
Public Class Form1
Dim PenColor As New Pen(Color.Red, 2)
Dim X As Long
Dim Y As Long
Dim W As Long
Dim H As Long
Private m_Bitmap As Bitmap
Private m_Graphics As Graphics
Private m_Drawing As Boolean
Private m_X As Integer
Private m_Y As Integer
Private Sub Form1_Load() Handles MyBase.Load
Call MakeNewBitmap()
End Sub
Private Sub mnuFileClear_Click() Handles mnuFileClear.Click
Call MakeNewBitmap()
End Sub
Private Sub MakeNewBitmap()
Dim wid As Integer = picCanvas.ClientSize.Width
Dim hgt As Integer = picCanvas.ClientSize.Height
m_Bitmap = New Bitmap(wid, hgt)
m_Graphics = Graphics.FromImage(m_Bitmap)
m_Graphics.Clear(Me.BackColor)
picCanvas.Image = m_Bitmap
End Sub
Private Sub picCanvas_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
m_Drawing = True
m_X = e.X
m_Y = e.Y
End Sub
Private Sub picCanvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseMove
If Not m_Drawing Then Exit Sub
For Temp = 1 To 8
Randomize()
X = 5 + (Rnd() * (m_Bitmap.Width - 30))
Y = 5 + (Rnd() * (m_Bitmap.Height - 30))
H = 2 + (Rnd() * 20)
W = 2 + (Rnd() * 20)
m_Graphics.DrawEllipse(PenColor, X, Y, W, H)
Next
m_Graphics.DrawLine(Pens.Black, m_X, m_Y, e.X, e.Y)
m_X = e.X
m_Y = e.Y
picCanvas.Refresh()
End Sub
Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp
m_Drawing = False
End Sub
Private Sub MakeGraphics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeGraphics.Click
m_Graphics = picCanvas.CreateGraphics
For Temp = 1 To 8
Randomize()
X = 5 + (Rnd() * (picCanvas.Width - 30))
Y = 5 + (Rnd() * (picCanvas.Height - 30))
H = 2 + (Rnd() * 20)
W = 2 + (Rnd() * 20)
m_Graphics.DrawEllipse(PenColor, X, Y, W, H)
Next
End Sub
Private Sub HidePanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HidePanel.Click
If Panel1.Visible = True Then Panel1.Visible = False Else Panel1.Visible = True
End Sub
End Class