2012-09-05 94 views
4

我有一个非常简单的NSIS脚本,允许用户选择他们想要安装的组件,但如果他们没有选择任何东西,我需要一种说“请选择一个组件”的方式。NSIS - 验证单选按钮选择

这里的脚本:

# Based on the one-section example 
# http://nsis.sourceforge.net/Examples/one-section.nsi 

!include "sections.nsh" 

Name "Humira & You" 
OutFile "Humira & You - September 2012.exe" 
RequestExecutionLevel user 

Page components 
Page instfiles 

Section /o "Rheumatoid Arthritis" P1 
    File "/oname=$pluginsdir\Setup.msi" "setupfiles\Humira and you - Rheumatoid Arthritis.msi" 
SectionEnd 

Section /o "Psoriatic Arthritis" P2 
    File "/oname=$pluginsdir\Setup.msi" "setupfiles\Humira and you - Psoriatic Arthritis.msi" 
SectionEnd 

Section /o "Ankylosing Spondylitis" P3 
    File "/oname=$pluginsdir\Setup.msi" "setupfiles\Humira and you - Ankylosing Spondylitis.msi" 
SectionEnd 

Section /o "Axial Spondyloarthritis" P4 
    File "/oname=$pluginsdir\Setup.msi" "setupfiles\Humira and you - Axial Spondyloarthritis.msi" 
SectionEnd 

Section ; Hidden section that runs the show 
    DetailPrint "Installing selected application..." 
    SetDetailsPrint none 
    ExecWait '"msiexec" /i "$pluginsdir\Setup.msi"' 
    SetDetailsPrint lastused 
SectionEnd 

Function .onInit 
    Initpluginsdir ; Make sure $pluginsdir exists 
    StrCpy $1 ${P2} ;The default 
FunctionEnd 

Function .onSelChange 
!insertmacro StartRadioButtons $1 
    !insertmacro RadioButton ${P1} 
    !insertmacro RadioButton ${P2} 
    !insertmacro RadioButton ${P3} 
    !insertmacro RadioButton ${P4} 
!insertmacro EndRadioButtons 
FunctionEnd 

我身边有一个外观和行为的这个例子来,http://nsis.sourceforge.net/Useful_InstallOptions_and_MUI_macros#Macro:_CHECKBOXCHECKER,但似乎因为我想要的东西过于复杂。有没有办法说在NSIS:

if ($1.selectedIndex > -1) { 
    // continue 
} else { 
    MessageBox.Show("Please select"); 
} 

谢谢, 格雷格。

回答

3

当离开组件页面时,可以使用回调函数来检查是否选择了一个。

这是我在设置中使用的一段代码。我用一个小宏在变量中总结选定的组件。如果没有,则该变量为空。我用的是PageEx块回调函数(如休假回调是第三个,我用前两个人的虚拟函数)

关联到组件页面通过

更换

Page components 

PageEx components 
    PageCallbacks DummyFunc DummyFunc componentsLeave 
PageExEnd 

让您.onSelChange回调来处理独家选择,然后添加到您的脚本的末尾:

!define SECTIONCOUNT 3 ; total -1 
;SaveSections adds one bit to the given variable for each selected component 
!macro SaveSections VAR 
    StrCpy ${VAR} 0 
    ${ForEach} $R0 ${SECTIONCOUNT} 0 - 1 
     IntOp ${VAR} ${VAR} << 1 
     ${If} ${SectionIsSelected} $R0 
      ;${DEBUG} "Section $R0 checked" 
      IntOp ${VAR} ${VAR} + 1 
     ${EndIf} 
    ${Next} 
!macroend 

Function DummyFunc 
FunctionEnd 

Function componentsLeave 
    !insertmacro SaveSections $2 
    ${if} $2 = 0 
     MessageBox MB_OK|MB_ICONEXCLAMATION "Select something !" /sd IDOK 
     Abort 
    ${endif} 
FunctionEnd 
+0

像梦一样工作,谢谢! – gfyans