|
-
Dec 4th, 2006, 03:45 PM
#1
Thread Starter
Hyperactive Member
[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 
-
Dec 4th, 2006, 04:07 PM
#2
Re: [2005] drawing text in GDI
This is a simple example to give you some explaination on using GDI+ to draw text
VB Code:
'The Paint event is used to ensure that the drawing is done verytime the control
'requires a repaint (like moving, resizing...)
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Me.Paint
'Delclare a font
Dim fntName As String = "Times New Roman"
Dim fntSize As Single = 14
Dim fntStyle As FontStyle = FontStyle.Bold
Dim fnt As New Font(fntName, fntSize, fntStyle)
'Declare a brush
Dim brsh As Brush = Brushes.DarkGreen
'Declare a location to start the drawing
Dim loc As Point = New Point(50, 50)
'Now you're redy to draw using DrawString. To draw on something, you need to
'use the graphics object of that thing. This example draws the text on the for,
'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)
End Sub
-
Dec 4th, 2006, 06:12 PM
#3
Thread Starter
Hyperactive Member
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 
-
Dec 4th, 2006, 06:28 PM
#4
Re: [2005] drawing text in GDI
 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()...
-
Dec 4th, 2006, 06:46 PM
#5
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.
-
Dec 4th, 2006, 06:56 PM
#6
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+
-
Dec 5th, 2006, 04:19 PM
#7
Thread Starter
Hyperactive Member
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 
-
Dec 5th, 2006, 04:32 PM
#8
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.
-
Dec 5th, 2006, 06:27 PM
#9
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.
-
Dec 5th, 2006, 06:44 PM
#10
Re: [2005] drawing text in GDI
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|