-
March 28th, 2011, 12:25 AM
#1
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
-
March 28th, 2011, 01:43 AM
#2
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.
-
March 28th, 2011, 02:00 AM
#3
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
-
March 28th, 2011, 02:30 AM
#4
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
-
March 28th, 2011, 02:35 AM
#5
Re: Current window font ?
No I used edit controls window handle.
Anyway tried other windows of other programs - the same result.
-
March 28th, 2011, 05:26 AM
#6
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.
-
March 29th, 2011, 05:33 PM
#7
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
-
March 31st, 2011, 04:46 PM
#8
Re: Current window font ?
Originally Posted by ovidiucucu
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|