NsDialogs FAQ: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 25: Line 25:
Pop $button
Pop $button
EnableWindow $button 0 # start out disabled
EnableWindow $button 0 # start out disabled
nsDialogs::Show
 
nsDialogs::Show
FunctionEnd
FunctionEnd


Line 61: Line 62:
Function pre
Function pre
nsDialogs::Create 1018
nsDialogs::Create 1018
Pop $dialog
Pop $dialog
${NSD_CreateCheckbox} 0 0 50% 6% "Show button below"
${NSD_CreateCheckbox} 0 0 50% 6% "Show button below"
Pop $hwnd
Pop $hwnd
Line 69: Line 70:
Pop $button
Pop $button
ShowWindow $button ${SW_HIDE} # start out hidden
ShowWindow $button ${SW_HIDE} # start out hidden
nsDialogs::Show
 
nsDialogs::Show
FunctionEnd
FunctionEnd


Line 79: Line 81:
${Else}
${Else}
ShowWindow $button ${SW_HIDE}
ShowWindow $button ${SW_HIDE}
${EndIf}
FunctionEnd
Section ""
SectionEnd
</highlight-nsis>
== How to create two groups of RadioButtons ==
A. Use ${NSD_AddStyle} to add the WS_GROUP style to the first radiobutton of each group.
In Windows' UI handling, all radiobuttons are considered to be part of the same group if no group starter is defined.
Therefore, to have two radiobutton groups of two radiobuttons each, you must specify a group starter for each, otherwise the third and fourth radiobuttons will be considered part of the first group.
Setting a group starter is easy using ${NSD_AddStyle}, use:
<highlight-nsis>
${NSD_AddStyle} $hwnd ${WS_GROUP} # WS_GROUP is defined in winmessages.nsh
</higlight-nsis>
example:
<highlight-nsis>
!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
Page Custom pre
var dialog
var hwnd
Function pre
nsDialogs::Create 1018
Pop $dialog
${NSD_CreateRadioButton} 0 0 40% 6% "Group 1, Radio 1"
Pop $hwnd
${NSD_AddStyle} $hwnd ${WS_GROUP}
${NSD_CreateRadioButton} 0 12% 40% 6% "Group 1, Radio 2"
Pop $hwnd
${NSD_CreateRadioButton} 50% 0 40% 6% "Group 2, Radio 1"
Pop $hwnd
${NSD_AddStyle} $hwnd ${WS_GROUP}
${NSD_CreateRadioButton} 50% 12% 40% 6% "Group 2, Radio 2"
Pop $hwnd
nsDialogs::Show
FunctionEnd
Section ""
SectionEnd
</highlight-nsis>
== How to easily handle radiobutton selections ==
A. Often you do not want to specify separate OnClick functions for each radiobutton...
<highlight-nsis>
!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
Page Custom pre
var dialog
var hwnd
Function pre
nsDialogs::Create 1018
Pop $dialog
${NSD_CreateRadioButton} 0 0 40% 6% "Group 1, Radio 1"
Pop $hwnd
${NSD_AddStyle} $hwnd ${WS_GROUP}
${NSD_OnClick} $hwnd Group1Radio1Click
${NSD_CreateRadioButton} 0 12% 40% 6% "Group 1, Radio 2"
Pop $hwnd
${NSD_OnClick} $hwnd Group1Radio2Click
nsDialogs::Show
FunctionEnd
Function Group1Radio1Click
Pop $hwnd
MessageBox MB_OK "onClick:Group1Radio1"
FunctionEnd
Function Group1Radio2Click
Pop $hwnd
MessageBox MB_OK "onClick:Group1Radio2"
FunctionEnd
Section ""
SectionEnd
</highlight-nsis>
But the only obvious alternative seems to be to place each in a different variable and comparing the hwnd on the stack when the callback function is called to that variable.
<highlight-nsis>
!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
Page Custom pre
var dialog
var hwnd
var Group1Radio1
var Group1Radio2
Function pre
nsDialogs::Create 1018
Pop $dialog
${NSD_CreateRadioButton} 0 0 40% 6% "Group 1, Radio 1"
Pop $Group1Radio1
${NSD_AddStyle} $Group1Radio1 ${WS_GROUP}
${NSD_OnClick} $Group1Radio1 RadioClick
${NSD_CreateRadioButton} 0 12% 40% 6% "Group 1, Radio 2"
Pop $Group1Radio2
${NSD_OnClick} $Group1Radio2 RadioClick
nsDialogs::Show
FunctionEnd
Function RadioClick
Pop $hwnd
${If} $hwnd == $Group1Radio1
    MessageBox MB_OK "onClick:Group1Radio1"
