Sign in to follow this  
Slyfox

Loading a font from a resource

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 this post


Link to post
Share on other sites
Maybe this helps a bit

http://www.eggheadcafe.com/conversation.aspx?messageid=30018713&threadid=30018713


http://www.eggheadcafe.com/conversation.aspx?messageid=30018716&threadid=30018713

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

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