nsis安装程序

官网
https://nsis.sourceforge.io/Main_Page
我的一份范本:

(DevelopApps下找到)
用于软件安装及卸载。
稍后我会发一份本程序基本指令介绍,不会太长。

1 个赞

1.头文件

; The name of the installer
Name "test"

; The file to write
OutFile "in.exe"

XPStyle on

!include "WinVer.nsh"

LoadLanguageFile "${NSISDIR}\Contrib\Language files\English.nlf"
LoadLanguageFile "${NSISDIR}\Contrib\Language files\SimpChinese.nlf"
LoadLanguageFile "${NSISDIR}\Contrib\Language files\TradChinese.nlf"
;--------------------------------
;Version Information

  VIProductVersion "1.0.0.0"
  VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "test"
  VIAddVersionKey /LANG=${LANG_ENGLISH} "Comments" "test"
  VIAddVersionKey /LANG=${LANG_ENGLISH} "CompanyName" "test"
  VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalTrademarks" ""
  VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" ""
  VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "test"
  VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "1.0.0"
  

;--------------------------------

; The default installation directory
InstallDir $PROGRAMFILES\test

; Registry key to check for directory (so if you install again, it will 
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\test" "Install_Dir"

; Request application privileges for Windows Vista
RequestExecutionLevel admin

;--------------------------------

; Pages
Page license
licensedata license
; license
Page directory
  
Page components
Page instfiles
ShowInstDetails show

;UninstPage uninstConfirm
UninstPage instfiles
ShowUninstDetails show

Name:项目名称。会显示在主程序标题栏。
Outfile:输出程序包名称。我选择in.exe,则成品如下:


xpstyle:是否要xp风格。on/off请自己尝试。对于下面的对话框指令效果有影响。
!include:此处纳入一个系统版本判断头文件,下面有用。
Loadlanguagefile:载入语言包,自选,按需选择。各语言文件名自行查询。

VI等版本指令不多解释,因为条目过多。LANG语言编码有LANG_ENGLISH,LANG_SIMPCHINESE,LANG_TRADCHINESE等,以此类推。
Installdir:设置默认安装目录。Unicode兼容性未知,未测试。
InstallDirRegKey:应该是注册表用于登记安装目录。我没尝试过其他写法,改写影响不明。
RequestExecutionLevel:申请权限。none/user/highest/admin四种。
Page:安装程序页。
license版权声明,directory目录选择,components项目选择,instfiles安装页。其他没了解。
UninstPage:卸载程序页。用法与上一个一样。
ShowInstDetails/ShowUninstDetails:安装过程显示。show默认显示,hide手动显示,nevershow无显示。

1 个赞

Function:程序段。
.oninit:启动第一任务区。
.onInstSuccess:成功安装后。
.onInstFailed:不成功安装后。
un.onUninstSuccess:成功卸载后。
un.onUninstFailed:不成功卸载后。
FunctionEnd:结束程序段。

Section,SectionEnd:安装主体。加!表示必选。

SetAutoClose true/false:窗口安装完后是否自动关闭。自行选择。
接下来从${if}到${endif}有一套版本判断语句,不用就删了。
Messagebox:通知框。
SetSilent:静默安装。
HideWindow:隐藏silent/显示normal主程序窗口。
SectionIn RO意义不明,已忘记。
SetOutPath $INSTDIR设安装目录。
File写文件。
Exec执行。
DetailPrint输出文本。
WriteUninstaller制作卸载程序。
WriteReg与注册表,Windows机制有关,我了解不多。DeleteRegKey也如此。
CreateDirectory建目录。
CreateShortcut建快捷方式。
Delete删文件。
RMDir删目录。
Abort输出信息并中断。
goto,IfErrors,IfAbort用于程序跳转。
如此。

(我查了一下,SectionIn应该起到了作用,请各位自己研究吧。以上。)

1 个赞

Function .oninit
  ;SetAutoClose true
  InitPluginsDir

  ${if} ${AtLeastWin95}
    MessageBox MB_OK|MB_ICONINFORMATION "you computer can run this program."
  ${else}
    MessageBox MB_OK|MB_ICONSTOP "you computer cannot run this program."
    Abort
  ${endif}
  MessageBox MB_YESNO|MB_ICONQUESTION "Would you like the installer to be silent from now on?" /SD IDNO IDNO no IDYES yes
  yes:
    SetSilent silent
    Goto done
  no:
    SetSilent normal
  done:
FunctionEnd
;when it finish
Function .onInstSuccess
  HideWindow
  MessageBox MB_OK|MB_ICONINFORMATION "install success."
FunctionEnd
;when it failed
Function .onInstFailed
  HideWindow
  MessageBox MB_OK|MB_ICONSTOP "install failed."
FunctionEnd
;-------------------

function到functionend是代码段。
程序一启动就会加载oninst
根据inst有没有正常结束(abort中断了运行和安装进程,使其不正常退出),选择在安装流程的末尾加载一段指示程序(这一类.~程序还有很多,我只用得上这三个。)

hidewindow使主窗口先消失,再弹出对话框
mbok仅确认
yesno选择
其他几个你们试试吧
注,头文件中xpstyle设置对对话框的显示有影响