the win32 programmers reference is confusing on the topic of changing fonts. all i want is to change the font of a single line edit box to Courier New. is there a simple way to do this?
the win32 programmers reference is confusing on the topic of changing fonts. all i want is to change the font of a single line edit box to Courier New. is there a simple way to do this?
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
You have to create a font using, for example, CreateFont which returns a handle that you send to the target window in a WM_SETFONT message. Since the font handle is a gdi object, use DeleteObject on it when you are done. eg.where hEdit is the handle of your edit control and hFont is the handle of your created font (HFONT).Code:hFont=CreateFont(0,12,0,0,0,0,0,0,0,0,0,0,0,TEXT("Courier New")); SendMessage(hEdit,WM_SETFONT,(WPARAM)hFont,0);
I just zeroed most of the parameters of the CreateFont function which works ok for 'simplicity' but you should check out the full parameter list for the function on msdn.
Hope that helps.
thanks, it worked. except i discovered i didn't really like courier new at all. oh well
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
now i have another problem. GetTextExtentPoint32() returns the same values as before, even though the height and width of the device font has changed. ill post a bit of code:
what IS the problem? does GetTextExtentPoint32() not take fonts into account? previously, when using the default font, it worked fine. help!Code:hwndEdit=CreateWindowEx(0,"EDIT","00000000",...) hFont=CreateFont(0,12,...,TEXT("Courier New")); SendMessage(hwndEdit,WM_SETFONT,(WPARAM)hFont,0); GetTextExtentPoint32(GetDC(hwndEdit),"00000000",8,&tmpSize);
Last edited by bennyandthejets; 12-20-2002 at 08:33 PM.
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
>>what IS the problem?<<
Don't quote me on this, but the default wndproc for the edit probably selects/deselects its font into the control dc during WM_PAINT handling and so is only available either then or, at any time, via WM_GETFONT.
Try this:Hope that is of some use to you.Code:SIZE& GetTextSize(HWND hwnd,TCHAR *chTxt) { /*retrieve font for hwnd and get dimensions of chTxt using that font.*/ HDC hdc; HFONT hFont,hOldFont; static SIZE st; hdc=CreateCompatibleDC(0); hFont=(HFONT)SendMessage(hwnd,WM_GETFONT,0,0); hOldFont=(HFONT)SelectObject(hdc,hFont); GetTextExtentPoint32(hdc,chTxt,lstrlen(chTxt),&st); /*clean up*/ SelectObject(hdc,hOldFont); DeleteDC(hdc); return st; }
edit: changed fn so it no longer returns temp
Last edited by Ken Fitlike; 12-29-2002 at 07:20 PM.
you know ken, you're a real smart guy. but tell me, what is the difference between the device context for the edit control and the one we made?
---a little later---
i think i worked it out. the device context is not as such a permanent object, it is more like a temporary key that CAN (but doesn't have to) contain information about a specific window. what happens is, you create a temporary hdc, and specify that it uses the HFONT you made. then you give it a string to calculate with, and delete the hdc afterwards, because it serves no more purpose. alternatively, you could do this:
in this case, the hdc DOES actually refer to a specific window, but for some reason it doesn't yet know that hFont is the relevant font. so, you SelectObject the font into it, and calculate the string width. afterwards, you can delete the hdc because it is simply info about a window that is also stored elsewhere.Code:hFont=CreateFont(0,12,0,0,FW_NORMAL,0,0,0,0,0,0,0,0,TEXT("Courier New")); SendMessage(hwndEdit,WM_SETFONT,(WPARAM)hFont,0); hdc=GetDC(hwndEdit); SelectObject(hdc,hFont); GetTextExtentPoint32(hdc,"00000000",8,&tmpSize); DeleteDC(hdc);
well, i sure have rambled a bit, but i think im on the right track. any feedback?
[email protected]
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro
Code Tags
Programming FAQ
Tutorials
>>any feedback?<<
I recall reading somewhere or other that a control uses its parent's dc, unless otherwise specified. That's why I suspected that the default wndproc for the control (in your case an edit) selects the font into the dc (as you have done) draws any text using it and then restores the dc. That's probably why the font can't be used for dc specific manipulations unless it's actually selected into the dc.
BTW, if you use GetDC, you should not delete it, use ReleaseDC instead.