ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  C++/MFC Fonts from a resource-DLL?
Posted by Kiro on Jan-16-2022 21:41
Dear Peter,

in a C++/MFC project with ChartDirector 7, I load some fonts from a resource-DLL as shown here:

TTF m_TtfRobotoCondReg;
HINSTANCE m_hInstResDLL = static_cast<CErgoApp*>(AfxGetApp())->m_hInstResDLL;
HRSRC res = FindResource(m_hInstResDLL, MAKEINTRESOURCE(IDR_ROBOTOCONDENSEDREG), L"BINARY");
PVOID lpFont = LockResource(LoadResource(m_hInstResDLL, res));
DWORD dwSize = SizeofResource(m_hInstResDLL, res); //cFonts = 0;
m_TtfRobotoCondReg.Parse(static_cast<BYTE*>(lpFont), dwSize);

After that, I can use the fonts by their generic name also in CFont::CreateFont() or by CreatePointFontIndirect().

My question: How can I use such temporary loaded fonts in TextBox.setFontStyle()?

Any help is appreciated!
Kai

  Re: C++/MFC Fonts from a resource-DLL?
Posted by Peter Kwan on Jan-18-2022 01:46
Attachments:
Hi Kiro,

You can use font from resources in ChartDirector. Also, you can use font in memory in ChartDirector. That means if you can load a font into memory, it can be used with ChartDirector.

To load a font from resources directly, you can use the resource path syntax as the font name:

https://www.advsofteng.com/doc/cdcpp.htm#pathspec.htm

For example, if the font is embedded as a resource into the executable as in the attached image, you can use "@/TTF/MyFont" as the font name.

If the font resource is not in the executable but is in a separate DLL, you can use the syntax:

"@//aaa.dll/path_to/MyFont"

Note that the "aaa.dll" must be a DLL that has already been loaded into your process. If the DLL is not part of your process, you may use LoadLibraryW to load it into your process first.

If the resource does not have a name, but have a number (usually represented by a macro), you can use the number instead of MyFont.

It seems you already have the font to load the font into memory. In this case, you can register the memory as a ChartDirector resource, like:

Chart::setResource("myCustomFont", MemBlock(ptr, size));

See:

https://www.advsofteng.com/doc/cdcpp.htm#Chart.setResource.htm

You can then use "@/myCustomFont" as the font name.

Note that once you use the in memory font, you should not free the memory until the program ends. It is internally ChartDirector will cache certain font tables. When your code uses the same font, ChartDirector will fetch the font from the same memory. If the memory has already been freed, the result will be unpredictable.

Regards
Peter Kwan
mfc_font_resource.png

  Re: C++/MFC Fonts from a resource-DLL?
Posted by Kiro on Jan-23-2022 03:32
Dear Peter,

thanks a lot for your answer!
Best regards,
K.