I embeded font "ABC VN Font" into resource file(font.rc)

IDR_MYFONT FONT DISCARDABLE "abc.fon"

How to load it from font.res file into memory at runtime and change font dialog boxes to "ABC VN Font"? Please help me! Can you give me the MASM32 source-code to do it?

Thanks!
Posted on 2002-02-15 20:54:26 by vkdt
i was just looking for that too..


sorry for that late answer :tongue:
AddFontResource
RemoveFontResource

bye
Posted on 2002-07-24 10:16:45 by NOP-erator
ooooooppsss... sorry..

i experimented a bit with this APIs, and obviously this is NOT the correct API! :eek:

can anybody help? here's the problem again: i've added a font to my resource file:



5000 FONT "comix.ttf"


now i want to use this font in my application, but i don't know how. if you have a bitmap or something you just use LoadBitmap to load it from the resource file, but there's nothing for fonts! and obviously i need to do something so that i can use it with CreateFont to choose the font.. right? :confused:

bye
Posted on 2002-07-25 05:36:18 by NOP-erator
Afternoon, vkdt & NOP-erator.

If the font isn't in the same directory as the .rc file, you have to give the complete filepath.
i.e.
#define     IDF_FONT        43

IDF_FONT FONT "C:\WINDOWS\FONTS\COMIC.TTF"


Cheers,
Scronty
Posted on 2002-07-25 05:48:50 by Scronty
hi scronty,

thanks, but that wasn't the problem. the problem was: how to use the font that is compiled in the res file?
now i found a "solution", actually it was pretty easy. i just load the font from resource and write the resource data to a file. then i can call the AddFontResource API:



.data
fontname db "Comix Regular Caps",0 ;font face name
fgfont db "mf.ttf",0 ;font file name

.data?
fontreshandle dd ? ;font resource handle
fontresptr dd ? ;font data pointer
fontressize dd ? ;size of font resource

.code
;on WM_CREATE:
invoke FindResource,hInstance,5000,RT_FONT ;5000 = font resource identifier
mov fontreshandle,eax
invoke LoadResource,hInstance,eax
mov fontresptr,eax
invoke SizeofResource,hInstance,fontreshandle
mov fontressize,eax
invoke LockResource,fontresptr
mov fontresptr,eax

;create the font file
invoke CreateFile,ADDR fgfont,GENERIC_READ or GENERIC_WRITE ,\ ;fgfont = filename
FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,\
NULL
mov fontfile,eax ; handle to font file
invoke WriteFile,fontfile,fontresptr,fontressize,addr bwritten,0 ;font data is written to file
invoke CloseHandle,fontfile ;handle to file is closed

;the font file is in the apps directory now, we have to "register" the font
invoke AddFontResource,addr fgfont

on WM_DESTROY:
invoke RemoveFontResource,addr fgfont ;"unregister" the font
invoke DeleteFile,addr fgfont ;delete the font file



the resource file:


5000 FONT "C:\windows\fonts\blabla.ttf"


this was tested on win98se and winXP, both works! :alright:

bye
Posted on 2002-07-25 07:10:51 by NOP-erator
Afternoon, NOP-erator.

heh. That's the solution I found as well :grin: :alright: .

For completion, for anyone else reading this thread (i.e. vkdt ;) ), I'll just add that, when using CreateFont, remember that it's the typeface name you give; not the file name. <gee...there's a friggin' lot of commas in that sentence>

i.e.


.data
...
szTypefaceName db "Comic Sans MS",0 ; the *typeface* name. NOT the filename
...
.code
...
; (what NOP-erator gave above ^^^^)
...
invoke CreateFont, 18, 0, 0, 0, FW_NORMAL, FALSE, FALSE, \
FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,\
ANTIALIASED_QUALITY, VARIABLE_PITCH, ADDR szTypefaceName
mov hFONT, eax
...
; create some controls
...
invoke PostMessage, hEdit, WM_SETFONT, hFONT, TRUE
invoke PostMessage, hListView, WM_SETFONT, hFONT, TRUE
...
[/code

Cheers,
Scronty
Posted on 2002-07-25 10:19:22 by Scronty