Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Load font from file or resource, without needed install to system.
CustomFont v2.01 (2018-8-25)
Description: Load font from file or resource, without needed install to system.
Useage Examples:
* Load From File
font1 := New CustomFont("ewatch.ttf")
Gui, Font, s100, ewatch
* Load From Resource
Gui, Add, Text, HWNDhCtrl w400 h200, 12345
font2 := New CustomFont("res:ewatch.ttf", "ewatch", 80) ; <- Add a res: prefix to the resource name.
* The fonts will removed automatically when script exits.
To remove a font manually, just clear the variable (e.g. font1 := "").
Class CustomFont
static FR_PRIVATE := 0x10
__New(FontFile, FontName="", FontSize=30) {
if RegExMatch(FontFile, "i)res:\K.*", _FontFile)
this.AddFromResource(_FontFile, FontName, FontSize)
AddFromFile(FontFile) {
if !FileExist(FontFile) {
throw "Unable to find font file: " FontFile
DllCall( "AddFontResourceEx", "Str", FontFile, "UInt", this.FR_PRIVATE, "UInt", 0 ) := FontFile
AddFromResource(ResourceName, FontName, FontSize = 30) {
static FW_NORMAL := 400, DEFAULT_CHARSET := 0x1
nSize := this.ResRead(fData, ResourceName)
fh := DllCall( "AddFontMemResourceEx", "Ptr", &fData, "UInt", nSize, "UInt", 0, "UIntP", nFonts )
hFont := DllCall( "CreateFont", Int,FontSize, Int,0, Int,0, Int,0, UInt,FW_NORMAL, UInt,0
, Int,0, Int,0, UInt,DEFAULT_CHARSET, Int,0, Int,0, Int,0, Int,0, Str,FontName ) := {fh: fh, hFont: hFont}
ApplyTo(hCtrl) {
SendMessage, 0x30,, 1,, ahk_id %hCtrl%
__Delete() {
if IsObject( {
DllCall( "RemoveFontMemResourceEx", "UInt", )
DllCall( "DeleteObject" , "UInt", )
} else {
DllCall( "RemoveFontResourceEx" , "Str",, "UInt", this.FR_PRIVATE, "UInt", 0 )
; ResRead() By SKAN, from
ResRead( ByRef Var, Key ) {
VarSetCapacity( Var, 128 ), VarSetCapacity( Var, 0 )
If ! ( A_IsCompiled ) {
FileGetSize, nSize, %Key%
FileRead, Var, *c %Key%
Return nSize
If hMod := DllCall( "GetModuleHandle", UInt,0 )
If hRes := DllCall( "FindResource", UInt,hMod, Str,Key, UInt,10 )
If hData := DllCall( "LoadResource", UInt,hMod, UInt,hRes )
If pData := DllCall( "LockResource", UInt,hData )
Return VarSetCapacity( Var, nSize := DllCall( "SizeofResource", UInt,hMod, UInt,hRes ) )
, DllCall( "RtlMoveMemory", Str,Var, UInt,pData, UInt,nSize )
Return 0
#Include Class_CustomFont.ahk
font1 := New CustomFont("CHOCD TRIAL___.otf")
Gui, Margin, 30, 10
Gui, Color, DECFB2
Gui, Font, s100 c510B01, Chocolate Dealer
Gui, Add, Text, w400, Chocolate
Gui, Show
#Include Class_CustomFont.ahk
font1 := New CustomFont("res:moonhouse.ttf", "moonhouse", 50)
font2 := New CustomFont("res:Selavy.otf", "Selavy-Regular", 20)
Gui, Color, Black
Gui, Add, Text, hwndhText1 w500 h50 c00FF00 Center, AutoHotkey
Gui, Add, Text, hwndhText2 wp hp cWhite Center,
Gui, Add, Button, xm gRemoveFont1, Remove Font1
Gui, Show
font1 := ""
WinSet, Redraw,, A
FileInstall, moonhouse.ttf, -
FileInstall, Selavy.otf, -

This comment has been minimized.

Copy link

@KRG-23 KRG-23 commented Aug 1, 2018

== solved in ==
Hello and thank you very much for this script.
However I am facing a problem when the script after sometime is not working, the default system font displays.
It worked for a couple of time and then the loaded font disappeared.
How should I request some help/support, here on in the boards? Thank you for help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment