Jump to content
Tuts 4 You

How To Use Custom Fonts


TheCodeCracker

Recommended Posts

TheCodeCracker
Posted

Hi Guys...i am learnning masm...and have managed to learn howto skin windows, buttons as well as some other controls.

I have even managed to design some small keygen templates. but guys i need some help regarding using custom fonts both open type as well as True Types

I'll be grateful for any help

greetz,

TCC/CiB

Posted (edited)

Quickly made a radasm bone:

FontFromResources.rar

It won't save the font to hdd, but be aware, that 'AddFontMemResourceEx' doesn't work on Win98.

Edited by Ufo-Pu55y
Posted (edited)
Quickly made a radasm bone:

FontFromResources.rar

It won't save the font to hdd, but be aware, that 'AddFontMemResourceEx' doesn't work on Win98.

nice. too bad that the AddFontMemResourceEx API is not supportet for win me/98... Maybe better use AddFontResource API ??? (After using the font it shoulde be removed with RemoveFontResource?)

is there also a method for win 98/me systems? Maybe there is a method installing a font over commandline?

Edited by diablo2oo2
TheCodeCracker
Posted

Thank You UFO-Pussy....Atlast...i was expecting the answer from you and Ziggy only...

Thanks Again :D

greetz,

TCC/CiB

Posted
nice. too bad that the AddFontMemResourceEx API is not supportet for win me/98... Maybe better use AddFontResource API ??? (After using the font it shoulde be removed with RemoveFontResource?)

is there also a method for win 98/me systems? Maybe there is a method installing a font over commandline?

Here's a code I've found on MASM board, which should work for 98 (AddFontResource):

Instal_Font.zipBut you have to write it to system's font dir and register it :ermm:

Thank You UFO-Pussy....Atlast...i was expecting the answer from you and Ziggy only...

Thanks Again :D

Tho I got it, after Killboy found a c++ snippet about that ^^
Posted

May be similar to what UFO posted (not checked) but I have used the following to register a new font (not removed afterwards though)

ResExtractFile proc hInst:DWORD, Resource:DWORD, ResMarker:DWORD, Destination:DWORD
LOCAL hRsrc:DWORD
LOCAL hResource:DWORD
LOCAL dwResSize:DWORD
LOCAL hFile:DWORD
LOCAL dwWritten:DWORD
LOCAL lpfResource:DWORD
LOCAL hkResult:DWORD
LOCAL dwDisposition:DWORD
LOCAL dwLength:DWORDinvoke FindResource,hInst,Resource,ResMarker
.if eax == NULL
not eax
.else
mov hRsrc,eax
invoke LoadResource,hInst,hRsrc
.if eax == NULL
not eax
.else
mov hResource,eax
invoke SizeofResource,hInst,hRsrc
mov dwResSize,eax
invoke LockResource,hResource
mov lpfResource,eax invoke CreateFile, Destination, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL
mov hFile, eax
invoke WriteFile, hFile, lpfResource, dwResSize, ADDR dwWritten, NULL
invoke CloseHandle, hFile invoke lstrlen, addr szFileName
mov dwLength,eax
invoke RegCreateKeyEx, HKEY_LOCAL_MACHINE,
ADDR szSubKey,
NULL,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
NULL,
ADDR hkResult,
ADDR dwDisposition .if eax == ERROR_SUCCESS
invoke RegSetValueEx, hkResult, addr szValueName, NULL, REG_SZ, addr szFileName, dwLength
invoke RegCloseKey, ADDR hkResult
.endif invoke AddFontResource, addr szFileName
invoke SendMessage, HWND_BROADCAST, WM_FONTCHANGE, NULL, NULL
.endif
.endif
ret
ResExtractFile endp

The font file is added to the resources with:

FontFile BINFILE fonts\<font_name>.TTF
Posted
Tho I got it, after Killboy found a c++ snippet about that ^^

Any chance Killboy can repost this C++ code snippet? :)

Posted

That was the snippet I got:

HINSTANCE h=AfxGetInstanceHandle(); 
ULONG nFont = 1;
HRSRC hr=FindResource(h, "#4" , RT_FONT);
HGLOBAL hg=LoadResource(h,hr);
LPSTR lp=(LPSTR)LockResource(hg);
DWORD dwSize = SizeofResource(h, hr);
HANDLE handle = AddFontMemResourceEx(lp, dwSize, 0, &nFont);
// TODO: Add extra initialization here
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure.
lf.lfHeight = 20; // Request a 20-pixel-high font
strcpy(lf.lfFaceName, "LadderKK"); // with face name "Arial".
m_font.CreateFontIndirect(&lf); // Create the font.
GetDlgItem(IDC_EDIT1)->SetFont(&m_font);

Still find it easier to read compiled c++ stuff than its source :ermm:

Posted

Hehehe, for me its the exact opposite. I really should learn ASM these days, as all I use is C/C++.

Thanks for the snippet though, it will come in handy :)

TheCodeCracker
Posted

@UFO-PU55Y

bro i'm using your code snippet but itn not setting the font....

I'm not getting any errors or warnings and the font is being set to Arial.

I'm using WinASM as IDE and Masm v6. on Windows Vista Ultimate

