PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

trouble changing the font

I need help changing the font. Any examples, suggestions, and advice would be appreciated.
Here is the code I'm trying to use to change the font.
Code:

LOGFONT lFont;
HFONT font;
HFONT prevFont;
HDC hdc;
...
memset(&lFont;, 0, sizeof(LOGFONT)); // fill with 0
lFont.lfHeight = 8; // Request a 8-pixel-height font.
lFont.lfWeight = FW_BOLD;
lstrcpy(lFont.lfFaceName, TEXT("Tahoma"));
font = CreateFontIndirect(&lFont;); // create logical font
hdc = BeginPaint(hWnd, &ps;);
prevFont = (HFONT)SelectObject(hdc, &font;);//**problem**
if (prevFont == NULL)
_ltoa(GetLastError(), buffer, 10); // LastError = 6
....
DrawText(hdc, cstr, cstr.GetLength(), &rt;, DT_LEFT);
EndPaint(hWnd, &ps;);
....

SelectObject is returning a NULL value, & GetLastError() returns 6 (The handle is Invalid)
Here's a wild guess...Is CreateFontIndirect failing?
You don't pass pointers to handles, you just pass the handle. Here is how line 12 should now read, in addition to the correct code to select the old font back in when you're done with the DC.

Code:

prevFont = (HFONT)SelectObject(hdc, font);//**problem solved**

//Do some stuff

//Clean up:
SelectObject(hdc, prevFont);

Dan East
Thanks for the help Dan, changing fonts is a breaze!