Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Access font on edit control

30 views
Skip to first unread message

Paul N

unread,
Jul 23, 2012, 5:05:00 PM7/23/12
to
I was just wondering if there was a neater way to do this? Obviously
it's not essential as I already have a way of doing it, but it seems
unduly messy. Consider:

case WM_CREATE:

editbox = CreateWindow(_TEXT("edit"), NULL,
WS_CHILDWINDOW | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
20, 60,
400,
24,
hWnd, (HMENU)4,
hInst, NULL);

SendMessage(editbox, EM_LIMITTEXT, MAXBUFF, 0);
SendMessage(editbox, WM_SETFONT, (WPARAM) font, FALSE);
oldeditproc = MySetWindowFunction(editbox, EditWndProc);

{
TEXTMETRIC tm;

HDC hdc = GetDC(hWnd);
SaveDC(hdc);
SelectObject(hdc, font);
GetTextMetrics(hdc, &tm);
halfwidth = tm.tmMaxCharWidth / 2;
RestoreDC(hdc, -1);
ReleaseDC(hWnd, hdc); }

I create an edit box and set a font for it. I want to find out a
parameter of the font. Even though the edit box window function is
subclassed, I couldn't find a way to get hold of the font from it, so
I'm creating a DC on the main window and putting the same font,
temporarily, into that. Do I have to?

Thanks.
Paul.

Christian ASTOR

unread,
Jul 23, 2012, 10:13:13 PM7/23/12
to
Paul N a �crit :
> I was just wondering if there was a neater way to do this?

Maybe I did not understand, but why don't you use
WM_GETFONT - GetObject() ?

Paul N

unread,
Jul 24, 2012, 5:05:40 PM7/24/12
to
On Jul 24, 3:13 am, Christian ASTOR <casto...@club-internet.fr> wrote:
> Paul N a écrit :
>
> > I was just wondering if there was a neater way to do this?
>
> Maybe I did not understand, but why don't you use
> WM_GETFONT - GetObject() ?

Hi Christian

I could use WM_GETFONT to get the font, but as I already have a handle
to the font this seems a bit unnecessary.

What I'm wanting is to get a parameter of the font, namely the maximum
character width. It seems that the only way to do this is with
GetTextMetrics. This in turn needs a DC set up with the font. And,
although the edit box can presumably produce such a DC when it is
displaying, I can't see a way of getting hold of it (even with
subclassing). This is why I am setting up my own DC and selecting the
font into it.

Is there an easier way of getting the font width?

Thanks.
Paul.

David Lowndes

unread,
Jul 24, 2012, 6:43:33 PM7/24/12
to
>I was just wondering if there was a neater way to do this?

None that I'm aware of.

The only difference I've done in similar code is to restore the
original font rather than use Save/ResoreDC.

Dave

Gunnar

unread,
Jul 24, 2012, 6:45:27 PM7/24/12
to
Hi Paul,

> Is there an easier way of getting the font width?


I'm afraid not. The actual text width depends on the settings (mapping mode
and so on) of the DC you select the font into - therefore the same font may
give different results for different DCs, that's why you have to specify the
DC you want to use.
The Windows API provides a thing called IC (Information Context) for
operations like yours when you don't need to draw anything - have a look at
the CreateIC() API function.

-Gunnar

Christian ASTOR

unread,
Jul 25, 2012, 12:27:28 AM7/25/12
to
Paul N a �crit :

> What I'm wanting is to get a parameter of the font, namely the maximum
> character width. It seems that the only way to do this is with
> GetTextMetrics. This in turn needs a DC set up with the font. And,
> although the edit box can presumably produce such a DC when it is
> displaying, I can't see a way of getting hold of it (even with
> subclassing). This is why I am setting up my own DC and selecting the
> font into it.

Ok, but I don't understand why you cannot call GetTextMetrics() inside
the Edit procedure.
I made a quick test and it works normally.

Paul N

unread,
Jul 25, 2012, 4:57:43 PM7/25/12
to
On Jul 25, 5:27 am, Christian ASTOR <casto...@club-internet.fr> wrote:
> Paul N a écrit :
>
Could you show me how you are getting the DC to use in GetTextMetrics,
please?

As an alternative approach, I've just tried using GetObject, which
gives some properties of the font, but this was not a success. I'd
been lazy when I created the font - I'd specified lfHeight but not
lfWidth - and GetObject only gives me back the values I put in.

Thanks.
Paul.

Christian ASTOR

unread,
Jul 26, 2012, 4:07:17 AM7/26/12
to
Paul N a �crit :

> Could you show me how you are getting the DC to use in GetTextMetrics,
> please?

Nothing special, just GetDC(), and I get same results than calling it on
main window =>


In Main window WM_CREATE :

LOGFONT lf = { 0 };
SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &lf,
FALSE);
hFont = CreateFontIndirect(&lf);
SendMessage(hEdit, WM_SETFONT, (WPARAM) hFont, FALSE);

OldEditProc = SetWindowLong(hEdit, GWL_WNDPROC,(LONG)(WNDPROC)NewEditProc);
PostMessage(hEdit, WM_USER, (WPARAM) hFont, FALSE);



in NewEditProc :

case WM_USER:
{
TEXTMETRIC tm;
HDC hdc = GetDC(hWnd);
HFONT hFontOld = (HFONT)SelectObject(hdc, hFont);
GetTextMetrics(hdc, &tm);
int halfwidth = tm.tmMaxCharWidth / 2;
SelectObject(hdc, hFontOld);
ReleaseDC(hWnd, hdc);
}
break;

Paul N

unread,
Jul 26, 2012, 4:32:43 PM7/26/12
to
Thanks Christian. I thought you had some cunning way to avoid using
GetDC, but it looks like this is essential (as Gunnar has said).

I presume that selecting the old font back again, as you do, is
quicker than using SaveDC and RestoreDC, as I was doing, so my code
has at least improved in that aspect. Thanks for the help.
0 new messages