有些时候,我们需要让用户设置多个安装目录,如果大家用过 Delphi 就知道了,安装 Delphi 的时候我们可以选择为不同功能的程序(例如共享文件目录,主程序目录,数据库设置程序目录等等)设置不同的安装目录,而这样的功能怎么实现呢,以下为一个很好的例子脚本
!include "MUI.nsh" Name "Test App" OutFile "test.exe" !insertmacro MUI_PAGE_COMPONENTS Page custom SetCustom LeaveCustom !insertmacro MUI_PAGE_INSTFILES !insertmacro MUI_LANGUAGE "SimpChinese" ;-------------------------------- Section "SectionA" SecA ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 4" State MessageBox MB_OK "SectionA 的安装路径为:$0" SectionEnd Section "SectionB" SecB ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 5" State MessageBox MB_OK "SectionB 的安装路径为:$0" SectionEnd Section "SectionC" SecC ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 6" State MessageBox MB_OK "SectionC 的安装路径为:$0" SectionEnd Function .Oninit InitPluginsDir File /oname=$PLUGINSDIR\test.ini ".\test.ini" FunctionEnd Function SetCustom ; 判断勾选的组件,并把未勾选组件的安装路径控件设为不可用 SectionGetFlags ${SecA} $0 StrCmp $0 0 0 +2 WriteINIStr "$PLUGINSDIR\test.ini" "Field 4" "Flags" "Disabled" StrCmp $0 1 0 +2 ; 如果组件勾选了,还需要去掉 Disabled,这两行代码不能省略 WriteINIStr "$PLUGINSDIR\test.ini" "Field 4" "Flags" "" SectionGetFlags ${SecB} $0 StrCmp $0 0 0 +2 WriteINIStr "$PLUGINSDIR\test.ini" "Field 5" "Flags" "Disabled" StrCmp $0 1 0 +2 WriteINIStr "$PLUGINSDIR\test.ini" "Field 5" "Flags" "" SectionGetFlags ${SecC} $0 StrCmp $0 0 0 +2 WriteINIStr "$PLUGINSDIR\test.ini" "Field 6" "Flags" "Disabled" StrCmp $0 1 0 +2 WriteINIStr "$PLUGINSDIR\test.ini" "Field 6" "Flags" "" ; 预定义组件安装路径 WriteINIStr "$PLUGINSDIR\test.ini" "Field 4" "State" "$ProgramFiles" WriteINIStr "$PLUGINSDIR\test.ini" "Field 5" "State" "$DeskTop" WriteINIStr "$PLUGINSDIR\test.ini" "Field 6" "State" "$WinDir" InstallOptions::initDialog /NOUNLOAD "$PLUGINSDIR\test.ini" !insertmacro MUI_HEADER_TEXT "选择各组件的安装路径" "必须输入有效路径" InstallOptions::show Pop $R0 FunctionEnd Function LeaveCustom ; 判断用户输入的路径是否合法。 ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 4" "State" StrCmp $0 "" +2 IfFileExists "$0\*" +3 MessageBox MB_OK|MB_ICONSTOP "组件 A 的安装路径无效!" Abort ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 5" "State" StrCmp $0 "" +2 IfFileExists "$0\*" +3 MessageBox MB_OK|MB_ICONSTOP "组件 B 的安装路径无效!" Abort ReadINIStr $0 "$PLUGINSDIR\test.ini" "Field 6" "State" StrCmp $0 "" +2 IfFileExists "$0\*" +3 MessageBox MB_OK|MB_ICONSTOP "组件 C 的安装路径无效!" Abort FunctionEnd ;test.Ini file generated by the HM NIS Edit IO designer. [Settings] NumFields=6 [Field 1] Type=Label Text=A 组件安装路径: Left=8 Right=68 Top=6 Bottom=13 [Field 2] Type=Label Text=B 组件安装路径: Left=5 Right=65 Top=44 Bottom=51 [Field 3] Type=Label Text=C 组件安装路径: Left=8 Right=68 Top=82 Bottom=89 [Field 4] Type=DirRequest Left=14 Right=253 Top=19 Bottom=32 [Field 5] Type=DirRequest Left=14 Right=254 Top=57 Bottom=70 [Field 6] Type=DirRequest Left=14 Right=254 Top=94 Bottom=107