NSIS 入门教程 (三)

引言
 
在教程的第二部分中,我们为安装程序增加了一个卸载程序,并查看了一些其他的向导页面以及安装部分的选择。第三部分的目标是使安装程序的外观更加现代化。
 
更现代的外观
 
为了给安装程序一个更现代的外观,我们要启用现代用户界面。要提升我们的安装程序(基于“secondinstaller”),不需要做太多的修改:
 

!include "MUI2.nsh"


Name "Fifth Installer"

OutFile "fifthinstaller.exe"
InstallDir " $PROGRAMFILES \MyFifthInstaller"

!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English"

Section ""
SetOutPath $INSTDIR
File "C:\Windows\system32\notepad.exe"
WriteUninstaller " $INSTDIR \uninstall.exe"
SectionEnd

Section
"Uninstall"
Delete $INSTDIR \uninstall.exe
Delete $INSTDIR \notepad.exe
RMDir $INSTDIR
SectionEnd
 
上面的脚本一共做出了三项改变:

!include "MUI2.nsh"

插入 MUI2.nsh 文件,就好像它是我们脚本的一部分一样。所需的宏已在 MUI2.nsh 文件中定义。

!insertmacro ......

作为 Page 或 UninstPage 命令的替代,插入带有宏的所需页面。

!insertmacro MUI_LANGUAGE "English"

使用此宏可以插入英文文本.对于中文,需要指定语言为“SimpChinese”

 

第四个安装程序重装

我们当然希望将第二部分中添加的对话框也使用现代用户界面来实现。

下面开始:

!include "MUI2.nsh"

Name "Sixth Installer"
!define INSTALLATIONNAME "MySixthInstaller"
OutFile "MySixthInstaller.exe"

InstallDir $PROGRAMFILES\${INSTALLATIONNAME}

Page custom getUsername nsDialogsPageLeave

!insertmacro MUI_PAGE_LICENSE "license.txt"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "SimpChinese"

LangString PAGE_TITLE ${LANG_SimpChinese} "自定义页面示例"
LangString PAGE_SUBTITLE ${LANG_SimpChinese} "请输入用户名或密码"

Var Text
Var Dialog

Function ".onInit"
InitPluginsDir
#此处预释放某些安装必须的资源或者文件....
#File /oname=$PLUGINSDIR\name.ini "name.ini"
FunctionEnd

Function
getUsername
!insertmacro MUI_HEADER_TEXT $(PAGE_TITLE) $(PAGE_SUBTITLE)
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog
== error
Abort
${EndIf}
${NSD_CreateLabel}
0 14u 19% 12u "用户名或密码"
${NSD_CreateText} 20% 13u 100% 15u ""
Pop $Text
nsDialogs::Show
FunctionEnd

Function
nsDialogsPageLeave
${NSD_GetText} $Text $0
MessageBox MB_OK " 用户名或密码是: $0 "
FunctionEnd

Section
""
SetOutPath $INSTDIR
File C:\Windows\system32\notepad.exe
WriteUninstaller $INSTDIR\uninstall.exe

WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${INSTALLATIONNAME}" "DisplayName" "Forth Installer"

WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${INSTALLATIONNAME}" "UninstallString" '"$INSTDIR\uninstall.exe"'

WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${INSTALLATIONNAME}" "NoModify" 1

WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${INSTALLATIONNAME}" "NoRepair" 1
SectionEnd

Section
"Sample Text File"
;File "license.txt"
SectionEnd

Section
/o "Another Sample Text File"
;非必要组件
; File "license2.txt"
SectionEnd

Section
"Start Menu Shortcuts"
CreateDirectory "$SMPROGRAMS\${INSTALLATIONNAME}"
CreateShortCut "$SMPROGRAMS\${INSTALLATIONNAME}\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
CreateShortCut "$SMPROGRAMS\${INSTALLATIONNAME}\notepad.lnk" "$INSTDIR\notepad.exe" "" "$INSTDIR\notepad.exe" 0
SectionEnd

Section
"Uninstall"
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${INSTALLATIONNAME}"
Delete $INSTDIR\uninstall.exe
Delete $INSTDIR\notepad.exe
Delete $INSTDIR\license.txt
Delete $INSTDIR\license2.txt
RMDir $INSTDIR
Delete "$SMPROGRAMS\${INSTALLATIONNAME}\*.*"
RMDir "$SMPROGRAMS\${INSTALLATIONNAME}"
SectionEnd

 

NSIS Modern UI 2.0 安装程序

 

与“第四个安装程序”相比最重要的变化:
 
我们已经在上面看到了!include 指令、!insertmacro MUI_LANGUAGE  命令和 !insertmacro MUI_PAGE_ ... 命令。Sections  被逐一从第四个安装程序中移植过来的。同样,Functions  也被移植过来,但有一个小而重要的变化:

!insertmacro MUI_HEADER_TEXT $(PAGE_TITLE) $(PAGE_SUBTITLE)

这行代码定义了自定义页面的标题栏文字内容。如脚本所示,这并不需要其他特殊的 !insertmacro 指令。标题和副标题内容使用 LangString 指令定义即可。

结论
 
通过这三篇入门教程,我们可以看到创建一个安装程序是相对简单的。
 
NSIS WiKi 是一个门户,提供各种脚本和代码片段,这些脚本和代码片段已经为许多需求提供了解决方案。在我的博客中,我也会继续在 NSIS 分类下发布自己对 nsis 的想法和解决方案。希望能帮到诸位刚入门的同好,与诸位共勉!
posted @ 2024-07-20 12:01  水晶石  阅读(30)  评论(0编辑  收藏  举报