${ElseIf} $hwnd == $Group1Radio2
    MessageBox MB_OK "onClick:Group1Radio2"
${EndIf}
FunctionEnd
Section ""
SectionEnd
</highlight-nsis>
However, you can eliminate both by making use of the getUserData and setUserData commands of nsDialogs.  Using these *UserData commands, you can store data in a control and easily retrieve this later.
To simplify the use of these commands, we'll be using the header from [[NsDialogs UserData]].
<highlight-nsis>
!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
Page Custom pre
var dialog
var hwnd
Function pre
nsDialogs::Create 1018
Pop $dialog
${NSD_CreateRadioButton} 0 0 40% 6% "Group 1, Radio 1"
Pop $hwnd
${NSD_AddStyle} $hwnd ${WS_GROUP}
${NSD_SetUserData} $hwnd "Group1Radio1"
${NSD_OnClick} $hwnd RadioClick
${NSD_CreateRadioButton} 0 12% 40% 6% "Group 1, Radio 2"
Pop $hwnd
${NSD_SetUserData} $hwnd "Group1Radio2"
${NSD_OnClick} $hwnd RadioClick
nsDialogs::Show
FunctionEnd
Function RadioClick
Pop $hwnd
${NSD_GetUserData} $hwnd $0
${If} $0 == "Group1Radio1"
    MessageBox MB_OK "onClick:Group1Radio1"
${ElseIf} $0 == "Group1Radio2"
    MessageBox MB_OK "onClick:Group1Radio2"
${EndIf}
FunctionEnd
Section ""
SectionEnd
</highlight-nsis>
Using this method, you can easily set a variable to an internal string stored on the radiobutton control without any If-ElseIf-EndIf or Case selections in the OnClick event function at all.
<highlight-nsis>
!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
Page Custom pre post
var dialog
var hwnd
var minor
Function pre
nsDialogs::Create 1018
Pop $dialog
${NSD_CreateRadioButton} 0 0 40% 6% "I am 14 years of age or older"
Pop $hwnd
${NSD_AddStyle} $hwnd ${WS_GROUP}
${NSD_SetUserData} $hwnd "false"
${NSD_OnClick} $hwnd RadioClick
${NSD_CreateRadioButton} 0 12% 40% 6% "I am younger than 14 of age"
Pop $hwnd
${NSD_SetUserData} $hwnd "true"
${NSD_OnClick} $hwnd RadioClick
nsDialogs::Show
FunctionEnd
Function RadioClick
Pop $hwnd
${NSD_GetUserData} $hwnd $minor
FunctionEnd
Function post
${If} $minor == ""
    MessageBox MB_OK "Please specify your age group"
    Abort
${ElseIf} $minor == true
    MessageBox MB_OK "installation will continue with content appropriate for your age"
${Else}
    MessageBox MB_OK "installation will continue normally"
${EndIf}
${EndIf}
FunctionEnd
FunctionEnd

Revision as of 22:14, 13 February 2009

How to Enable/Disable a control

A. Use the standard NSIS EnableWindow command.

NSDialogs lets you pop the hwnd of a control created via ${NSD_Create*}. EnableWindow takes a hwnd as one of its parameters. With this, you can easily enable/disable a control.

!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
 
Page Custom pre
 
var dialog
var hwnd
var button
 
Function pre
	nsDialogs::Create 1018
	Pop $dialog
	${NSD_CreateCheckbox} 0 0 50% 6% "Enable button below"
		Pop $hwnd
		${NSD_OnClick} $hwnd EnDisableButton
 
	${NSD_CreateButton} 25% 25% 50% 50% "Hello World"
		Pop $button
		EnableWindow $button 0 # start out disabled
 
	nsDialogs::Show
