2015-09-16 46 views
0
!include "nsDialogs.nsh" 
!include "LogicLib.nsh" 

Name "Test " 
OutFile Setup.exe 

XPStyle on 
Page Custom radioButton radioButtonClick 
Page instfiles 

var Group1Radio1 
var Group1Radio2 
var dialog 
var hwnd 
var label 

Function radioButton 
nsDialogs::Create 1018 
Pop $dialog 
${NSD_CreateLabel} 0 0 100% 6% "Please choose" 
Pop $label 
${NSD_CreateRadioButton} 0 12% 40% 6% "1" 
Pop $Group1Radio1 
${NSD_AddStyle} $Group1Radio1 ${WS_GROUP} 
${NSD_OnClick} $Group1Radio1 radioButtonClick 
${NSD_CreateRadioButton} 0 20% 40% 6% "2" 
Pop $Group1Radio2 
${NSD_OnClick} $Group1Radio2 radioButtonClick 

nsDialogs::Show 
FunctionEnd 

Function radioButtonClick 
Pop $hwnd 
${If} $hwnd == $Group1Radio1 
    ${NSD_CreateLabel} 0 40% 40% 6% "1 Selected" 
    ${NSD_OnChange} $Group1Radio1 radioButton 
${ElseIf} $hwnd == $Group1Radio2 
    ${NSD_CreateLabel} 0 40% 40% 6% "2 Selected" 
    ${NSD_OnChange} $Group1Radio2 radioButton 

${EndIf} 

FunctionEnd 

Section 
SetOutPath "$DESKTOP" 
SectionEnd 

運行此代碼時,進行修正顯示在標籤上選擇的第一個按鈕的名稱中的「功能radioButtonClick」,但是當你選擇別的東西后,它不更新,並且按鈕標籤出來交換。NSIS - 單選框的標籤不匹配的一個選擇

所以基本上,會發生什麼情況是:

點擊單選按鈕,1 - >顯示 「1選擇」 然後, 點擊單選按鈕2 - >什麼也沒有發生。 然後再次點擊 單選按鈕,1 - >顯示「2選擇」 最後,再次 點擊單選按鈕2 - >顯示「1選擇」

我該如何解決這個問題?

預先感謝您。

回答

0

您的代碼沒有什麼意義,每次單選按鈕更改時都會創建一個新標籤,並且您在每次單擊時註冊一個新的NSD_OnChange事件處理函數,並且此處理函數會嘗試在該函數的頂部創建另一個頁面舊的,這是完全不受支持的行爲!

有很多方法來編寫,這裏有3個例子:

!include "nsDialogs.nsh" 
!include "LogicLib.nsh" 

Page Custom radioButtonExamplePageMethod1 
Page Custom radioButtonExamplePageMethod2 
Page Custom radioButtonExamplePageMethod3 
Page instfiles 

var Group1Radio1 
var Group1Radio2 
var dialog 
var hwnd 
var label 

Function radioButtonExamplePageMethod1 
nsDialogs::Create 1018 
Pop $dialog 
${NSD_CreateLabel} 0 0 100% 6% "Please choose" 
Pop $0 ; Don't care about this handle 
${NSD_CreateRadioButton} 0 12% 40% 6% "1" 
Pop $Group1Radio1 
${NSD_OnClick} $Group1Radio1 radioButtonClicked_Method1 
${NSD_CreateRadioButton} 0 20% 40% 6% "2" 
Pop $Group1Radio2 
${NSD_OnClick} $Group1Radio2 radioButtonClicked_Method1 
${NSD_CreateLabel} 0 40% 40% 6% "" 
Pop $label 
; You could do 
; SendMessage $Group1Radio1 ${BM_CLICK} 0 0 
; here to simulate a click for the inital radio button state 
nsDialogs::Show 
FunctionEnd 

Function radioButtonClicked_Method1 
Pop $hwnd 
${If} $hwnd == $Group1Radio1 
    ${NSD_SetText} $label "1 Selected" 
${ElseIf} $hwnd == $Group1Radio2 
    ${NSD_SetText} $label "2 Selected" 
${EndIf} 
FunctionEnd 


Function radioButtonExamplePageMethod2 
nsDialogs::Create 1018 
Pop $dialog 
${NSD_CreateLabel} 0 0 100% 6% "Please choose" 
Pop $0 ; Don't care about this handle 
${NSD_CreateLabel} 0 40% 40% 6% "" 
Pop $label 
${NSD_CreateRadioButton} 0 12% 40% 6% "1" 
Pop $Group1Radio1 
${NSD_OnClick} $Group1Radio1 radioButtonClicked_Method2_Radio1 
${NSD_CreateRadioButton} 0 20% 40% 6% "2" 
Pop $Group1Radio2 
${NSD_OnClick} $Group1Radio2 radioButtonClicked_Method2_Radio2 
nsDialogs::Show 
FunctionEnd 

Function radioButtonClicked_Method2_Radio1 
Pop $0 
${NSD_SetText} $label "1 Selected" 
FunctionEnd 
Function radioButtonClicked_Method2_Radio2 
Pop $0 
${NSD_SetText} $label "2 Selected" 
FunctionEnd 


Function radioButtonExamplePageMethod3 
nsDialogs::Create 1018 
Pop $dialog 
${NSD_CreateLabel} 0 0 100% 6% "Please choose" 
Pop $0 ; Don't care about this handle 
${NSD_CreateLabel} 0 40% 40% 6% "" 
Pop $label 
${NSD_CreateRadioButton} 0 12% 40% 6% "1" 
Pop $Group1Radio1 
nsDialogs::SetUserData $Group1Radio1 "1 Selected" 
${NSD_OnClick} $Group1Radio1 radioButtonClicked_Method3 
${NSD_CreateRadioButton} 0 20% 40% 6% "2" 
Pop $Group1Radio2 
nsDialogs::SetUserData $Group1Radio2 "2 Selected" 
${NSD_OnClick} $Group1Radio2 radioButtonClicked_Method3 
nsDialogs::Show 
FunctionEnd 

Function radioButtonClicked_Method3 
Pop $0 
nsDialogs::GetUserData $0 
Pop $1 
${NSD_SetText} $label $1 
FunctionEnd 
+0

這個偉大的工程!非常感謝! – niamleeson

+0

@niamleeson:不需要說謝謝,你可以將它標記爲答案;) – Anders