NsDialogs FAQ: Difference between revisions
Line 673: | Line 673: | ||
Unfortunately, there are two caveats in this case: | Unfortunately, there are two caveats in this case: | ||
1. The standard Progress Bar is styled by XP styles which override this style. For the same reason you can't have a Marquee progress bar _without_ XP Styles, you can't have a Smooth progress bar _with_ XP Styles (see also [http://nsis.sourceforge.net/Docs/Chapter4.html#4.8.1.46 XPStyle]). | 1. The standard Progress Bar is styled by XP styles which override this style. For the same reason you can't have a Marquee progress bar _without_ XP Styles, you can't have a Smooth progress bar _with_ XP Styles (see also [http://nsis.sourceforge.net/Docs/Chapter4.html#4.8.1.46 XPStyle]). | ||
2. The Smooth style ([http://social.msdn.microsoft.com/Search/en-US?query=PBS_SMOOTH PBS_SMOOTH]) can't be added -after- the control has been created by nsDialogs. So you have to create it with that style already set. | 2. The Smooth style ([http://social.msdn.microsoft.com/Search/en-US?query=PBS_SMOOTH PBS_SMOOTH]) can't be added -after- the control has been created by nsDialogs. So you have to create it with that style already set. | ||
Revision as of 21:02, 2 April 2010
How to Enable/Disable a control
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
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 a multi-line edit (text) control
Although multi-line is a Style of a control, for Edit (Text) controls it is one of few styles that cannot be set once the control has already been created - so you can't use ${NSD_AddStyle}.
Instead, you will have to create the control manually:
!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_CreateText} 0 0 100% 40% "This is NOT a$\r$\nmulti-line$\r$\nedit control" Pop $hwnd nsDialogs::CreateControl EDIT \ "${__NSD_Text_STYLE}|${WS_VSCROLL}|${ES_MULTILINE}|${ES_WANTRETURN}" \ "${__NSD_Text_EXSTYLE}" \ 0 50% 100% 40% \ "This IS a$\r$\nmulti-line$\r$\nedit control" Pop $hwnd nsDialogs::Show FunctionEnd Section "" SectionEnd
Or, much cleaner, using the NsDialogs_CreateTextMultiline header:
!include "nsDialogs.nsh" !include "nsDialogs_createTextMultiline.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_CreateText} 0 0 100% 40% "This is NOT a$\r$\nmulti-line$\r$\nedit control" Pop $hwnd ${NSD_CreateTextMultiline} 0 50% 100% 40% "This IS a$\r$\nmulti-line$\r$\nedit control" Pop $hwnd nsDialogs::Show FunctionEnd Section "" SectionEnd
How to create a Text Password control
See the nsDialogs documentation - a Password control is one of the controls supported by default; ${NSD_CreatePassword}
How to set the character used by a Text Password control
By default a Text Password control will use an asterisk (*****) character to mask the text. You can change this character using SendMessage with the EM_SETPASSWORDCHAR flag.
The password character is identified by its ASCII or UNICODE index; keep in mind that the font you use for your installer may not support the character you wish to use.
NOTE If you set the password character to 0 (zero), the text will not be masked! You can use this to toggle between the text being visible and masked. See also the How to hide/show passwords in a Text Password control entry.
!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_CreatePassword} 0 0 50% 8% "This is a password field" Pop $hwnd SendMessage $hwnd ${EM_SETPASSWORDCHAR} 149 0 # 149 = fat dot nsDialogs::Show FunctionEnd Section "" SectionEnd
How to hide/show passwords in a Text Password control
By using SendMessage with the EM_SETPASSWORDCHAR flag, specifying the character as 0 (zero), you can make a password visible, and invisible again by specifying a non-zero character.
See also the How to set the character used by a Text Password control entry.
NOTE The Password control has to be forcibly redrawn after changing the mask character. This is done by hiding and then showing the control using ShowWindow.
!include "nsDialogs.nsh" !include "winmessages.nsh" !include "logiclib.nsh" OutFile "test.exe" Page Custom pre var dialog var hwnd var passwordControl Function pre nsDialogs::Create 1018 Pop $dialog ${NSD_CreateCheckbox} 0 0 25% 8% "Show password" Pop $hwnd ${NSD_OnClick} $hwnd ShowPassword ${NSD_CreatePassword} 0 10% 50% 8% "This is a password field" Pop $passwordControl nsDialogs::Show FunctionEnd Function ShowPassword Pop $hwnd ${NSD_GetState} $hwnd $0 ShowWindow $passwordControl ${SW_HIDE} ${If} $0 == 1 SendMessage $passwordControl ${EM_SETPASSWORDCHAR} 0 0 ${Else} SendMessage $passwordControl ${EM_SETPASSWORDCHAR} 42 0 ${EndIf} ShowWindow $passwordControl ${SW_SHOW} FunctionEnd Section "" SectionEnd
How to create a Numbers-only Text control
See the nsDialogs documentation - a Numbers-only control is one of the controls supported by default; ${NSD_CreateNumber}
How to create a Read-only Text control
The read-only state of a Text control can be set using SendMessage with the EM_SETREADONLY flag.
Note that a read-only Edit (Text) control is not the same as a disabled Text control. A disabled Text control will display its text grayed out, a read-only Text control remains black on a light grey background. In addition, a disabled multi-line Text control cannot be scrolled. A read-only multi-line Text control, on the other hand, can be scrolled.
!include "nsDialogs.nsh" !include "winmessages.nsh" !include "logiclib.nsh" OutFile "test.exe" Page Custom pre var dialog var hwnd var textControl Function pre nsDialogs::Create 1018 Pop $dialog ${NSD_CreateCheckBox} 0 0 50% 6% "Set text field below read-only" Pop $hwnd ${NSD_OnClick} $hwnd SetReadonly ${NSD_CreateText} 0 12% 100% 8% "Hello World" Pop $textControl nsDialogs::Show FunctionEnd Function SetReadonly Pop $hwnd ${NSD_GetState} $hwnd $0 SendMessage $textControl ${EM_SETREADONLY} $0 0 FunctionEnd Section "" SectionEnd
How to create two groups of RadioButtons
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
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
How to Control a Progress Bar control (using absolute values)
You can control a Progress Bar using sendMessage and the messages for Progress Bar controls. These are _almost all_ defined in the WinMessages.nsh header that comes with NSIS, so be sure to include this header.
There is at least one exception, which is very useful to define yourself because it cuts down on code and makes your source more easily readable, and that is PBM_SETRANGE32. PBM_SETRANGE32 allows you to set the start and end range as simply two numbers, instead of having to use ${MakeLong} from WinDef.nsh as required by PBM_SETRANGE
!define /math PBM_SETRANGE32 ${WM_USER} + 6
In order to update the Progress Bar's value, you'll need to use an nsDialogs timer to invoke a function that handles your actual processing. This timer is only invoked once.
Here's a simple example that creates a Progress Bar that has a 0 (zero) to 100 range for a typical full percentage increase, and sets the progress to 25%, 50%, 75% and 100%, using the Sleep command to simulate processing occurring - normally this might be other NSIS calls or calling external applications/installers before your main installation, etc.
!include "WinMessages.nsh" !include "MUI2.nsh" !include "nsDialogs.nsh" OutFile "test.exe" Section SectionEnd Var dialog Var hwnd Var null !define /math PBM_SETRANGE32 ${WM_USER} + 6 Page Custom page.custom Function page.custom nsDialogs::Create 1018 Pop $dialog ${NSD_CreateProgressBar} 0 0 100% 10% "Test" Pop $hwnd ${NSD_CreateTimer} NSD_Timer.Callback 10 nsDialogs::Show FunctionEnd Function NSD_Timer.Callback ${NSD_KillTimer} NSD_Timer.Callback SendMessage $hwnd ${PBM_SETRANGE32} 0 100 SendMessage $hwnd ${PBM_SETPOS} 25 0 Sleep 2000 SendMessage $hwnd ${PBM_SETPOS} 50 0 Sleep 2000 SendMessage $hwnd ${PBM_SETPOS} 75 0 Sleep 2000 SendMessage $hwnd ${PBM_SETPOS} 100 0 FunctionEnd !insertmacro MUI_LANGUAGE "English"
How to Control a Progress Bar control (using step values)
One of the down sides of the previous example is that you always have to keep track of your percentages, when those percentages might not even be important, as long as the user knows there -is- progress. An example where keeping track of your percentages could become problematic is with copy/pasting chunks of code. You might suddenly find yourself with a progress bar that goes from 0% to 50% back to 25% and then up to 75%.
One pretty simple solution to this is to use a stepped progress bar instead. This method lets you define a step increment value, and simply call a step function to always increment the progress bar by that value. Here is an example using this method, which is functionally the same as the previous example. Note that we're setting the range to the number of steps we're expecting in our function.
!include "WinMessages.nsh" !include "MUI2.nsh" !include "nsDialogs.nsh" OutFile "test.exe" Section SectionEnd Var dialog Var hwnd Var null !define /math PBM_SETRANGE32 ${WM_USER} + 6 Page Custom page.custom Function page.custom nsDialogs::Create 1018 Pop $dialog ${NSD_CreateProgressBar} 0 0 100% 10% "Test" Pop $hwnd ${NSD_CreateTimer} NSD_Timer.Callback 10 nsDialogs::Show FunctionEnd Function NSD_Timer.Callback ${NSD_KillTimer} NSD_Timer.Callback SendMessage $hwnd ${PBM_SETRANGE32} 0 4 SendMessage $hwnd ${PBM_SETSTEP} 1 0 SendMessage $hwnd ${PBM_STEPIT} 0 0 Sleep 2000 SendMessage $hwnd ${PBM_STEPIT} 0 0 Sleep 2000 SendMessage $hwnd ${PBM_STEPIT} 0 0 Sleep 2000 SendMessage $hwnd ${PBM_STEPIT} 0 0 SendMessage $hwnd ${PBM_SETPOS} 4 0 FunctionEnd !insertmacro MUI_LANGUAGE "English"
Note that at the end, we still set the progress to 100%. The reason for this is that you might have some conditional steps in your function. If you forget to step the progress, then the progress value will fall short of 100%. This is an oft-reported user interface issue, where the user complains that even though the process is complete, the progress bar is stuck at less than 100%.
You'll still have to take care to not use -more- steps than you have specified in the PBM_SETRANGE32 call, as any steps after that will just show 100% without any apparent progress.
How to Create a Marquee (endless loop) Progress Bar control
Sometimes you might not know anything about progress - for example, you're simply executing a task that may take some time but there's no way to tell how far along that task you are. In that case you might want to use a so-called Marquee progress bar that simply loops from left to right endlessly.
There is one caveat to this: You can't use a Marquee progress bar unless you use an interface that uses XP styles (see also XPStyle).
Other than that, it's smooth sailing by using ${NSD_AddStyle} to add the PBS_MARQUEE style to the progress bar.
Here's an example of doing exactly that:
!include "WinMessages.nsh" !include "MUI2.nsh" !include "nsDialogs.nsh" OutFile "test.exe" Section SectionEnd Var dialog Var hwnd Var null !define PBS_MARQUEE 0x08 Page Custom page.custom Function page.custom nsDialogs::Create 1018 Pop $dialog ${NSD_CreateProgressBar} 0 0 100% 10% "Test" Pop $hwnd ${NSD_AddStyle} $hwnd ${PBS_MARQUEE} ${NSD_CreateTimer} NSD_Timer.Callback 10 nsDialogs::Show FunctionEnd Function NSD_Timer.Callback ${NSD_KillTimer} NSD_Timer.Callback ; Kill the timer SendMessage $hwnd ${PBM_SETMARQUEE} 1 50 ; start=1|stop=0 interval(ms)=+N _again: MessageBox MB_YESNO "Stop the marquee?" IDNO _again SendMessage $hwnd ${PBM_SETMARQUEE} 0 0 FunctionEnd !insertmacro MUI_LANGUAGE "English"
Note that a Marquee Progress Bar always runs from left to right - you can't set it to any other position.
How to Create a Smooth Progress Bar control
A Smooth Progress Bar is one that is not cut up into small rectangles - i.e. the entire progress is one solid, smooth, bar.
Unfortunately, there are two caveats in this case:
1. The standard Progress Bar is styled by XP styles which override this style. For the same reason you can't have a Marquee progress bar _without_ XP Styles, you can't have a Smooth progress bar _with_ XP Styles (see also XPStyle).
2. The Smooth style (PBS_SMOOTH) can't be added -after- the control has been created by nsDialogs. So you have to create it with that style already set.
Below is an example that uses a Smooth Progress Bar, with XP Styles off.
!include "WinMessages.nsh" !include "MUI2.nsh" !include "nsDialogs.nsh" OutFile "test.exe" Section SectionEnd Var dialog Var hwnd Var null !define PB_EXSTYLE ${WS_EX_WINDOWEDGE}|${WS_EX_CLIENTEDGE} !define PBS_SMOOTH 0x01 !define /math PBM_SETRANGE32 ${WM_USER} + 6 Page Custom page.custom Function page.custom nsDialogs::Create 1018 Pop $dialog nsDialogs::CreateControl "msctls_progress32" \ ${DEFAULT_STYLES}|${PBS_SMOOTH} \ ${PB_EXSTYLE} \ 0 0 100% 10% \ "Test" \ Pop $hwnd ${NSD_CreateTimer} NSD_Timer.Callback 10 nsDialogs::Show FunctionEnd Function NSD_Timer.Callback ${NSD_KillTimer} NSD_Timer.Callback ; Kill the timer SendMessage $hwnd ${PBM_SETRANGE32} 0 100 SendMessage $hwnd ${PBM_SETPOS} 25 0 Sleep 2000 SendMessage $hwnd ${PBM_SETPOS} 50 0 Sleep 2000 SendMessage $hwnd ${PBM_SETPOS} 75 0 Sleep 2000 SendMessage $hwnd ${PBM_SETPOS} 100 0 FunctionEnd !insertmacro MUI_LANGUAGE "English" XPStyle off
How to let controls receive Hotkey (alt+letter) combinations
Only controls that have labels can be assigned a hotkey, simply by including an ampersand in its title. E.g.
${NSD_CreateCheckbox} 3% 3% 20% 8% "The hot&key here is alt+k"
However, when the page is first navigated to in NSIS, the NSIS dialog is what has focus, rather than the inner nsDialogs dialog. So set focus to the nsDialogs dialog before you tell nsDialogs to show it:
nsDialogs::Create 1018 Pop $dialog ; assuming you have a 'dialog' variable! ; further code here SendMessage $dialog ${WM_SETFOCUS} $HWNDPARENT 0 nsDialogs::Show
The NSIS dialog will still handle e.g. alt+N for the Next button as the hotkey event simply 'bubbles up' to the parent window if window with current focus does not react to it. ( Hint: That means you shouldn't set any hotkeys for the B(ack) or N(ext) letters! )
Using these methods, you can easily create an installer that can be navigated entirely and easily by keyboard-only.