杉宫竹苑工作室

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

NSIS实现多目录安装设置

[复制链接]
发表于 2016-9-2 15:03:54 | 显示全部楼层 |阅读模式

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

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

x

  1. !include "MUI.nsh"
  2. OutFile "test.exe"

  3. !define MUI_PAGE_CUSTOMFUNCTION_Pre ComponentsPre
  4. !insertmacro MUI_PAGE_COMPONENTS
  5. !insertmacro MUI_PAGE_INSTFILES

  6. !insertmacro MUI_LANGUAGE "Simpchinese"

  7. Section "组件A" SecA
  8. SectionEnd

  9. Section "组件B" SecB
  10. SectionEnd

  11. Section "组件C" SecC
  12. SectionEnd

  13. Function ComponentsPre
  14. ; 如果注册表 HKEY_CURRENT_USER\Software\Test 下存在 SecA 键,且其值为 0 ,那么第一个组件默认就不勾选。
  15. ; 以下同。
  16. ReadRegStr $0 HKCU "Software\Test" "SecA"
  17. IfErrors +2
  18. StrCmp $0 0 0 +2
  19. SectionSetFlags ${SecA} 0

  20. ReadRegStr $0 HKCU "Software\Test" "SecB"
  21. IfErrors +2
  22. StrCmp $0 0 0 +2
  23. SectionSetFlags ${SecB} 0

  24. ReadRegStr $0 HKCU "Software\Test" "SecC"
  25. IfErrors +2
  26. StrCmp $0 0 0 +2
  27. SectionSetFlags ${SecC} 0

  28. Functionend
复制代码
如何使用NSIS实现多目录安装设置
有些时候,我们需要让用户设置多个安装目录,如果大家用过 Delphi 就知道了,安装 Delphi 的时候我们可以选择为不同功能的程序(例如共享文件目录,主程序目录,数据库设置程序目录等等)设置不同的安装目录,而这样的功能怎么实现呢,以下为一个很好的例子脚本。
  1. !include "MUI.nsh"

  2. Name "Test App"
  3. OutFile "test.exe"

  4. !insertmacro MUI_PAGE_COMPONENTS
  5. Page custom SetCustom LeaveCustom
  6. !insertmacro MUI_PAGE_INSTFILES

  7. !insertmacro MUI_LANGUAGE "SimpChinese"

  8. ;--------------------------------

  9. Section "SectionA" SecA
  10. ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 4" State
  11. MessageBox MB_OK "SectionA 的安装路径为:$0"
  12. SectionEnd

  13. Section "SectionB" SecB
  14. ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 5" State
  15. MessageBox MB_OK "SectionB 的安装路径为:$0"
  16. SectionEnd

  17. Section "SectionC" SecC
  18. ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 6" State
  19. MessageBox MB_OK "SectionC 的安装路径为:$0"
  20. SectionEnd

  21. Function .Oninit
  22. InitPluginsDir
  23. File /oname=$PLUGINSDIR\test.ini ".\test.ini"
  24. FunctionEnd

  25. Function SetCustom
  26. ; 判断勾选的组件,并把未勾选组件的安装路径控件设为不可用
  27. SectionGetFlags ${SecA} $0
  28. StrCmp $0 0 0 +2
  29. WriteINIStr "$PLUGINSDIR\test.ini" "Field 4" "Flags" "Disabled"
  30. StrCmp $0 1 0 +2 ; 如果组件勾选了,还需要去掉 Disabled,这两行代码不能省略
  31. WriteINIStr "$PLUGINSDIR\test.ini" "Field 4" "Flags" ""

  32. SectionGetFlags ${SecB} $0
  33. StrCmp $0 0 0 +2
  34. WriteINIStr "$PLUGINSDIR\test.ini" "Field 5" "Flags" "Disabled"
  35. StrCmp $0 1 0 +2
  36. WriteINIStr "$PLUGINSDIR\test.ini" "Field 5" "Flags" ""

  37. SectionGetFlags ${SecC} $0
  38. StrCmp $0 0 0 +2
  39. WriteINIStr "$PLUGINSDIR\test.ini" "Field 6" "Flags" "Disabled"
  40. StrCmp $0 1 0 +2
  41. WriteINIStr "$PLUGINSDIR\test.ini" "Field 6" "Flags" ""

  42. ; 预定义组件安装路径
  43. WriteINIStr "$PLUGINSDIR\test.ini" "Field 4" "State" "$ProgramFiles"
  44. WriteINIStr "$PLUGINSDIR\test.ini" "Field 5" "State" "$DeskTop"
  45. WriteINIStr "$PLUGINSDIR\test.ini" "Field 6" "State" "$WinDir"

  46. InstallOptions::initDialog /NOUNLOAD "$PLUGINSDIR\test.ini"
  47. !insertmacro MUI_HEADER_TEXT "选择各组件的安装路径" "必须输入有效路径"
  48. InstallOptions::show
  49. Pop $R0

  50. FunctionEnd

  51. Function LeaveCustom
  52. ; 判断用户输入的路径是否合法。

  53. ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 4" "State"
  54. StrCmp $0 "" +2
  55. IfFileExists "$0\*" +3
  56. MessageBox MB_OK|MB_ICONSTOP "组件 A 的安装路径无效!"
  57. Abort

  58. ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 5" "State"
  59. StrCmp $0 "" +2
  60. IfFileExists "$0\*" +3
  61. MessageBox MB_OK|MB_ICONSTOP "组件 B 的安装路径无效!"
  62. Abort

  63. ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 6" "State"
  64. StrCmp $0 "" +2
  65. IfFileExists "$0\*" +3
  66. MessageBox MB_OK|MB_ICONSTOP "组件 C 的安装路径无效!"
  67. Abort
  68. FunctionEnd
复制代码
Ini 文件代码
  1. ; Ini file generated by the HM NIS Edit IO designer.
  2. [Settings]
  3. NumFields=6

  4. [Field 1]
  5. Type=Label
  6. Text=A 组件安装路径:
  7. Left=8
  8. Right=68
  9. Top=6
  10. Bottom=13

  11. [Field 2]
  12. Type=Label
  13. Text=B 组件安装路径:
  14. Left=5
  15. Right=65
  16. Top=44
  17. Bottom=51

  18. [Field 3]
  19. Type=Label
  20. Text=C 组件安装路径:
  21. Left=8
  22. Right=68
  23. Top=82
  24. Bottom=89

  25. [Field 4]
  26. Type=DirRequest
  27. Left=14
  28. Right=253
  29. Top=19
  30. Bottom=32

  31. [Field 5]
  32. Type=DirRequest
  33. Left=14
  34. Right=254
  35. Top=57
  36. Bottom=70

  37. [Field 6]
  38. Type=DirRequest
  39. Left=14
  40. Right=254
  41. Top=94
  42. Bottom=107
复制代码









回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-17 09:00 , Processed in 0.130456 second(s), 30 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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