tags:

views:

38

answers:

2

I am setting the font for a control like this:

HDC hdc = GetDC(NULL);
int lfHeight = -MulDiv(szFont, GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(NULL, hdc);
HFONT font = CreateFont(lfHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Font.c_str());
SendMessage(hwnd,WM_SETFONT,(WPARAM)font,0);

The control is a static. How would I find the width of the text in the static for a given string?

+2  A: 

Use GetTextExtentPoint32. You'll need to select the font into the DC first.

Mark Ransom
What do I pass to HDC?
Alexander Rafferty
Should I select the font into the DC I got through `GetDC(NULL);` and then also pass that DC to the function? I'll try that.
Alexander Rafferty
@Alexander, If you are doing this some time after the initialization code you show in the question, use GetWindowDC using the HWND of the control.
Mark Ransom
Ah, well I just recreated the DC and the HFONT in the width check. It works well, thankyou for the reply.
Alexander Rafferty
A: 

CDC::GetTextExtent() and CDC::GetOutputTextExtent() should help.

marc ochsenmeier
I didn't see MFC in the tags.
Mark Ransom