Click to See Complete Forum and Search --> : GetObject problem
Vintik
July 20th, 2006, 02:18 PM
Hello people,
I have some problems with GetObject(). It always returns zero (error), so I do not know how to change the font (to make it underline).
Look plz
HFONT hFont = (HFONT) SendMessage(hWnd, WM_GETFONT, 0, 0);
LOGFONT lf;
GetObject(hFont, sizeof(lf), &lf);
lf.lfUnderline = TRUE;
HFONT hNewFont = CreateFontIndirect(&lf);
SendMessage(hWnd, WM_SETFONT, (WPARAM) NewFont, FALSE);
Brenton S.
July 20th, 2006, 03:12 PM
Hi Vintik,
Firstly, make sure hFont is not equal to NULL. If hFont is NULL, GetObject might fail. WM_GETFONT returns a handle to the font used by the window. If the window is using the system font, WM_GETFONT returns NULL.
Vintik
July 20th, 2006, 04:42 PM
Hi Vintik,
Firstly, make sure hFont is not equal to NULL. If hFont is NULL, GetObject might fail. WM_GETFONT returns a handle to the font used by the window. If the window is using the system font, WM_GETFONT returns NULL.
So... I do not know if the window is using the system font, but this window is static control
hStatic = CreateWindow("STATIC",
"Link",
WS_CHILD | WS_VISIBLE | SS_SIMPLE ,
10, 250, 70, 20,
hMainWnd,
(HMENU) NULL,
(HINSTANCE) GetWindowLong(hMainWnd, GWL_HINSTANCE),
NULL);
How can I know about if this is the system font? And how can I change the font of static control such like that?
P. S. hFont is not NULL.
golanshahar
July 21st, 2006, 02:04 AM
Why do you care if the control has system font or not, If you want to change it anyway? Simply change it to what you want.
Look at:
::CreateFontIndirect() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_4rw4.asp)
::SendMessage() with WM_SETFONT.
This FAQ (http://www.codeguru.com/forum/showthread.php?t=321408) might also help you.
Cheers
Vintik
July 21st, 2006, 02:34 AM
Why do you care if the control has system font or not, If you want to change it anyway? Simply change it to what you want.
Look at:
::CreateFontIndirect() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_4rw4.asp)
::SendMessage() with WM_SETFONT.
This FAQ (http://www.codeguru.com/forum/showthread.php?t=321408) might also help you.
Cheers
Look at the beginning of the thread. I use that, but problem is in GetObject().
humptydumpty
July 21st, 2006, 03:18 AM
Have a Look on this Link
http://www.codeguru.com/forum/showthread.php?t=379565
Edit :-
and
http://www.codeguru.com/cpp/controls/buttonctrl/article.php/c2065/
or you can try
HFONT hFont = SendMessage (hWnd, WM_GETFONT, 0, 0L);
LOGFONT lf;
if (!GetObject(hFont , sizeof(LOGFONT), &lf))
error;
or
HFONT f = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
LOGFONT lf;
int iRes = GetObject(f, sizeof(LOGFONT), &lf);
Thanx
Viorel
July 21st, 2006, 03:19 AM
As was suggested by Brenton S. before, the GetObject function probably fails because of the passed null font. Can you confirm that the hFont value returned by WM_GETFONT is not NULL?
If it is NULL, I think you can first get a predefined font with GetStockObject function. Like this:
HFONT hFont = (HFONT)GetStockObject(SYSTEM_FONT);
Then use your code for adjusting and assigning this font. Instead of SYSTEM_FONT, you can also use other parameters related to fonts (see the Documentation).
I hope this helps.
golanshahar
July 21st, 2006, 04:11 AM
Look at the beginning of the thread. I use that, but problem is in GetObject().
You didn’t get my point. I know you used that, but I don’t understand why you need the ::GetObject() at all? Simply create the font you want use in your application and then set it to your controls.
Cheers
Vintik
July 21st, 2006, 05:51 AM
Thank you all very much! I'll try.
You didn’t get my point. I know you used that, but I don’t understand why you need the ::GetObject() at all? Simply create the font you want use in your application and then set it to your controls.
Cheers
Sorry golanshahar, so... thank you for your help.
golanshahar
July 21st, 2006, 06:25 AM
Sorry golanshahar, so... thank you for your help.
Its ok, no need to be sorry :wave:
Cheers
Vintik
July 21st, 2006, 06:46 AM
Thank you all! It works OK now.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.