:( (

DlgProc proc hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
mov eax,uMsg .if eax == WM_INITDIALOG
invoke LoadIcon,hInstance,200
invoke SendMessage, hWin, WM_SETICON, 1, eax
invoke FindResource,NULL,300,RT_RCDATA
mov fntinfo,eax
invoke LoadResource,NULL,eax
invoke LockResource,eax
mov pfont,eax
invoke SizeofResource,NULL,fntinfo
invoke AddFontMemResourceEx,pfont,eax,0,offset nfont
invoke CreateFontIndirect,offset lfFont
mov hfont,eax invoke GetDlgItem,hWin,1002
invoke SendMessage,eax,WM_SETFONT,offset hfont,1 .elseif eax == WM_COMMAND
mov eax,wParam
.if eax == IDB_EXIT
invoke SendMessage, hWin, WM_CLOSE, 0, 0
.endif
.elseif eax == WM_CLOSE
invoke EndDialog, hWin, 0
.endif xor eax,eax
ret
DlgProc endp
TheCodeCracker
Posted

Sorry i got my error :P

Me silly :P

Anywyas Thanks again...i've learnt alot here and hope to learn more....

greetz

TCC/CiB

Posted

Another Problem:

If you have a font file which is named not correctly. How to get the correct font name so that you can use "CreateFont..." Funktion? Because the fontname seems to be stored inside the fontfile...

Any idea?

Posted (edited)
Another Problem:

If you have a font file which is named not correctly. How to get the correct font name so that you can use "CreateFont..." Funktion? Because the fontname seems to be stored inside the fontfile...

Any idea?

You'll see the correct name by simply doubleclicking and opening with system's font viewer.

I renamed a bunch of font files with their correct font name in order to simply create them

later on with their filename w/o extension as parameter.

But if you're after a code to retrieve the correct names:

http://www.codeguru.com/cpp/g-m/gdi/fontha...le.php/c3659__1

But that would prolly mean a ****load of code, coz truetype and opentype fonts are too

different to handle :X

EDIT/

Tho I just read about an undocumented GDI API call: GetFontResourceInfo !

But no further info about what parameters it needs. I only read the comment

to fire up SoftIce to check this function out for yourself.. lol

Edited by Ufo-Pu55y
Posted (edited)

thank you ufo! i think the sourcecode getfontnamefromfile.cpp is interesting. i want build in this function additionally. because ttf files are used often. maybe i will translate it to masm.

But first i will take a look to this GDI API GetFontResourceInfo....

--> http://www.autoitscript.com/forum/index.php?showtopic=30552

...very interesting...

Edited by diablo2oo2
TheCodeCracker
Posted

Thanks UFO-PU55y and Diablo for this informative discusiion is this thread

Thanks Again :D

greetz,

TCC/CiB

Posted

I figured out how to use the GetFontResourceInfoW function. Here is the masm code, how to get the correct fontname from any fontfile...

GetFontName proc uses edi esi ebx _fontfile:dword,_buffer:dword,_buffersize:dword	LOCAL local_hlib		:DWORD
LOCAL local_fontfile_wide[1024] :BYTE
LOCAL local_sizeof_buffer :DWORD
LOCAL local_fontstruct[92] :BYTE;unknown structure
;---set return value to 0---
xor ebx,ebx invoke LoadLibrary,chr$("gdi32.dll")
mov local_hlib,eax
invoke GetProcAddress,eax,chr$("GetFontResourceInfoW")
.if eax!=NULL
mov edi,eax ;---convert ansi to unicode---
invoke MultiByteToWideChar,0,MB_PRECOMPOSED,_fontfile,-1,addr local_fontfile_wide,sizeof local_fontfile_wide
;---add font to Windows font table---
invoke AddFontResource,_fontfile
;---call undocumented GetFontResourceInfoW---
mov local_sizeof_buffer,sizeof local_fontstruct
lea ecx,local_sizeof_buffer
lea eax,local_fontfile_wide
lea esi,local_fontstruct
push 2 ;1=get fontname (works not always good) / 2=get structure with fontname
push esi ;structure
push ecx ;size of recievebuffer
push eax ;font filename
call edi ;GetFontResourceInfoW
.if eax==TRUE
mov ebx,eax ;modify return value to 1 add esi,28 ;point to correct unicode fontname ;---convert unicode to ansi---
invoke WideCharToMultiByte,0,WC_COMPOSITECHECK,esi,-1,_buffer,_buffersize,0,0
.endif
;---remove font from Windows font table---
invoke RemoveFontResource,_fontfile ;---free loaded dll---
invoke FreeLibrary,local_hlib
.endif mov eax,ebx
ret
GetFontName endp
Posted

Great ! I also tried it, but missed the AddFontResource and only tried it with

unregistered fonts.. dooh. Ofc I didn't get **** returned from that function all the time ^^

Cool that you figured it out ! :cool:

Posted

but this function is only for XP systems where gdi32.dll contains "GetFontResourceInfoW"

in win98`s gdi32.dll its just called "GetFontResourceInfo". So maybe there are no widechars. I dont have win98 system. so i cant figure it out...

Posted

Why dont you use the ascii version by default ? Is there no A-version on XP ?

Posted (edited)
Why dont you use the ascii version by default ? Is there no A-version on XP ?
nop. at least I couldn't find it. M$ ftw. Edited by Ufo-Pu55y
Posted (edited)

Which means this particular function is WinNT specific, since Unicode (wide char) functions can't natively be used under Win9x. And from my test app, it crashed when using a "A" alternative. Plus I have read somewhere that it is a native WinNT function..

Edited by mudlord
Posted

i noticed that using:

SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);

is not good after installing a font, even microsofts says, that its better when you use it after installing a font.

But it causes strange bugs.

  • 1 month later...
Posted
i noticed that using:
SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);

is not good after installing a font, even microsofts says, that its better when you use it after installing a font.

But it causes strange bugs.

Much better to use PostMessage... ;-)

Posted
i noticed that using:
SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);

is not good after installing a font, even microsofts says, that its better when you use it after installing a font.

But it causes strange bugs.

Much better to use PostMessage... ;-)

no, the problem is not only the SendMessage API.

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
×
×
  • Create New...