Jump to content
  • Advertisement
Sign in to follow this  
cppcdr

Cannot change fonts when using CreateFont()

This topic is 3968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i have been trying to use different fonts in my program and all i am getting is the same one all the time. Here is the code: if((m_hFont = CreateFont(-12, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE|DEFAULT_PITCH, LPCWSTR("Comic Sans MS")))==NULL) return false; I can change the size, the weight, italics, everything except the font type... for some reason it doesn't change even if i use Times New Roman, Courrier New, Arial, etc. Any ideas? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Have you messed around with the last parameter at all???

i.e. trying "fontname" or L"fontname" without the LPCWSTR()?

Your code looks similar to some code I wrote last summer. Also make sure to call SelectObject and pass in the device context and your font handle.

You should also post how you're rendering your fonts/creating the bitmaps if this is game related?

Share this post


Link to post
Share on other sites
The above poster is correct. The cast to LPCWSTR is dangerous, because it's not converting the string, just the pointer to the char array (i.e it's just converting a char* to a wchar_t*, will will result in garbage when trying to read the wchar_t* as a string)

Share this post


Link to post
Share on other sites
My guess is that CreateFont() is either failing or not finding the font you passed in, for the reason Sc4Freak said. You can't cast a string like that, you should use _T("Comic Sans MS") to handle the conversion to unicode if needed.

Share this post


Link to post
Share on other sites
I had tried "fontname" and it would not work because it was the wrong type, so i looked on the net and they said to use LPCWSTR. I had not tried _T("fontname") and when i did, it worked :)

Thanks everyone for your help!

Share this post


Link to post
Share on other sites
Quote:
Original post by cppcdr
I had tried "fontname" and it would not work because it was the wrong type, so i looked on the net and they said to use LPCWSTR. I had not tried _T("fontname") and when i did, it worked :)

Thanks everyone for your help!
A bit of an explanation of why it works:
"fontname" is a string of narrow (8-bit usually) characters.
L"fontname" is a string of wide (16-bit usually) characters.

When UNICODE is defined, LPCTSTR is defined to const wchar_t*. The _T() macro takes a constant string like "fontname" as a parameter, and prefixes the L if UNICODE is defined, so it can be passed to a function that takes a LPCTSTR.

Alternatively, use CreateFontA or CreateFontW to explicitly use narrow or wide strings.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!