Jump to content
  • Advertisement
Sign in to follow this  
deadimp

Calculating String Dimensions in WindowsGDI (SOLVED, due to retardation)

This topic is 5053 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 having a problem with my 'string_width' and 'string_height' function. They are functions that utilize WindowsGDI to caclulate a strings width and height, and for some reason it measures the string in the default font. I can change the font of the HDC when I draw it, but nothing is affected in the output of either of these functions. Here's the code:
int string_width(string &strSrc) {
 SIZE curSize;
 GetTextExtentPoint32(hdc,strSrc.c_str(),strSrc.length(),&curSize);
 return curSize.cx;
}

int string_height(string &strSrc) {
 //Does not support multi-line yet.
 SIZE curSize;
 GetTextExtentPoint32(hdc,strSrc.c_str(),strSrc.length(),&curSize);
 return curSize.cy;
}
[Edited by - deadimp on January 17, 2005 9:02:39 PM]

Share this post


Link to post
Share on other sites
Advertisement
I think this answers your question : GetCharWidth32. I use it in my GraphicsManager like follows:


int SingletonGraphicsManager::GetStringWidth(const char* fmt, ...) const
{
char text[256];
va_list ap;
if(fmt==NULL)
return 0;
va_start(ap, fmt);
vsprintf(text, fmt, ap);
va_end(ap);

HDC hdc = boost::any_cast<HDC>(SettingsManager::Instance().GetSetting("theHDC"));
int totalLeng = 0;
int incLeng = 0;
unsigned int charpos = 0;

while(charpos < strlen(fmt))
{
GetCharWidth32(hdc, fmt[charpos], fmt[charpos], &incLeng);
totalLeng += incLeng;
charpos++;
}

return totalLeng;
}



HTH,
Jim.

Share this post


Link to post
Share on other sites
Good gosh I feel incredulously retarded.
Ends up that I *wasn't* setting up the font, and that led to the 'mysterious' error.
I have two functions: 'font_setup' and 'font_cleanup', which update and cleanup the fonts according to simple global variables. I execute each of these per drawing function that deals with text. Ends up I wasn't putting them inside of those functions, thus throwing the output off. Oops.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!