#### Archived

This topic is now archived and is closed to further replies.

# Length of a string (in pixels)

This topic is 5256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm working in managed DX in VB.net. After a bit of fooling around I've gotten my surfaces to let me draw text on them in whatever font i want, (using the font class), now assuming i have a fully filled out font class, how do i go about figuring out how long a string of text would be if it were drawn using that font? I've done this in VB6 using an api, but it used structures i don't think exist in .net. If i'm wrong, could someone point me at how to use a non-managed api in .net? Thanks. [edited by - ZenDarva on June 4, 2004 9:07:22 PM]

##### Share on other sites
Use DrawTextFormat.CalculateRect as a flag to Font.DrawText and it will return the size the text needs.

##### Share on other sites
Hmm, i think we must be talking about different font classes. The one i''m using doesn''t have any drawtext method. And the drawtext method for the directdraw.surface only has one manner to be called in: surface.drawtext(x as integer, y as integer, text as string, drawatlastposition as boolean).

Is there some way i can use the DC from the surface to use the GDI functions to draw on the surface? Might be messy, but i suppose i''ll take it.

##### Share on other sites
I''m really confused now. I''ve looked at using GDI+ to draw onto the directX sufrace i''m using, since i can''t find any actual way of determining the width of the text i wanna draw. It doesn''t look possible to use gdi+ either. GAH, anyone, help please!!?

##### Share on other sites
After some more research i re-discovered the api call i used last time i had to do this:
 Declare Auto Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, ByVal lpSize As Size) As Long

Unfortuantly, it wants hdc as a long, and directdraw.surface.getdc returns system.intptr. How would i go about converting it? I''ve already tried using clng(surface.getdc) and it didn''t like that at all. So next i tried the following:

Dim blah As New System.Drawing.Size            GetTextExtentPoint32(cSurf.Surf.GetDc.ToInt64, sTitle, sTitle.Length, blah)            Debug.WriteLine(blah.Width & " Height  " & blah.Height)

I''m getting a nullreference error when it hits the gettextextentpoint32 call. Now, everything is defined... csurf''s fine, it''s been drawn to extensivly in the sub before this, stitle is set, and i new''d blah right before (Yes, blah''s a stupid variable name, and i''ll change it, it''s just for testing right now) i tried to use it. Anyone have any advice, or perhaps a .net equivilent??

##### Share on other sites
Well, i got it, but it's a bit fugly. what i did was dim a friend object of type graphics in the engine. Then when I create my backbuffer I intilize the graphics obj like so

cGraph = parent.CreateGraphics

Parent is a system.windows.forms.form object, a reference to the form creating the directx object.

Then, any time i need to know the width of a string i just:
Dim size As SizeFsize = cGraph.MeasureString(templine, cFont)

Like i said, hackish. And it wastes resources, :( But it was the only thing i could find.

##### Share on other sites
Quote:
 Original post by ZenDarvaWell, i got it, but it's a bit fugly. what i did was dim a friend object of type graphics in the engine. Then when I create my backbuffer I intilize the graphics obj like socGraph = parent.CreateGraphicsParent is a system.windows.forms.form object, a reference to the form creating the directx object.Then, any time i need to know the width of a string i just: Dim size As SizeFsize = cGraph.MeasureString(templine, cFont)Like i said, hackish. And it wastes resources, :( But it was the only thing i could find.

Why not make a table with the lengths of each character (256 in total), at initialization. Then make your own function which calculates the length.

##### Share on other sites
hey i got this

Declare Auto Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32offuckthisissolongA" (ByVal hdc As veryLong, ByVal lpsz As String, ByVal cbString As Long, ByVal lpSize As Size) As Long

But as somebody else said..its a bit fuckkish

##### Share on other sites
Mm. This is so easy in Java. ;)

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 19
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633362
• Total Posts
3011531
• ### Who's Online (See full list)

There are no registered users currently online

×