Using Text

I found that it would be really useful if I could get the pixel length of any string in DX that I want to display (I'm using 2D) so I can keep it withing bounds of a textbox for example.  Does anybody know a way of doing this If not, does anybody know another way to achieve the scrolling in a textbox


Answer this question

Using Text

  • BenKC

    Well, I think the intention is to have that functionality available to you using the DrawTextFormat.CalculateRect when you call the Draw function of your Direct3D.Font object.

    Unfortunately this does not currently work since you also are required to pass in a rectangle to the Draw function at the same time but you can't pass in a rectangle size because you haven't calculated it yet...quite the vicious cycle and I'm hoping this will be (is maybe ) fixed in a future managed directX update.

    Until then i know of two methods of calculating the length of your text, one is the one that I currently use but I just found out tonight that there is an error (that I've never noticed) in that extra pixels are thrown onto the end so I'm including a link to another solution that apparently addresses that issue (although I haven't tried it myself yet)

    Method 1:




    Public Function MeasureString(ByVal e As PaintEventArgs, ByVal stringToMeasure As String, ByVal font As System.Drawing.Font) As SizeF
        Return e.Graphics.MeasureString(stringToMeasure, font)
    End Function

     


    So basically that function will return you the rectangle dimensions of your string (height and width)

    but like I mentioned apparently it's about an em to long so this other guy has created a new method which you can read about here


    New MeasureString method

    But eventually you should be able to bypass using both of these methods and we can all begin using the much desired
    DrawTextFormat.CalculateRect

    (also anyone please feel free to correct me if I'm passing incorrect information along here, I'm trying my best to be helpful, but I'm new to a lot of this myself)


    [Edit: I tried to fix whatever funky stuff is going on with the text oddly sizing here (they were VERY small) but I gave up after a bit, but it is larger now at least (and readable)]




  • reno_rens2002

    Much as I hate to bring further bad news... if you are using DirectDraw for your 2d graphics then note that it is deprecated (i.e its still there but you should avoid using it for new projects) in DX9 so you need to use the Sprite class or draw your own sprites using 2 triangles and a texture.

    (I'm assuming means it won't show up in any form in DX10 - maybe someone from DX can confirm this)




  • boran_blok

    Ya I guess I'll try to switch to the DX 9 and .NET


  • Pete US

    wow, thanks! It's a relief to finally see how easy this is. Btw, the link gives an HTTP server error.

    Is this from DX9 I'm using DX7 for 2D and DX8 for netoworking and VB 6 can't find PaintEventArgs. If it's not, does this require me to include another Reference or something If it is, do you know how to do this in earlier versions of DX

  • Leonard French

    Well PaintEventArgs is actually .Net rather than DX9, but either way the problem is old versions.

    Considering DX9 has been out for 2 years now and we are starting to talk about DX10 you are going to find it tough to find people with DX7/8 knowledge....





  • Using Text