CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2005
    Posts
    12

    Current window font ?

    How do I get some window(control actually) font ?
    I tried to do this by using:

    HDC hdc1 = 0;
    HGDIOBJ hgdio1 = 0;
    LOGFONT fontInfo1 = {0};
    int result1 = 0;

    hdc1 = GetDC( (HWND)0xA0217 );
    hgdio1 = GetCurrentObject( hdc1, OBJ_FONT );
    result1 = GetObject( hgdio1, sizeof(LOGFONT), &fontInfo1 );

    The fact is that this code doesn't retrieve CURRENT window font - it just returns "system" font. It returns always the same - doesn't matter that I changed font for this window - and I see that changed font now.

    How do I retrieve current window font ?

    thanks
    Vilius

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Current window font ?

    The "window font" is chaged by sending WM_SETFONT message. This is usually sent to a control (button, edit, etc) in order to tell which font to use.
    In you want to retrieve it, then you have to send WM_GETFONT.

    GetCurrentObject(hDC, OBJ_FONT) retrieves the font currently selected in a device context, which is a different stuff. If no font was previously selected in DC by a call of SelectObject, then fially you'll get the system font.
    Last edited by ovidiucucu; March 28th, 2011 at 01:54 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    May 2005
    Posts
    12

    Re: Current window font ?

    Hi,

    Ok, so now I tried:

    LRESULT r1 = 0;
    r1 = SendMessage( (HWND)0x1502A6, WM_GETFONT, 0, 0 );

    r1 is allways 0. Documentation for WM_GETFONT states that it could be 0 if system font is used. I took notepad.exe as example and selected some odd font - definitely not system font - result still 0.
    I retrieved window handle using spy++ - and it's valid.
    I try to get other process window font - could it be the problem ?

    V

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Current window font ?

    Most possible, you have used the handle of Notepad's main window.
    In fact, the text editor is its child window, of class "Edit".
    See this article: Controlling Notepad From C++ Applications
    Last edited by ovidiucucu; March 28th, 2011 at 02:35 AM. Reason: typo
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    May 2005
    Posts
    12

    Re: Current window font ?

    No I used edit controls window handle.
    Anyway tried other windows of other programs - the same result.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Current window font ?

    Well, I have run the following test, both under Windows XP and Windows 7, and got the correct font face name.
    Code:
    #include <windows.h>
    #include <TCHAR.h>
    #include <stdio.h>
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
       HWND hWnd = ::FindWindow(_T("Notepad"), NULL);
       if(NULL == hWnd)
       {
          ::MessageBox(NULL, _T("Notepad not found"), _T("Test"), MB_OK);
          return 0;
       }
       hWnd = ::GetWindow(hWnd, GW_CHILD);
       if(NULL ==  hWnd)
       {
          ::MessageBox(NULL, _T("Child EDIT not found"), _T("Test"), MB_OK);
          return 0;
       }
       HFONT hFont = (HFONT)::SendMessage(hWnd, WM_GETFONT, 0, 0);
       if(NULL == hFont)
       {
          ::MessageBox(NULL, _T("WM_GETFONT returns NULL"), _T("Test"), MB_OK);
          return 0;
       }
    
       LOGFONT lf = {0};
       TCHAR pszMsg[128] = {0};
    
       BOOL bRet = ::GetObject(hFont, sizeof(LOGFONT), &lf);
       if(! bRet)
       {
          _stprintf(pszMsg, _T("GetObject failed. Error: %u"), ::GetLastError());
          ::MessageBox(NULL, pszMsg, _T("Test"), MB_OK);
          return 0;
       }
    
       _stprintf(pszMsg, _T("OK. Font face name is: '%s'"), lf.lfFaceName);
       ::MessageBox(NULL, pszMsg, _T("Test"), MB_OK);
    
       return 0;
    }
    Something else you are doing wrong.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    May 2005
    Posts
    12

    Re: Current window font ?

    Hello,

    Well I figured it out - SendMessage returns 0 only when I use remote debugging.
    I tried to launch program on local boxes(XP, win7) - no problem works as it should.

    Any ideas why this happening - always thought that remote debbuging is 100% the same as local launch..

    Vilius

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Current window font ?

    Quote Originally Posted by ovidiucucu View Post
    Well, I have run the following test, both under Windows XP and Windows 7, and got the correct font face name.
    Hi Ovidiu,
    I am having hard time trying to understand how it works. I always thought that GDI object handles from one process have no meaning in another - was I wrong? Or are HFONT objects excluded? What if you create a font in one process and try to access it from another?
    Thank you!
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured