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]
Calculating String Dimensions in WindowsGDI (SOLVED, due to retardation)
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:
I think this answers your question : GetCharWidth32. I use it in my GraphicsManager like follows:
HTH,
Jim.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement