2012-09-05 129 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