Public Group

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

## Recommended Posts

Hi, I'm wondering if it is possible to load and use a font from a resource, using C++ and windows. I've implemented the following to load the font into a resource...
in Resource.h

#define IDF_LCDFONT	3000


in Resource.rc

IDF_LCDFONT		FONT		"Res\\DS-DIGI.ttf"


This all seems to compile ok, but I'm not sure how I can then use this font, I tried the following but to no avail!
g_hLCDFont = CreateFont(200,0,0,0,0,0,0,0,0,0,0,0,0,(LPCSTR)IDF_LCDFONT);


Any help would be much appreciated

##### Share on other sites
Maybe this helps a bit

##### Share on other sites
The system doesn't just magically know you have a font in a resource that you may or may not want to use. You have to tell it. You need to get a pointer to the resource bits using Find/Load/LockResource and then call AddFontMemResourceEx. Once you've done that then specify the font name (not your resource id - why would you think that would do anything?) and you should be ok.

Also, you shouldn't just slap zeros in your CreateFont call like that. In particular 0 for the charset means ANSI_CHARSET. You almost always should use DEFAULT_CHARSET.

##### Share on other sites
Hey, thanks very much for you replies sorry that it has taken me so long to reply to this thread but I've been extremely busy at work and my coding had to put on hold for a while. I've looked at both that link and what you said Anon Mike and this is the code that I have tried...

HMODULE hModule;DWORD Count =0;GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,NULL, &hModule);HRSRC Resource = FindResource(hModule, MAKEINTRESOURCE(IDF_LCDFONT),RT_FONT);HGLOBAL Address = LoadResource(hModule, Resource);LockResource(Address);DWORD Length = SizeofResource(hModule, Resource);HANDLE Handle = AddFontMemResourceEx(Address,Length,0,&Count);g_hLCDFont = CreateFont(200,0,0,0,0,0,0,0,DEFAULT_CHARSET,0,0,0,0,"DS-Digital");

However this doesn't seem to work and I've tried researching else where but am still unable to solve the problem, any help would be much appreciated.

##### Share on other sites
1. You're not checking for errors anywhere
2. hModule is a local variable on the stack, not something that lives in your module. GetModuleHandleEx is very likely setting hModule to NULL.
3. I would be a bit dubious about using RT_FONT. That's a holdover from the days of bitmap fonts. It may be allright, I just don't know. I use a custom resource type so I can be sure the resource compiler isn't messing with my data.
4. LoadResource doesn't return an address, it returns a handle. LockResource returns an address. I'm sure you've read that for resources it's the same thing but it's generally better to do things in the proper way. Especially in this case where there's no good reason not to.

I was able to get a sample app to work after fixing the stuff above.

1. 1
2. 2
frob
16
3. 3
Rutin
12
4. 4
5. 5

• 13
• 12
• 59
• 14
• 15
• ### Forum Statistics

• Total Topics
632124
• Total Posts
3004243

×