Sign in to follow this  
Shadowflare

Font Width Problem

Recommended Posts

My problem is real simple: I'm making a simple textbox control using D3DXFont. The problem is with clipping. When you write in a single line textbox, the text is clipped if it is larger than the textbox, so that you can see what you've most recently typed. I hope that makes sense :P I can get my textbox to do this, but the problem is that the characters are not all the same width. My textbox does not need to change size so at the moment I just have a maximum number of characters that are visible (i.e. if the text is larger than the text area, it will display the last 40 characters.) Since all the characters are different widths, if you enter many large characters, the text does not get properly clipped and the characters go out of sight. If you enter many small characters it does not clip it far enough and so there is a big empty space at the end of the textbox. I'm certain there has to be a better way to do this but I haven't found anything that would indicate the opposite :( Any help would be appreciated!

Share this post


Link to post
Share on other sites
You can check the final size of text to be drawn by calling ID3DXFont::DrawText with DT_CALCRECT format flag set.

Quote:
From MSDN

DT_CALCRECT
Determines the width and height of the rectangle. If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the pRect parameter and extends the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text.


You can call DrawText twice - for calculation only, and for the actual drawing.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this