Hi all,
I am currently struggling to figure out how to determine how wide a string of text is on the screen, particularly when I change resolutions in my Win32 application.
The reason I want to determine how wide a string will be, particularly between resolution changes, is that I want the strings to be of the same length relative to the size of the screen, regardless of what resolution the user picks.
Note: Using C++ MSVC 6.0 and Win32 application.
Here's how I create the font handle (I do this once):
m_FontHandle = LoadResourceFont(&m_hFont,
(LPCTSTR)(MAKEINTRESOURCE(IDR_RT_FONT_1)),
"RT_FONT", "Font 1",
-MulDiv(32, GetDeviceCaps(hdc, LOGPIXELSY), 72));
Here is how I try to determine the width (in pixels) of the text that will be displayed:
"Calculate Width":
float GetTextWidth( const std::string& text )
{
HDC hdc = GetDC(NULL);
SelectObject(hdc, &m_hFont);
RECT screenRect;
ZeroMemory(&screenRect, sizeof(RECT));
GetClientRect(m_g->GetHWND(), &screenRect);
UINT screenWidth = screenRect.right - screenRect.left;
RECT rect;
DrawText(hdc, text.c_str(), text.length(), &rect, DT_CALCRECT);
// float width = ????
.
.
.
}
I've tried several different ways to calculate the width, to no success. When I count the pixels wide for the text, it never came up to be the same as the formula calculation I did in my code.
Has any one tried this before? Any help would be much appreciated!
roger_hq