杉宫竹苑工作室

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1890|回复: 0

NSIS添加删除功能组件

[复制链接]
发表于 2017-2-23 21:49:25 | 显示全部楼层 |阅读模式

正式会员享受无限制浏览网站功能和高速网盘下载,赶快加入本站吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

x
脚本

下面的例子会根据你在组件页的选择在Windows桌面上创建子目录 "Component One", "Component Two", 等等。
请编译后,多运行几次,通过改变组件的选项,以查看不同的结果。
如果有更好的方法来改进,请和 [url=]ts2001@hotbox.ru[/url]联系。
如果在你的安装程序里使用它:
1、在你的脚本开头复制并粘贴系统宏,或者直接引用文件。
2、在卸载区块前复制并粘贴回调函数,确保你为每个可选项在命令区块指定区块索引的输出常量。
3、在SectionList macro列出你的可选区块,为每个区块使用下面的格式(替换下面的section_index)。
!insertmacro "${MacroName}" "section_index"


4、在每个可选区块后写上 Remove_${section_index}宏,该宏必须描述删除区块。
下面是添加删除功能的一个简单代码。

  1. ;--- Add/Remove system macros: ---
  2. ; (可以调用头文件来代替这些)
  3. Var AR_SecFlags
  4. Var AR_RegFlags

  5. !macro InitSection SecName
  6.   ;  This macro reads component installed flag from the registry and
  7.   ;changes checked state of the section on the components page.
  8.   ;Input: section index constant name specified in Section command.

  9.   ClearErrors
  10.   ;Reading component status from registry
  11.   ReadRegDWORD $AR_RegFlags HKLM \
  12.     "${REG_UNINSTALL}\Components\${SecName}" "Installed"
  13.   IfErrors "default_${SecName}"
  14.     ;Status will stay default if registry value not found
  15.     ;(component was never installed)
  16.   IntOp $AR_RegFlags $AR_RegFlags & 0x0001  ;Turn off all other bits
  17.   SectionGetFlags ${${SecName}} $AR_SecFlags  ;Reading default section flags
  18.   IntOp $AR_SecFlags $AR_SecFlags & 0xFFFE  ;Turn lowest (enabled) bit off
  19.   IntOp $AR_SecFlags $AR_RegFlags | $AR_SecFlags      ;Change lowest bit

  20.   ;Writing modified flags
  21.   SectionSetFlags ${${SecName}} $AR_SecFlags

  22. "default_${SecName}:"
  23. !macroend

  24. !macro FinishSection SecName
  25.   ;  This macro reads section flag set by user and removes the section
  26.   ;if it is not selected.
  27.   ;Then it writes component installed flag to registry
  28.   ;Input: section index constant name specified in Section command.

  29.   SectionGetFlags ${${SecName}} $AR_SecFlags  ;Reading section flags
  30.   ;Checking lowest bit:
  31.   IntOp $AR_SecFlags $AR_SecFlags & 0x0001
  32.   IntCmp $AR_SecFlags 1 "leave_${SecName}"
  33.     ;Section is not selected:
  34.     ;Calling Section uninstall macro and writing zero installed flag
  35.     !insertmacro "Remove_${${SecName}}"
  36.     WriteRegDWORD HKLM "${REG_UNINSTALL}\Components\${SecName}" \
  37.   "Installed" 0
  38.     Goto "exit_${SecName}"

  39. "leave_${SecName}:"
  40.     ;Section is selected:
  41.     WriteRegDWORD HKLM "${REG_UNINSTALL}\Components\${SecName}" \
  42.   "Installed" 1

  43. "exit_${SecName}:"
  44. !macroend

  45. !macro RemoveSection SecName
  46.   ;  This macro is used to call section's Remove_... macro
  47.   ;from the uninstaller.
  48.   ;Input: section index constant name specified in Section command.

  49.   !insertmacro "Remove_${${SecName}}"
  50. !macroend
  51. ;--- End of Add/Remove macros ---


  52. ;  This constant specifies the installer file name.
  53. !define InstFile "AddRemove.exe"
  54. OutFile "${InstFile}"

  55. ;  This constant specifies Windows uninstall key for your application.
  56. !define REG_UNINSTALL "Software\Microsoft\Windows\CurrentVersion\Uninstall\
  57. \AddRemoveExample"

  58. InstallDir "$DESKTOP\AddRemove Example"
  59. Name "Add/Remove Example 1.0"
  60. ComponentText "Check the components you want to add and uncheck \
  61. the components you want to remove:"
  62. ShowInstDetails show
  63. ShowUnInstDetails show


  64. Section "Required Section"
  65. SectionIn RO
  66.   ;This section is required. It can't be removed.

  67.   CreateDirectory $INSTDIR
  68.   WriteUninstaller "$INSTDIR\Uninstall.exe"

  69. ;Writing uninstall info to registry:
  70.   WriteRegStr HKLM "${REG_UNINSTALL}" "DisplayName" "Add/Remove Example"
  71.   WriteRegStr HKLM "${REG_UNINSTALL}" "DisplayIcon" "$INSTDIR\Uninstall.exe"
  72.   WriteRegStr HKLM "${REG_UNINSTALL}" "DisplayVersion" "1.0"
  73.   WriteRegStr HKLM "${REG_UNINSTALL}" "Publisher" "THRaSH"
  74.   WriteRegStr HKLM "${REG_UNINSTALL}" "InstallSource" "$EXEDIR"

  75.   ;Under WinXP this creates two separate buttons: "Modify" and "Remove".
  76.   ;"Modify" will run installer and "Remove" will run uninstaller.
  77.   WriteRegDWord HKLM "${REG_UNINSTALL}" "NoModify" 0
  78.   WriteRegDWord HKLM "${REG_UNINSTALL}" "NoRepair" 0
  79.   WriteRegStr HKLM "${REG_UNINSTALL}" "UninstallString" \
  80. '"$INSTDIR\Uninstall.exe"'
  81.   WriteRegStr HKLM "${REG_UNINSTALL}" "ModifyPath" '"$EXEDIR\${InstFile}"'
  82. SectionEnd

  83. Section "Component One (selected by default)" sec_One
  84.   ;Installs component one
  85.   ;By default this section is selected
  86.   DetailPrint "*** Adding Component One..."
  87.   CreateDirectory "$INSTDIR\Component One"
  88. SectionEnd
  89. !macro Remove_${sec_One}
  90.   ;Removes component one
  91.   DetailPrint "*** Removing Component One..."
  92.   RMDir /r "$INSTDIR\Component One"
  93. !macroend

  94. Section /o "Component Two (unselected by default)" sec_Two
  95.   ;Installs component two
  96.   ;By default this section is not selected
  97.   DetailPrint "*** Adding Component Two..."
  98.   CreateDirectory "$INSTDIR\Component Two"
  99. SectionEnd
  100. !macro Remove_${sec_Two}
  101.   ;Removes component two
  102.   DetailPrint "*** Removing Component Two..."
  103.   RMDir /r "$INSTDIR\Component Two"
  104. !macroend

  105. Section /o "Component Three (unselected by default)" sec_Three
  106.   ;Installs component three
  107.   ;By default this section is not selected
  108.   DetailPrint "*** Adding Component Three..."
  109.   CreateDirectory "$INSTDIR\Component Three"
  110. SectionEnd
  111. !macro Remove_${sec_Three}
  112.   ;Removes component three
  113.   DetailPrint "*** Removing Component Three..."
  114.   RMDir /r "$INSTDIR\Component Three"
  115. !macroend


  116. ;--- Add/Remove callback functions: ---
  117. !macro SectionList MacroName
  118.   ;This macro used to perform operation on multiple sections.
  119.   ;List all of your components in following manner here.

  120.   !insertmacro "${MacroName}" "sec_One"
  121.   !insertmacro "${MacroName}" "sec_Two"
  122.   !insertmacro "${MacroName}" "sec_Three"
  123. !macroend

  124. Function .onInit
  125.   ;Reads components status for registry
  126.   !insertmacro SectionList "InitSection"
  127. FunctionEnd

  128. Section -FinishComponents
  129.   ;Removes unselected components and writes component status to registry
  130.   !insertmacro SectionList "FinishSection"
  131. SectionEnd

  132. Section -Post
  133.   ;Showing the results
  134.   ExecShell "open" "$INSTDIR"
  135. SectionEnd
  136. ;--- End of Add/Remove callback functions ---


  137. Section Uninstall
  138.   ;First removes all optional components
  139.   !insertmacro SectionList "RemoveSection"

  140.   ;Removes directory and registry key:
  141.   RMDIR /r $INSTDIR
  142.   DeleteRegKey HKLM "${REG_UNINSTALL}"
  143. SectionEnd
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SgzyStudio

GMT+8, 2024-5-18 00:23 , Processed in 0.107345 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表