|
It's time to wrap up this thread, then. I've got the trick down a tee for loading and unloading fonts on the fly (instantly, too - no delay) without rebooting, or mucking about with the windows font folder or system registry, so here's the rundown in case anyone else needs it. My ignorance has the advantage of keeping the coding simple:
THE PROBLEM
Some programs, such as multilingual dictionaries, require special fonts to work. These are normally installed along with the program. However, those additional fonts also appear in the font menu of word processors, which can become very cluttered. The solution, as described below, is only to load special "one program only" fonts when those programs start, and to unload the fonts when the program closes. The following method works on Windows XP SP2 Home edition, and should work on most other flavours of Windows too. The idea could easily be applied to other OS.
THE SOLUTION
The program is loaded via a batch file rather than via the program's icon/shortcut. Before triggering the program (the second line, below), the batch file sends a script to autohotkey to load the required fonts using the AddFontResource function, and broadcast the change system-wide using WM_FONTCHANGE. The program then loads and runs. Upon the program closing, the batch file continues by sending an unload script to autohotkey.
I didn't try the alternative approach of letting autohotkey detect the program launch - I'm much happier with a one-off, user-triggered solution than have autohotkey poll constantly for programs that I might not use for weeks.
Files required:
[BATCH FILE]
c:\programs\autohotkey\autohotkey.exe c:\fonts\fonts_load.ahk"
c:\programs\fonthungryprogram\whatever.exe
c:\programs\autohotkey\autohotkey.exe c:\fonts\fonts_unload.ahk"
[FONTS_LOAD.AHK]
DllCall("AddFontResource", Str, "c:\fonts\fontone.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fonttwo.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fontthree.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fontfour.ttf")
DllCall("AddFontResource", Str, "c:\fonts\fontfive.ttf")
PostMessage, 0x1D,,,, ahk_id 0xFFFF
return
[FONTS_UNLOAD.AHK]
DllCall("RemoveFontResource", Str, "c:\fonts\fontone.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fonttwo.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fontthree.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fontfour.ttf")
DllCall("RemoveFontResource", Str, "c:\fonts\fontfive.ttf")
PostMessage, 0x1D,,,, ahk_id 0xFFFF
return
Barring any major oversight, I think that's all there is to it. I'm not sure whether sending the scripts to autohotkey when it's already running is recommended, but it works, and scripts contained in autohotkey.ini don't appear to be affected (they continue to work during and after the font loading and unloading).
Thanks once again to everyone who helped out on this thread, and of course to the developers of autohotkey (thankyouthankyouthankyou!) - a really sweet piece of programming that I'm well chuffed to have come across.
|
|