NSIS 简易教程(三)

本文详细介绍了NSIS(Nullsoft Scriptable Install System)安装脚本中的回调函数,包括.onGUIInit、.onInit、.onInstFailed、.onInstSuccess等,以及在不同安装阶段如何自定义用户界面、处理用户交互和错误情况。同时,还讲解了如.onRebootFailed、.onSelChange等特殊场景下的回调函数应用。
摘要由CSDN通过智能技术生成

;回调函数有特定的名字,在特定的时间调用
  ;目前可用的callback functions:
  1.    .onGUIInit
  ;this callback will be called just before the first page is loaded and the installer dialog is shown
  ; allowing you to tweak(调整) the user interface
  !include "WinMessages.nsh"
  Function .onGUIInit
  GetDlgItem $R0 $HWNDPARENT 1028
  CreateFont $R1 "Tahoma" 10 700
  SendMessage $R0 ${WM_SETFONT} $R1 0
  #set background color to white and text color to red
  SetCtlColors $R0 FFFFFF FF0000
  FunctionEnd
  2.    .onInit;即将完成初始化
  ;this function will be called when the installer is nearly finished initializing. if the '.onInit' function
  ; calls abort,the installer will quit instantly.
  Function .onInit
  MessageBox MB_YESNO "This will install .Continue?" IDYES NoAbort
  Abort;causes installer to quit.
  FunctionEnd
  3.    .onInstFailed;安装失败时点击取消
  ;this callback is called when the user hits the 'cancel' button after the install has failed
  Function .onInstFailed
  MessageBox MB_OK "Better luck next time."
  FunctionEnd
  4.    .onInstSuccess ;安装成功窗口关闭前
  ;this callback is called when the install was successful,right before the install window closes(which
  ;may be after the user clicks 'Close' if AutoCloseWindow or SetAutoClose is set to false)
  Function .onInstSuccess
  MessageBox MB_YESNO "Congrats,it worked.View readme?" IDNO NoReadme
  Exec notepad.exe;view readme or whatever if you want.
  NoReadme:
  FunctionEnd
  5.    .onGUIEnd;;安装成功窗口关闭后
  ;this callback is called right after the installer window closes.Use it to free any user interface
  ;related plug-ins if needed
  6.    .onMouseOverSection;鼠标放置的session发生变化是被调用,可以用于改变描述信息
  ;this callback is called whenever the mouse position over the sections tree has changed.this allows
  ;you to set a description for each section for example.this section id on which the mouse is over
  ;currently is stored ,temporarily,in $0
  Function .onMouseOverSection
  FindWindow $R0 "#32770" "" $HWNDPARENT
  GetDlgItem $R0 $R0 1043
  StrCmp $0 0 "" +2
  SendMessage $R0 ${WM_SETTEXT} 0 "STR:first section description"
  StrCmp $0 1 "" +2
  SendMessage $R0 ${WM_SETTEXT} 0 "STR:second section description"
  FunctionEnd
  7.    onRebootFailed;自动重启失败
  ;this callback is called if Reboot fails.WritUninstaller,plug-ins,File and WriteRegBin should not be used
  ;in this callback
  Function .onRebootFailed
  MessageBox MB_OK|MB_ICONSTOP "Reboot failed,Please reboot manually." /SD IDOK
  FunctionEnd
  8.    onSelChange;选择发生变化
  ;called when the selection changes on the component page.Usefull for using with SectionSetFlags and
  ;SectionSetFlags and SectionGetFlags.
  ;selection changes include both section selection and installation type change.
  9.    onUserAbort
  ;安装还没有失败但用户选择了取消时调用,调用Abort时,程序不会退出
  ;called when the user hits the 'cancel' button,and the install hasn't already failed.If this function
  ;calls Abort,the install will not be aborted.
  Function
  MessageBox MB_YESNO "Abort install?" IDYES NoCancelAbort
  Abort;
  NoCancelAbort:
  do something
  FunctionEnd
  10.    .onVerifyInstDir
  ;在安装文件夹改变后调用,检查该路径是否有效,如果调用了Abort则$INSTDIR视为无效
  ;enables control over whether or not an installation path is valid for your installer. This code will be
  ;called every time the user changes the install directory,so it shouldn't do anything crazy with MessageBox
  ;or the likes.If this function calls Abort,the installation path in $INSTDIR is deemed invalid.
  Function
  IfFileExists $INSTDIR\Winamp.exe PathGood
  Abort
  PathGood:
  FunctionEnd
  Uninstall Callbacks
  11.    un.onGUIInit;卸载程序的类似于.onGUIInit
  ;called just before the first page is loaded and the installer dialog is shown, allowing you to tweak the
  ;user interface.
  12.    un.onInit;若调用了Abort,程序直接退出,在有必要时检查一下$INSTDIR
  ;this callback will be called when the uninstaller is nearly finished initializeing.If the
  ;'un.onInit' function calls Abort,the uninstaller will quit instantly.Note that this function can verify
  ;and/or modify $INSTDIR if necessary.
  Function un.onInit
  MessageBox MB_YESNO "This will uninstall." IDYES NoAbort
  Abort
  NoAbort:
  FunctionEnd
  Function un.onInit
  IfFileExists $INSTDIR\myfile.exe found
  MessageBox MB_OK "Uninstall path incorrent"
  Abort
  found:
  FunctionEnd
  13.    un.onUninstFailed
  ;called when the user hits the 'cancel' button after the uninstall has failed(if it used the 
  ;Abort command or otherwise failed).
  14.    un.onUninstSuccess
  15.    un.onGUIEnd
  16.    un.onRebootFailed
  17.    un.onSelChange
  18.    un.onUserAbort
  Installer Attributes
  these attributes control how the installer looks and functions,including which pages are present in the
  installer ,as what text is displayed in each part of each page,how the installer is named,what icon the
  installer uses,the default installation directory,what file it writes out,and more,note that these attributes
  can be set anywhere in the file except in a Section or Function .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值