|
-
Dec 14th, 2010, 03:36 PM
#1
Thread Starter
New Member
GDI+ vector graphics and text
I have the following Windows forms application using default names for objects.
1 form, and 1 button.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
Dim myRect As Rectangle
myRect = New Rectangle(0, 0, 10, 10)
formGraphics = Me.CreateGraphics
formGraphics.DrawRectangle(myPen, myRect)
myPen.Dispose()
formGraphics.Dispose()
End Sub
End Class
After pushing the button, what this does is place a small red rectangle in the top left corner of the form.
I would like to add a small text to the right of the rectangle, or under and to the left and etc. Is there a simple way to do this, or do I have to use several GDI+ functions together. I would like to use as much of the .NET classes as possible instead of re-creating code for this if possible. Any suggestions?
Also, as a further note. The idea is a map that has icons moving around it, indicating different kind of units. Cars, planes etc.
-
Dec 15th, 2010, 06:26 PM
#2
Re: GDI+ vector graphics and text
Try this:
Code:
Public Class Form1
Private booSquare As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
booSquare = Not booSquare
Me.Invalidate()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim formGraphics As System.Drawing.Graphics = e.Graphics
If booSquare Then
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim myRect As New Rectangle(0, 0, 10, 10)
formGraphics.DrawRectangle(myPen, myRect)
formGraphics.DrawString("MyText", New Font("Arial", 7, FontStyle.Regular), Brushes.Red, myRect.X + myRect.Width + 3, myRect.Y)
myPen.Dispose()
End If
formGraphics.Dispose()
MyBase.OnPaint(e)
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|