Sign in to follow this  
johnnyBravo

c++ win32, how to set DrawText font?

Recommended Posts

johnnyBravo    100
Hi, im drawing text onto the window with DrawText. I want to change the font of the text. I've tried: HFONT font = CreateFont(18, 0, 0, 0, 300, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial"); SendMessage(handle, WM_SETFONT, (WPARAM)font, TRUE); which doesn't do anything. Any ideas? Thanks.

Share this post


Link to post
Share on other sites
Endurion    5411
You have to use SelectObject to select the HFONT into the HDC you use with DrawText. You should also store the original HFONT and select it back in after you're done:


// drawing code

// select the font in
HFONT hFontOld = (HFONT)SelectObject( hDC, font );

DrawText( hDC, ... );

// put back the old font
SelectObject( hDC, hFontOld );

Share this post


Link to post
Share on other sites
Riekistyx    100
Agreed... once critical thing to always remember about GDI elements....

just like pointers new and delete...

if you call create You must call destroy.

IE:

HFONT font = CreateFont(18, 0, 0, 0, 300, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial");

then.... when your program ends
same with HDCS.... but if you use a buffer its either DestroyDC() or ReleaseDC() i believe.

DestroyObject(font);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this