캔버스에 그리는 루틴을 직접 만들 수 있습니다.
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim canvas As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim canvasGraphics As Graphics = Graphics.FromImage(canvas)
Dim cellRect As New Rectangle(100, 100, 100, 100)
cell.draw(canvasGraphics, cellRect, Color.Red, New Pen(New SolidBrush(Color.Green), 1))
PictureBox1.Image = canvas
End Sub
Public Class cell
Public Shared Sub draw(ByRef canvasGraphics As Graphics, ByVal cellRect As Rectangle, ByVal fillColor As Color, ByVal borderPen As Pen)
Dim renderedCell As New Bitmap(cellRect.Width, cellRect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim borderRect As New Rectangle(0, 0, cellRect.Width - 1, cellRect.Height - 1)
Dim context As BufferedGraphicsContext
Dim bGfx As BufferedGraphics
context = BufferedGraphicsManager.Current
context.MaximumBuffer = New Size(cellRect.Width + 1, cellRect.Height + 1)
bGfx = context.Allocate(Graphics.FromImage(renderedCell), New Rectangle(0, 0, cellRect.Width, cellRect.Height))
Dim g As Graphics = bGfx.Graphics
g.Clear(fillColor)
g.DrawRectangle(borderPen, borderRect)
bGfx.Render()
canvasGraphics.DrawImage(renderedCell, cellRect.Location)
End Sub
End Class
End Class
PictureBox1은 Cell 클래스의 멤버가 아닌 Form 클래스의 멤버입니다. 이 방법을 계속 사용하려면 PictureBox 인수를 사용하는 생성자가 필요하며 AddHandler 문을 사용하여 이벤트를 구독합니다. Windows Forms 프로그래밍 btw에 대한 입문서를 통해 많은 이점을 얻을 수 있습니다. 기본 사항을 모르는 경우에는 아주 멀리 할 수 없습니다. –
가능한 [다른 클래스/파일에 정의 된 개체 이벤트 처리] 중복 (http://stackoverflow.com/questions/24698988/handle-events-of-object-defined-in-another-class-file) – Plutonix