Jump to content
skyee

Font install

Recommended Posts

skyee

This is the current font install script:

$sFileName = '\\server.local\Software\_Install\_Default\1\CORPORATE FONT\*.*'
            If FileCopy($sFileName, @WindowsDir & '\Fonts',1) Then
            $sFileName = @WindowsDir & '\Fonts\' & StringTrimLeft($sFileName, StringInStr($sFileName, '\', 0, -1))
            DllCall('gdi32', 'long', "AddFontResourceA", 'String', $sFileName)
            EndIf

The fonts get copied, however they're not registred with system when I try to use them in i.e. MS Word. What else needs to be done? :D

Share this post


Link to post
Share on other sites
MrCreatoR

Hi,

This function should work (not mine, i just modified a little)...

Func InstallFont($sSourceFile, $sFontDescription, $sFontsPath)
    Local Const $HWND_BROADCAST = 0xFFFF
    Local Const $WM_FONTCHANGE = 0x1D
    
    Local $sFontFileName = StringRegExpReplace($sSourceFile, '^.*\\', '')
    If Not FileCopy($sSourceFile, $sFontsPath & "\" & $sFontFileName, 1) Then Return SetError(1, 0, 0)
    
    Local $aRet = DllCall("gdi32.dll", "Int", "AddFontResource", "str", @WindowsDir & "\fonts\" & $sFontFileName)
    If IsArray($aRet) And $aRet[0] > 0 Then
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", $sFontDescription, "REG_SZ", $sFontFileName)
        DllCall("user32.dll", "Int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_FONTCHANGE, "int", 0, "int", 0)
        Return 1
    EndIf
    Return SetError(2, 0, 0)
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites
skyee

Hi,

This function should work (not mine, i just modified a little)...

Func InstallFont($sSourceFile, $sFontDescription, $sFontsPath)
    Local Const $HWND_BROADCAST = 0xFFFF
    Local Const $WM_FONTCHANGE = 0x1D
    
    Local $sFontFileName = StringRegExpReplace($sSourceFile, '^.*\\', '')
    If Not FileCopy($sSourceFile, $sFontsPath & "\" & $sFontFileName, 1) Then Return SetError(1, 0, 0)
    
    Local $aRet = DllCall("gdi32.dll", "Int", "AddFontResource", "str", @WindowsDir & "\fonts\" & $sFontFileName)
    If IsArray($aRet) And $aRet[0] > 0 Then
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", $sFontDescription, "REG_SZ", $sFontFileName)
        DllCall("user32.dll", "Int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_FONTCHANGE, "int", 0, "int", 0)
        Return 1
    EndIf
    Return SetError(2, 0, 0)
EndFunc
Do you mind modifying this to enter source of my fonts? (\\server.local\Software\_Install\_Default\1\CORPORATE FONT\) since I admit have no clue where to put source from where script will get fonts?

Share this post


Link to post
Share on other sites
skyee

Do you mind modifying this to enter source of my fonts? (\\server.local\Software\_Install\_Default\1\CORPORATE FONT\) since I admit have no clue where to put source from where script will get fonts?

I tried this:

Func InstallFont($sSourceFile, $sFontDescription, $sFontsPath)
    Local Const $HWND_BROADCAST = 0xFFFF
    Local Const $WM_FONTCHANGE = 0x1D

    Local $sFontsPath = "\\server.local\Software\_Install\_Default\1\CORPORATE FONT\"
    Local $sFontFileName = StringRegExpReplace($sSourceFile, '^.*\\', '')
    If Not FileCopy($sSourceFile, $sFontsPath & "\" & $sFontFileName, 1) Then Return SetError(1, 0, 0)
   
    Local $aRet = DllCall("gdi32.dll", "Int", "AddFontResource", "str", @WindowsDir & "\fonts\" & $sFontFileName)
    If IsArray($aRet) And $aRet[0] > 0 Then
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", $sFontDescription, "REG_SZ", $sFontFileName)
        DllCall("user32.dll", "Int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_FONTCHANGE, "int", 0, "int", 0)
        Return 1
    EndIf
    Return SetError(2, 0, 0)
EndFunc

But it doesn't work :P I would _really_ like someone to help me out here.. Fonts just dont seem to install. And I'm a script noobie :D

Share this post


Link to post
Share on other sites
Jos

I guess it should be something like this (untested):

InstallFont('\\server.local\Software\_Install\_Default\1\CORPORATE FONT\fontname.ttf',"font description")

Func InstallFont($sSourceFile, $sFontDescription)
    Local Const $HWND_BROADCAST = 0xFFFF
    Local Const $WM_FONTCHANGE = 0x1D
    
    Local $sFontFileName = StringRegExpReplace($sSourceFile, '^.*\\', '')
    If Not FileCopy($sSourceFile, @WindowsDir & "\fonts\" & $sFontFileName, 1) Then Return SetError(1, 0, 0)
    
    Local $aRet = DllCall("gdi32.dll", "Int", "AddFontResource", "str", @WindowsDir & "\fonts\" & $sFontFileName)
    If IsArray($aRet) And $aRet[0] > 0 Then
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", $sFontDescription, "REG_SZ", $sFontFileName)
        DllCall("user32.dll", "Int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_FONTCHANGE, "int", 0, "int", 0)
        Return 1
    EndIf
    Return SetError(2, 0, 0)
EndFunc

:D


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
MrCreatoR

I changed the function, so now it can install more then one font (if passing wildcard as file name)...

InstallFont("\\server.local\Software\_Install\_Default\1\CORPORATE FONT\*.*")

Func InstallFont($sSourceFile, $sFontDescript="", $sFontsPath="")
    Local Const $HWND_BROADCAST = 0xFFFF
    Local Const $WM_FONTCHANGE = 0x1D
    
    If $sFontsPath = "" Then $sFontsPath = @WindowsDir & "\fonts"
    
    Local $sFontName = StringRegExpReplace($sSourceFile, "^.*\\", "")
    If Not FileCopy($sSourceFile, $sFontsPath & "\" & $sFontName, 1) Then Return SetError(1, 0, 0)
    
    Local $hSearch = FileFindFirstFile($sSourceFile)
    Local $iFontIsWildcard = StringRegExp($sFontName, "\*|\?")
    Local $aRet, $hGdi32_DllOpen = DllOpen("gdi32.dll")
    
    If $hSearch = -1 Then Return SetError(2, 0, 0)
    If $hGdi32_DllOpen = -1 Then Return SetError(3, 0, 0)
    
    While 1
        $sFontName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        
        If $iFontIsWildcard Then $sFontDescript = StringRegExpReplace($sFontName, "\.[^\.]*$", "")
        
        $aRet = DllCall($hGdi32_DllOpen, "Int", "AddFontResource", "str", $sFontsPath & "\" & $sFontName)
        If IsArray($aRet) And $aRet[0] > 0 Then
            RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", _
                $sFontDescript, "REG_SZ", $sFontsPath & "\" & $sFontName)
        EndIf
    WEnd
    
    DllClose($hGdi32_DllOpen)
    DllCall("user32.dll", "Int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_FONTCHANGE, "int", 0, "int", 0)
    Return 1
EndFunc

Edit: changed to DllOpen mode.

Edit 2: Added full path writing (to registry), in case that we want to install font to other path then @WindowsDir\fonts.

Edited by MsCreatoR
  • Like 1

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites
skyee

I changed the function, so now it can install more then one font (if passing wildcard as file name)...

InstallFont("\\server.local\Software\_Install\_Default\1\CORPORATE FONT\*.*")

Func InstallFont($sSourceFile, $sFontDescript="", $sFontsPath="")
    Local Const $HWND_BROADCAST = 0xFFFF
    Local Const $WM_FONTCHANGE = 0x1D
    
    If $sFontsPath = "" Then $sFontsPath = @WindowsDir & "\fonts"
    
    Local $sFontName = StringRegExpReplace($sSourceFile, "^.*\\", "")
    If Not FileCopy($sSourceFile, $sFontsPath & "\" & $sFontName, 1) Then Return SetError(1, 0, 0)
    
    Local $hSearch = FileFindFirstFile($sSourceFile)
    Local $iFontIsWildcard = StringRegExp($sFontName, "\*|\?")
    Local $aRet, $hGdi32_DllOpen = DllOpen("gdi32.dll")
    
    If $hSearch = -1 Then Return SetError(2, 0, 0)
    If $hGdi32_DllOpen = -1 Then Return SetError(3, 0, 0)
    
    While 1
        $sFontName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        
        If $iFontIsWildcard Then $sFontDescript = StringRegExpReplace($sFontName, "\.[^\.]*$", "")
        
        $aRet = DllCall($hGdi32_DllOpen, "Int", "AddFontResource", "str", $sFontsPath & "\" & $sFontName)
        If IsArray($aRet) And $aRet[0] > 0 Then
            RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", _
                $sFontDescript, "REG_SZ", $sFontsPath & "\" & $sFontName)
        EndIf
    WEnd
    
    DllClose($hGdi32_DllOpen)
    DllCall("user32.dll", "Int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_FONTCHANGE, "int", 0, "int", 0)
    Return 1
EndFunc

Edit: changed to DllOpen mode.

Edit 2: Added full path writing (to registry), in case that we want to install font to other path then @WindowsDir\fonts.

Excellent work! Thank you so much :D

Share this post


Link to post
Share on other sites
Ármányos Kő

I changed the function, so now it can install more then one font (if passing wildcard as file name)...

InstallFont("\\server.local\Software\_Install\_Default\1\CORPORATE FONT\*.*")

Func InstallFont($sSourceFile, $sFontDescript="", $sFontsPath="")
    Local Const $HWND_BROADCAST = 0xFFFF
    Local Const $WM_FONTCHANGE = 0x1D
    
    If $sFontsPath = "" Then $sFontsPath = @WindowsDir & "\fonts"
    
    Local $sFontName = StringRegExpReplace($sSourceFile, "^.*\\", "")
    If Not FileCopy($sSourceFile, $sFontsPath & "\" & $sFontName, 1) Then Return SetError(1, 0, 0)
    
    Local $hSearch = FileFindFirstFile($sSourceFile)
    Local $iFontIsWildcard = StringRegExp($sFontName, "\*|\?")
    Local $aRet, $hGdi32_DllOpen = DllOpen("gdi32.dll")
    
    If $hSearch = -1 Then Return SetError(2, 0, 0)
    If $hGdi32_DllOpen = -1 Then Return SetError(3, 0, 0)
    
    While 1
        $sFontName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        
        If $iFontIsWildcard Then $sFontDescript = StringRegExpReplace($sFontName, "\.[^\.]*$", "")
        
        $aRet = DllCall($hGdi32_DllOpen, "Int", "AddFontResource", "str", $sFontsPath & "\" & $sFontName)
        If IsArray($aRet) And $aRet[0] > 0 Then
            RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", _
                $sFontDescript, "REG_SZ", $sFontsPath & "\" & $sFontName)
        EndIf
    WEnd
    
    DllClose($hGdi32_DllOpen)
    DllCall("user32.dll", "Int", "SendMessage", "hwnd", $HWND_BROADCAST, "int", $WM_FONTCHANGE, "int", 0, "int", 0)
    Return 1
EndFunc

Edit: changed to DllOpen mode.

Edit 2: Added full path writing (to registry), in case that we want to install font to other path then @WindowsDir\fonts.

This is excellent!

How would the deinstalling look like? I tried many basic things (deleting from font directory...), but surely they did not work.

I need to do something like Suitcase does on Mac.

Share this post


Link to post
Share on other sites
bluespirit

Hi,

I'm trying to extract the font name out of a .ttf file to enter the correct info into the registry. Anyone know how to do this?

I'm installing the fonts using the AddFontResource DLL call, Registry write and SendMessage DLL call method as described below. However, this relies on you already knowing the font name. I was also going to rename the .ttf file with the full name before installing.

The best I've managed to find is a GetFontName function by Holger but this only works once the font is already installed or in memory.

Thanks for any help,

James

Share this post


Link to post
Share on other sites
DangerousDan

nice find, I am adding this later since I have been trying to use fileinstall by itself. new to scripting as well and this autoit thing is aswesome!

I usually veiw the files from the dos prompt to get actual file names...

Share this post


Link to post
Share on other sites
ProgAndy

Here is a full install routine for TTF fonts in VB : http://www.codeguru.com/vb/gen/vb_system/w...icle.php/c1659/

Basically, a .FOT font resource is created and installed. From this, the font name is being extracted :)

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
DangerousDan

are you guys having to restart after using this function to install a font? I am... :)

Share this post


Link to post
Share on other sites
jennico

very nice work ! thx. j. :D


Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Share this post


Link to post
Share on other sites
Myicq

How would the deinstalling look like? I tried many basic things (deleting from font directory...), but surely they did not work.

I have not tried this, but guess that mostly same code can be used.

You have

BOOL RemoveFontResource(
  __in  LPCTSTR lpFileName
);

See: http://msdn.microsoft.com/en-us/library/windows/desktop/dd162922%28v=vs.85%29.aspx

My steps would be

  • From registry find full path name(s)
  • Call above DLL
  • Remove registry entry
  • Delete file
  • Send broadcast message WM_FONTCHANGE
Not sure if you need to use RemoveFontResource or RemoveFontResource[A|W] for Ansi/Unicode.

Code from MrCreator should be simple to modify.

Good luck.


I am just a hobby programmer, and nothing great to publish right now.

Share this post


Link to post
Share on other sites
Silverback83

I know this is a very old topic, but rather than start a new one its best I just reply to this?

Post #9 doesnt work for me. (Obv replacing the server directory with my own. It seems to do nothing. Is that because the code no longer works due to different syntax in newer versions of autoit or possibly another reason?

I'm simply trying to auto install a folder of fonts using a script like shown in post #9

I get Exit Code 0 which Google tells is a success expect nothing happens.

Share this post


Link to post
Share on other sites
Melba23

Silverback83,

Quote

I know this is a very old topic, but rather than start a new one its best I just reply to this?

No, we prefer that you open a new thread and link to the old one if required. As the code you are trying to run dates from some 9 years ago I am not at all surprised that it does not run under the current release.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______Open Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Silverback83

Ahh ok sorry.

Share this post


Link to post
Share on other sites

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

×