分享

NSIS学习笔记-Function

 不甘平庸的疯子 2024-05-19

Function与FunctionEnd:定义一个函数。自定义函数使用call命令调用。

Function func		#定义名字为func的函数
  # some commands
FunctionEnd

Section
  Call func			#调用func函数
SectionEnd

Callback Function:回调函数。回调函数具有特殊的名称。以下是可用的回调函数列表。

  • .onGUIInit:加载第一个页面时调用,可以在这个函数里调整用户界面。注意前面有个.

 !include "WinMessages.nsh"

 Function .onGUIInit		#定义回调函数
   # 1028 is the id of the branding text control
   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

  • .onInit:完成初始化是调用。可以通过Abort命令退出安装。

 Function .onInit		#定义回调函数
   MessageBox MB_YESNO "This will install. Continue?" IDYES NoAbort
     Abort ; causes installer to quit.		#根据弹框内容确定是否终止安装。
   NoAbort:
 FunctionEnd
 
 Function .onInit		#定义回调函数
   ReadINIStr $INSTDIR $WINDIR\wincmd.ini Configuration InstallDir
   StrCmp $INSTDIR "" 0 NoAbort
     MessageBox MB_OK "Windows Commander not found. Unable to get install path."
     Abort ; causes installer to quit.		#根据弹框内容确定是否终止安装。
   NoAbort:
 FunctionEnd

  • .onInstFailed:安装失败或Abort命令后调用。

Function .onInstFailed
  MessageBox MB_OK "Better luck next time."
FunctionEnd

  • .onInstSuccess:默认在安装成功时,并在窗口关闭之前调用。当AutoCloseWindows或SetAutoClose设置为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

  • .onGUIEnd:在安装程序窗口关闭后被调用,可以在这个函数里释放各种插件。

  • .onMouseOverSection:鼠标在Section节点间移动式调用。

Function .onMouseOverSection
    FindWindow $R0 "#32770" "" $HWNDPARENT
    GetDlgItem $R0 $R0 1043 	#获取显示Section节点描述的对象。

    StrCmp $0 0 "" +2
      SendMessage $R0 ${WM_SETTEXT} 0 "STR:first section description"	#设置第一个Section的描述内容

    StrCmp $0 1 "" +2
      SendMessage $R0 ${WM_SETTEXT} 0 "STR:second section description"	#设置第二个Section的描述内容
FunctionEnd

  • .onRebootFailed:Reboot失败后调用。

Function .onRebootFailed
   MessageBox MB_OK|MB_ICONSTOP "Reboot failed. Please reboot manually." /SD IDOK
FunctionEnd

  • .onSelChange:组件页面上选择更改时调用,与SectionSetFlags和SectionGetFlags一起使用。修改后的Section id存储在$0中,安装类型改变时$0=-1,且会收到通知。

  • .onUserAbort:点击“取消”时调用。此时安装尚未成功或失败,在这个函数里调用Abort,安装不会被终止。

Function .onUserAbort
   MessageBox MB_YESNO "Abort install?" IDYES NoCancelAbort
     Abort 				#此时,安装不会被终止
   NoCancelAbort:
 FunctionEnd

  • .onVerifyINsDir:用户更改安装目录时都会调用此代码。在这个函数里调用Abort则认为安装路径($INSTDIR)无效,而非终止。

Function .onVerifyInstDir
    IfFileExists $INSTDIR\Winamp.exe PathGood
      Abort 		#表示选择的安装路径无效,无法进行下一步。
    PathGood:
  FunctionEnd

  • un.onGUIInit:加载第一个卸载页面和显示卸载程序框之前被调用。允许调整用户界面。参考.onGUIInit回调。

  • un.onInit:卸载程序几乎完成初始化时调用。这里调用Abort则终止卸载。且在这个函数里可以修改$INSTDIR变量。

  • un.onUninstFailed:卸载失败且点击“取消”时调用。

  • un.onUninstSucess:卸载成功,窗口关闭之前调用。当AutoCloseWindows或SetAutoClose设置为false是,可能在关闭窗口之后调用。

  • un.onGUIEnd:卸载程序窗口关闭后调用,可以释放插件。

  • un.onRebootFailed:Reboot失败后调用

  • un.onSelChange:组件页上的选择更改时调用。用于与SectionSetFlags和SectionGetFlags一起使用。修改后的Section id存储在$0中,安装类型改变时$0=-1,且会收到通知。

  • un.onUserAbort:用户点击“取消”按钮并且卸载还没有失败时被调用

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多