Ok, I want 开发者_Go百科to create a class that will handle a special rectangle graphic.
In my form, I want to have two of these special rectangles. So, basically, I need two instances of that class in my form, right?
I manage to initialize two, alright. But, how exactly am I supposed to manage drawing/graphics etc in a class, and the results to be displayed in my form?
There are a few concepts you need to figure out to put this together:
- You need to track the objects you're going to draw. You're partially there but this is usually done with a collection of some sort like List(of ...)
- You need to handle the Paint event of your Form class (or Panel or Control or whatever visual object you want to draw in or on)
- You need to draw your objects within you Paint handler
- Whenever the state of your application changes you need to call Invalidate on the object that is being painted to "force" a fresh repaint.
Here's a quick snippet:
' suppose you have:
Private _myRects as New List(of Rectangle) ' populated elsewhere
' then you handle the paint event of a UI control
Private Sub Control_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim g As Graphics = e.Graphics
' loop through your collection drawing each rectangle:
for each rect As Rectangle in _myRects
g.FillRectangle(Brushes.Aqua, rect)
next for
...more drawing as needed
end sub
And here is a pretty nice tutorial on .NET painting with VB. If you follow it through you should have all the pieces to do any kind of 2D .NET drawing you like. (The fun doesn't start until page 2 but do not skip page 1!)
Sounds like the two thing you need to read up on are Developing Custom Controls and Using GDI+ in Windows Forms.
Grab a comfy chair and a nice cup of hot cocoa; you have a lot of reading to do.
精彩评论