NSIS 安装字体

2025-06-08 88点热度 1人点赞 0条评论

NSIS 基本字体安装/卸载,需要用到头文件 FontInstall.nsh

FontInstall.nsh:

!include LogicLib.nsh
!include WinMessages.nsh

!macro FontInstallHelper FontFileSrc FontFileDst FontInternalName Resource RegSuffix RegRoot
	ClearErrors
	!if "${FontFileSrc}" != ""
	${IfNot} ${FileExists} "${FontFileDst}"
		File "/oname=${FontFileDst}" "${FontFileSrc}"
	${EndIf}
	!endif
	${IfNot} ${Errors}
		Push $0
		Push "${Resource}"
		Exch $1
		Push "${FontInternalName}${RegSuffix}"
		Exch $2
		Push $9
		StrCpy $9 "Software\Microsoft\Windows NT\CurrentVersion\Fonts"
		!if "${NSIS_CHAR_SIZE}" < 2
		ReadRegStr $0 ${RegRoot} "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
		${IfThen} $0 == "" ${|} StrCpy $9 "Software\Microsoft\Windows\CurrentVersion\Fonts" ${|}
		!endif
		System::Call 'GDI32::AddFontResource(tr1)i.r0'
		${If} $0 <> 0
			WriteRegStr ${RegRoot} "$9" "$2" "$1"
		${Else}
			SetErrors
		${EndIf}
		Pop $9
		Pop $2
		Pop $1
		Pop $0
	${Else}
		SetErrors
	${EndIf}
!macroend

!macro FontInstallTTF FontFileSrc FontFileName FontInternalName
	!insertmacro FontInstallHelper "${FontFileSrc}" "$Fonts\${FontFileName}" "${FontInternalName}" "${FontFileName}" " (TrueType)" HKLM
!macroend

!macro FontUninstallHelper FontFileDst FontInternalName Resource RegSuffix RegRoot
	System::Call 'GDI32::RemoveFontResource(t"${Resource}")'
	DeleteRegValue ${RegRoot} "Software\Microsoft\Windows NT\CurrentVersion\Fonts" "${FontInternalName}${RegSuffix}"
	!if "${NSIS_CHAR_SIZE}" < 2
	DeleteRegValue ${RegRoot} "Software\Microsoft\Windows\CurrentVersion\Fonts" "${FontInternalName}${RegSuffix}"
	!endif
	ClearErrors
	Delete "${FontFileDst}"
!macroend

!macro FontUninstallTTF FontFileName FontInternalName
	!insertmacro FontUninstallHelper "$Fonts\${FontFileName}" "${FontInternalName}" "${FontFileName}" " (TrueType)" HKLM
!macroend

如果知道字体内部名称,可以使用硬编码名称安装,否则请使用 FontInfo 插件动态获取字体名称安装。

示例:

RequestExecutionLevel Admin
!include "FontInstall.nsh"

Section

	StrCpy $1 0
	; 硬编码名称安装
	insertmacro FontInstallTTF "D:\Project\MyFoot.ttf" "MyFoot.ttf" "MyFoot Regular" ; MyFoot Regular 为字体内部名称,字体路径为本机路径而非客户机路径!
	${IfNotThen} ${Errors} ${|} IntOp $1 $1 + 1 ${|}

	; 使用插件动态获取名称安装
	InitPluginsDir
	File "/oname=$PluginsDir\MyFoot.ttf" "D:\Project\myfiles\MyFoot.ttf"
	FontInfo::GetFontName "$PluginsDir\MyFoot.ttf"
	${If} $0 != ""
		!insertmacro FontInstallTTF "D:\Project\MyFoot.ttf" "MyFoot.ttf" $0
		${IfNotThen} ${Errors} ${|} IntOp $1 $1 + 1 ${|}
	${EndIf}

	; 字体成功安装
	${If} $1 <> 0
		DetailPrint "已成功安装$1字体..."
		SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
	${EndIf}

SectionEnd

Section Uninstall

	StrCpy $1 0
	; 硬编码名称卸载
	!insertmacro FontUninstallTTF "MyFoot.ttf" "MyFoot Regular"
	${IfNotThen} ${Errors} ${|} IntOp $1 $1 + 1 ${|}

	; 使用插件动态获取名称卸载
	FontInfo::GetFontName "$Fonts\MyFoot.ttf"
	${If} $0 != ""
		!insertmacro FontUninstallTTF "MyFoot.ttf" $0
		${IfNotThen} ${Errors} ${|} IntOp $1 $1 + 1 ${|}
	${EndIf}

	; 字体成功卸载
	${If} $1 <> 0
		DetailPrint "已成功卸载$1字体..."
		SendMessage ${HWND_BROADCAST} ${WM_FONTCHANGE} 0 0 /TIMEOUT=5000
	${EndIf}

SectionEnd

南陇居士

NLJS.SITE - 分享 · 记录

文章评论