4

我有一个非常简单的 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");
}

谢谢,格雷格。

4

1 回答 1

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
于 2012-09-05T12:14:27.223 回答