NSIS - Radiobutton标签与选定的标签不匹配

时间:2015-09-16 20:47:15

标签: button nsis

!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

当此代码运行时,所选的第一个按钮的名称将更正显示在“Function radioButtonClick”中的标签上,但是当您在此之后选择其他内容时,它不会更新,并且按钮标签会被替换掉

所以基本上,会发生什么:

点击单选按钮1 - >显示“1选中” 然后, 单击单选按钮2 - >什么都没发生。 然后, 再次单击单选按钮1 - >显示“2选中” 最后, 再次单击单选按钮2 - >显示“1选中”

我该如何解决这个问题?

提前谢谢。

1 个答案:

答案 0 :(得分: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