FunctionEnd
 
Function EnDisableButton
	Pop $hwnd
	${NSD_GetState} $hwnd $0
	${If} $0 == 1
		EnableWindow $button 1
	${Else}
		EnableWindow $button 0
	${EndIf}
FunctionEnd
 
Section ""
SectionEnd

How to Show/Hide a control

A. Use the standard NSIS ShowWindow command.

NSDialogs lets you pop the hwnd of a control created via ${NSD_Create*}. ShowWindow takes a hwnd as one of its parameters. With this, you can easily show/hide a control.

!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
 
Page Custom pre
 
var dialog
var hwnd
var button
 
Function pre
	nsDialogs::Create 1018
		Pop $dialog
	${NSD_CreateCheckbox} 0 0 50% 6% "Show button below"
		Pop $hwnd
		${NSD_OnClick} $hwnd EnDisableButton
 
	${NSD_CreateButton} 25% 25% 50% 50% "Hello World"
		Pop $button
		ShowWindow $button ${SW_HIDE} # start out hidden
 
	nsDialogs::Show
FunctionEnd
 
Function EnDisableButton
	Pop $hwnd
	${NSD_GetState} $hwnd $0
	${If} $0 == 1
		ShowWindow $button ${SW_SHOW}
	${Else}
		ShowWindow $button ${SW_HIDE}
	${EndIf}
FunctionEnd
 
Section ""
SectionEnd

How to create two groups of RadioButtons

A. Use ${NSD_AddStyle} to add the WS_GROUP style to the first radiobutton of each group. In Windows' UI handling, all radiobuttons are considered to be part of the same group if no group starter is defined.

Therefore, to have two radiobutton groups of two radiobuttons each, you must specify a group starter for each, otherwise the third and fourth radiobuttons will be considered part of the first group.

Setting a group starter is easy using ${NSD_AddStyle}, use:

${NSD_AddStyle} $hwnd ${WS_GROUP} # WS_GROUP is defined in winmessages.nsh
</higlight-nsis>
 
example:
<highlight-nsis>
!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
 
Page Custom pre
 
var dialog
var hwnd
 
Function pre
	nsDialogs::Create 1018
		Pop $dialog
	${NSD_CreateRadioButton} 0 0 40% 6% "Group 1, Radio 1"
		Pop $hwnd
		${NSD_AddStyle} $hwnd ${WS_GROUP}
	${NSD_CreateRadioButton} 0 12% 40% 6% "Group 1, Radio 2"
		Pop $hwnd
 
	${NSD_CreateRadioButton} 50% 0 40% 6% "Group 2, Radio 1"
		Pop $hwnd
		${NSD_AddStyle} $hwnd ${WS_GROUP}
	${NSD_CreateRadioButton} 50% 12% 40% 6% "Group 2, Radio 2"
		Pop $hwnd
 
	nsDialogs::Show
FunctionEnd
 
Section ""
SectionEnd

How to easily handle radiobutton selections

A. Often you do not want to specify separate OnClick functions for each radiobutton...

!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
 
Page Custom pre
 
var dialog
var hwnd
 
Function pre
	nsDialogs::Create 1018
		Pop $dialog
	${NSD_CreateRadioButton} 0 0 40% 6% "Group 1, Radio 1"
		Pop $hwnd
		${NSD_AddStyle} $hwnd ${WS_GROUP}
		${NSD_OnClick} $hwnd Group1Radio1Click
	${NSD_CreateRadioButton} 0 12% 40% 6% "Group 1, Radio 2"
		Pop $hwnd
		${NSD_OnClick} $hwnd Group1Radio2Click
 
	nsDialogs::Show
FunctionEnd
 
Function Group1Radio1Click
	Pop $hwnd
	MessageBox MB_OK "onClick:Group1Radio1"
FunctionEnd
Function Group1Radio2Click
	Pop $hwnd
	MessageBox MB_OK "onClick:Group1Radio2"
