Tech Support > Microsoft Windows > Development Resources > problem setting font size on a subclassed edit control
problem setting font size on a subclassed edit control
Posted by Jack on April 7th, 2008


Hi,

I have subclassed an edit control so that I can control it's colouring - in
particular when it is disabled - copied below.

I perform the colouring and text output (DrawText() ) in an overridden
WM_PAINT.

The colours work fine, however I can't get the text to be the same size as
the text in other edit controls on the dialog which are default size, it is
slightly bigger.

I have tried to GetDC for the dialog and create a font from that but the
textmetrics returned are of the slightly bigger size.

I have tried to see what happens if I use the dc from certain windows
messages that send a dc eg:

WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC but they are not received by the
subclass.

All I seem to receive are WM_PAINT and WM_ERASEBACKGROUND and WM_DESTROY -
the control properties is set to disabled state.

I can set the font successfully in the WM_PAINT for the control with a
hard-coded font size, but I want to automatically have the font sizes equal.

TIA for any ideas.



// ClrCtlProc: this is the subclass procedure for colouring the edit control

LRESULT CALLBACK ClrCtlProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)

{ HGLOBAL hglob = (HGLOBAL)GetProp(hwnd,_T("clrref_dat"));

if (hglob==NULL)

return DefWindowProc(hwnd,msg,wParam,lParam);

TClrData *oud=(TClrData*)GlobalLock(hglob); TClrData ud=*oud;

GlobalUnlock(hglob); // I made a copy of the structure just so I could
GlobalUnlock it now, to be more local in my code

switch (msg)

{ case WM_DESTROY:

{ RemoveProp(hwnd,_T("clrref_dat")); GlobalFree(hglob);

if (ud.url!=0) delete[] ud.url;

// nb. remember that ud.url is just a pointer to a memory block. It might
look weird

// for us to delete ud.url instead of oud->url, but they're both equivalent.

if(ud.hb)

DeleteObject(ud.hb);

} break;

case WM_ERASEBKGND:

return (LRESULT)1; // Say we handled it.





case WM_PAINT:

{

if(IsWindowEnabled(hwnd))//use default processing

break;

PAINTSTRUCT ps;

HDC dc=BeginPaint(hwnd, &ps);

// Paint(hWnd, &ps);

// HFONT hFont = gMenuBarFontStandardNumbers;


// Erase the background.

//

RECT rc;

GetClientRect(hwnd, &rc );



FillRect(dc, &rc, ud.hb);

SetBkMode(dc, TRANSPARENT);

DrawText(dc,ud.url, -1, &rc, DT_SINGLELINE|DT_CENTER|DT_VCENTER);

//Restore the old font

// SelectObject(dc, hOldFont); // deselect hF (if selected)

EndPaint(hwnd, &ps);

}

break;



}

return CallWindowProc(ud.oldproc,hwnd,msg,wParam,lParam);

}





Posted by Christian ASTOR on April 9th, 2008


Jack wrote:

The default font for Edit controls is the System font.
(If I draw a text with GetStockObject(SYSTEM_FONT) or WM_GETFONT, I get
the same size than for standard Edit controls (at least on XP))


Similar Posts