Jump to content
  • Advertisement

Archived

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

ZenDarva

Length of a string (in pixels)

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 SizeF

size = cGraph.MeasureString(templine, cFont)


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

Share this post


Link to post
Share on other sites
Quote:
Original post by ZenDarva
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 SizeF

size = 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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!