FunctionEnd
 
Section ""
SectionEnd

But the only obvious alternative seems to be to place each in a different variable and comparing the hwnd on the stack when the callback function is called to that variable.

!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
 
Page Custom pre
 
var dialog
var hwnd
 
var Group1Radio1
var Group1Radio2
 
Function pre
	nsDialogs::Create 1018
		Pop $dialog
	${NSD_CreateRadioButton} 0 0 40% 6% "Group 1, Radio 1"
		Pop $Group1Radio1
		${NSD_AddStyle} $Group1Radio1 ${WS_GROUP}
		${NSD_OnClick} $Group1Radio1 RadioClick
	${NSD_CreateRadioButton} 0 12% 40% 6% "Group 1, Radio 2"
		Pop $Group1Radio2
		${NSD_OnClick} $Group1Radio2 RadioClick
 
	nsDialogs::Show
FunctionEnd
 
Function RadioClick
	Pop $hwnd
	${If} $hwnd == $Group1Radio1
	    MessageBox MB_OK "onClick:Group1Radio1"
	${ElseIf} $hwnd == $Group1Radio2
	    MessageBox MB_OK "onClick:Group1Radio2"
	${EndIf}
FunctionEnd
 
Section ""
SectionEnd

However, you can eliminate both by making use of the getUserData and setUserData commands of nsDialogs. Using these *UserData commands, you can store data in a control and easily retrieve this later.

To simplify the use of these commands, we'll be using the header from NsDialogs UserData.

!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
 
Page Custom pre
 
var dialog
var hwnd
 
Function pre
	nsDialogs::Create 1018
		Pop $dialog
	${NSD_CreateRadioButton} 0 0 40% 6% "Group 1, Radio 1"
		Pop $hwnd
		${NSD_AddStyle} $hwnd ${WS_GROUP}
		${NSD_SetUserData} $hwnd "Group1Radio1"
		${NSD_OnClick} $hwnd RadioClick
	${NSD_CreateRadioButton} 0 12% 40% 6% "Group 1, Radio 2"
		Pop $hwnd
		${NSD_SetUserData} $hwnd "Group1Radio2"
		${NSD_OnClick} $hwnd RadioClick
 
	nsDialogs::Show
FunctionEnd
 
Function RadioClick
	Pop $hwnd
	${NSD_GetUserData} $hwnd $0
	${If} $0 == "Group1Radio1"
	    MessageBox MB_OK "onClick:Group1Radio1"
	${ElseIf} $0 == "Group1Radio2"
	    MessageBox MB_OK "onClick:Group1Radio2"
	${EndIf}
FunctionEnd
 
Section ""
SectionEnd

Using this method, you can easily set a variable to an internal string stored on the radiobutton control without any If-ElseIf-EndIf or Case selections in the OnClick event function at all.

!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
OutFile "test.exe"
 
Page Custom pre post
 
var dialog
var hwnd
var minor
 
Function pre
	nsDialogs::Create 1018
		Pop $dialog
 
	${NSD_CreateRadioButton} 0 0 40% 6% "I am 14 years of age or older"
		Pop $hwnd
		${NSD_AddStyle} $hwnd ${WS_GROUP}
		${NSD_SetUserData} $hwnd "false"
		${NSD_OnClick} $hwnd RadioClick
	${NSD_CreateRadioButton} 0 12% 40% 6% "I am younger than 14 of age"
		Pop $hwnd
		${NSD_SetUserData} $hwnd "true"
		${NSD_OnClick} $hwnd RadioClick
 
	nsDialogs::Show
FunctionEnd
 
Function RadioClick
	Pop $hwnd
	${NSD_GetUserData} $hwnd $minor
FunctionEnd
 
Function post
	${If} $minor == ""
	    MessageBox MB_OK "Please specify your age group"
	    Abort
	${ElseIf} $minor == true
	    MessageBox MB_OK "installation will continue with content appropriate for your age"
	${Else}
	    MessageBox MB_OK "installation will continue normally"
	${EndIf}
FunctionEnd
 
Section ""
SectionEnd