; ======================================================================================= ; Scriptname : Class_CustomFont.ahk ; Description : Load font from a font file, without need installed to system. ; Date : 2013-12-5 ; Tested On : AutoHotkey 1.1.13.01 A32/U32, Windows XP SP3 ; Author : tmplinshi ; Credits : ResRead(), and some other codes by SKAN. ; ======================================================================================= Class CustomFont { static FontList := [] static MemFontList := [] static FR_PRIVATE := 0x10 Add(FontFile) { This.FontList.Insert( FontFile ) Return, DllCall( "AddFontResourceEx", "Str", FontFile, "UInt", This.FR_PRIVATE, "UInt", 0 ) } ; Reference: http://www.autohotkey.com/board/topic/29396-crazy-scripting-include-and-use-truetype-font-from-script/ AddFromResource(hCtrl, FontFile, FontName, FontSize = 30, ByRef hFont="") { static FW_NORMAL := 400, DEFAULT_CHARSET := 0x1 if !hFont { nSize := This.ResRead(fData, FontFile) 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 ) This.MemFontList.Insert( {"fh":fh, "hFont":hFont} ) } SendMessage, 0x30, hFont, 1,, ahk_id %hCtrl% } Remove() { Loop, % This.FontList.MaxIndex() DllCall( "RemoveFontResourceEx" , "Str", This.FontList[A_Index], "UInt", This.FR_PRIVATE, "UInt", 0 ) Loop, % This.MemFontList.MaxIndex() { DllCall( "RemoveFontMemResourceEx", "UInt", This.MemFontList[A_Index]["fh"] ) DllCall( "DeleteObject" , "UInt", This.MemFontList[A_Index]["hFont"] ) } } ; ResRead() By SKAN, from http://www.autohotkey.com/board/topic/57631-crazy-scripting-resource-only-dll-for-dummies-36l-v07/?p=609282 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 } }