Sign in to follow this  

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

This topic is 4710 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
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

This topic is 4710 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.

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