Хочется рисовать на PictureBox из другого класса, но то что перерисовывается (красная линия) сразу закрашивается родным методом (зеленая линия). Как быть? Сейчас код такой: Private myPainter As OuterPainter Private pictureBox1 As New PictureBox() Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Dock the PictureBox to the form and set its background to white. pictureBox1.Dock = DockStyle.Fill pictureBox1.BackColor = Color.White ' Connect the Paint event of the PictureBox to the event handling method. AddHandler pictureBox1.Paint, AddressOf Me.pictureBox1_Paint AddHandler pictureBox1.Resize, AddressOf Me.pictureBox1_Resize myPainter = New OuterPainter(pictureBox1) ' Add the PictureBox control to the Form. Me.Controls.Add(pictureBox1) End Sub 'Form1_Load Private Sub pictureBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) PictureBox1.Invalidate() End Sub
Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) ' Create a local version of the graphics object for the PictureBox. Dim g As Graphics = e.Graphics ' Draw a line in the PictureBox. g.DrawLine(System.Drawing.Pens.Green, pictureBox1.Right, _ pictureBox1.Top, pictureBox1.Left, pictureBox1.Bottom) myPainter.Update() End Sub End Class
Public Class OuterPainter Private myControl As Control Public Sub New(ByVal myPictureBox As Control) myControl = myPictureBox End Sub Public Sub Update() Dim g As Graphics = myControl.CreateGraphics g.DrawLine(System.Drawing.Pens.Red, _ myControl.Left, myControl.Top, myControl.Right, myControl.Bottom) g.Dispose() End Sub End Class
Ответить
|