NSIS - 验证单选按钮选择

时间:2012-09-05 10:45:45

标签: 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");
}

谢谢, 格雷格。

1 个答案:

答案 0 :(得分:3)

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

这是我在设置中使用的一段代码。我使用一个小宏来在变量中总结所选组件。如果没有,则变量为null。我使用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