changing edit control font

This is a discussion on changing edit control font within the Windows Programming forums, part of the Platform Specific Boards category; the win32 programmers reference is confusing on the topic of changing fonts. all i want is to change the font ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    changing edit control font

    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?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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.
    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);
    where hEdit is the handle of your edit control and hFont is the handle of your created font (HFONT).

    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.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    thanks, it worked. except i discovered i didn't really like courier new at all. oh well
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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:

    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);
    what IS the problem? does GetTextExtentPoint32() not take fonts into account? previously, when using the default font, it worked fine. help!
    Last edited by bennyandthejets; 12-20-2002 at 08:33 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>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:
    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;
    }
    Hope that is of some use to you.

    edit: changed fn so it no longer returns temp
    Last edited by Ken Fitlike; 12-29-2002 at 07:20 PM.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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:

    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);
    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.

    well, i sure have rambled a bit, but i think im on the right track. any feedback?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. line number on a rich edit control
    By rakan in forum Windows Programming
    Replies: 1
    Last Post: 02-18-2008, 07:58 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  3. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 10:52 PM
  4. Controlling an edit control
    By master5001 in forum Windows Programming
    Replies: 2
    Last Post: 10-16-2001, 04:08 PM
  5. Rich edit control example Win API
    By Echidna in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2001, 03:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21