Results 1 to 10 of 10

Thread: [2005] drawing text in GDI

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    [2005] drawing text in GDI

    I tried and tried and tried for like an hour to get GDI to draw some text into a graphics area but they made it too hard on purpose I think. I keep getting stuck on the choosing the font part. Why can't I just supply a font by name?!?! If anyone has a working .net 2.0 chunk of code for it, I'd love to see it in it's lovely full workingness.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] drawing text in GDI

    This is a simple example to give you some explaination on using GDI+ to draw text
    VB Code:
    1. 'The Paint event is used to ensure that the drawing is done verytime the control
    2.     'requires a repaint (like moving, resizing...)
    3.     Private Sub Form1_Paint(ByVal sender As Object, _
    4.                     ByVal e As System.Windows.Forms.PaintEventArgs) _
    5.                     Handles Me.Paint
    6.         'Delclare a font
    7.         Dim fntName As String = "Times New Roman"
    8.         Dim fntSize As Single = 14
    9.         Dim fntStyle As FontStyle = FontStyle.Bold
    10.         Dim fnt As New Font(fntName, fntSize, fntStyle)
    11.  
    12.         'Declare a brush
    13.         Dim brsh As Brush = Brushes.DarkGreen
    14.  
    15.         'Declare a location to start the drawing
    16.         Dim loc As Point = New Point(50, 50)
    17.  
    18.         'Now you're redy to draw using DrawString. To draw on something, you need to
    19.         'use the graphics object of that thing. This example draws the text on the for,
    20.         'so I get the graphics object of the form by calling Me.CreateGraphics
    21.         Dim g As Graphics = Me.CreateGraphics
    22.         g.DrawString("This is a line", fnt, brsh, loc)
    23.     End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: [2005] drawing text in GDI

    aha! Now why couldn't they have just put that in the MSDN Library? (well duh, cuz they hate us) I'm gonna draw up a storm now! A program's just not a program without crap flying around all over the place lol.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] drawing text in GDI

    Quote Originally Posted by stanav
    'so I get the graphics object of the form by calling Me.CreateGraphics
    Dim g As Graphics = Me.CreateGraphics
    g.DrawString("This is a line", fnt, brsh, loc)
    Don't forget to dispose of the graphics object after you use it by calling g.dispose()...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] drawing text in GDI

    You don't create a Graphics object in a Paint event handler. It is already supplied via the e.Graphics property. Thus it doesn't require disposal either because that's handled elsewhere.

    Frankly, I can't believe that this was an issue in the first place. The MSDN topics for the DrawString method contain numerous links to topics about the Font class and how to create one. You can't expect MSDN to have everything you want all on the one page. It's a reference library and it's a well constructed one. Everything you need is generally within a few links if you only look. Many people enjoy criticising the MSDN library but it is what it is and if you are determined to make it work the way you want then you won't always be happy. Take a step back and look at how IT works, then change the way you use it to match that structure. If you do that you'll almost always be able to find what you want. I did and I can, so there's no reason that everyone else can't do the same. Believe me, I'm no genius.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] drawing text in GDI

    I agree with John. Before you claim that MS has "made things too hard" you should really try to look around. There are multiple resources for finding information.

    1) Search this forum
    2) Actually look in the MSDN documentation where you will find an example
    3) Search the web. Microsoft has a nice site of examples setup, that, wouldn't you know it, also has examples on writing text via GDI+

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: [2005] drawing text in GDI

    I did quite an extensive search of the MSDN library through the helpfile thingy and in the 0.00000000001% of content that's actually an answer to the "How do I code it to do ______ and why?" question, it didn't have anything for text in GDI. Plus, why would I put it in the form paint event? Then I'd just use a label. I'm putting it in a timer tick sub or a button sub.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] drawing text in GDI

    thats funny because the 2 links in my post above both have code examples for drawing text in GDI+, and both examples are right from Microsoft, one being right out of the documentation of MSDN.

    As far as your question.

    The reason you would put it in the paint event is because if the form gets invalidated, it will repaint itself. If your code to do your drawing is not in the paint event (or called from it) then the form will simply wipe clean.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] drawing text in GDI

    Absolutely ALL GDI+ drawing gets done on a Paint event, either in the Paint event handler itself or in a method that get's called from the Paint event handler. There's a link to a GDI+ tutorial in my signature and it explains how and why in the very first FAQ question at the top of the first page. Note that I found that link by searching Google for "gdi+" ".net". There are plenty of results for "gdi+" tutorial too.

    Any beginner tutorial on GDI+ would lead you to the Graphics class. That would prompt me to go straight to the MSDN help topic for the Graphics class, which would lead me to the help topic for the Graphics.DrawString method.

    Everyone seems to think that they should search MSDN and get a snippet of code that does exactly what they want. I almost never search MSDN. If you know what types and/or members you're using then you should go straight to the help topics for those types and/or members. Now, there are times that you don't know exactly what types and/or members to use, and those times are more frequent the less experienced you are. Like I said though, any rudimentary knowledge of GDI+ in .NET would tell you that you had to use the Graphics class.

    Also, when you do search MSDN, you don't really use terms like "How do I code it to do ______ and why?" do you? Many people ruin their chances of finding meaningful results by using terms that are either too general or too specific. You should use meaningful key words and nothing more. Inclusions like "how do I" are pointless and counter-productive. How many pages do you think will include those words? As you can see from the Google searches I suggested you don't need much to get what you want. Extraneous key words will only reduce the efficiency of the search.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] drawing text in GDI

    Quote Originally Posted by jmcilhinney

    Also, when you do search MSDN, you don't really use terms like "How do I code it to do ______ and why?" do you? Many people ruin their chances of finding meaningful results by using terms that are either too general or too specific. You should use meaningful key words and nothing more. Inclusions like "how do I" are pointless and counter-productive. How many pages do you think will include those words? As you can see from the Google searches I suggested you don't need much to get what you want. Extraneous key words will only reduce the efficiency of the search.
    That goes for any search engine

    Half the people that answer questions on this forum often just google up the answer in 5 seconds and post it. The other half of answered questions come from experience

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