Results 1 to 2 of 2

Thread: GDI+ vector graphics and text

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    5

    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.

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width