2015-09-16 43 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