简体   繁体   中英

Checkbox in NSIS

I am looking to add a basic checkbox to the default directory page using MUI_PAGE_CUSTOMFUNCTION_SHOW in NSIS. However, the checkbox doesn't seem to be appearing or functioning in any way. Here are the different functions I have attempted; I'm not sure if I need to grab the current window in order to draw to it? Any advice would be much appreciated.

!define MUI_PAGE_CUSTOMFUNCTION_SHOW "DirectoryShow"
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE "DirectoryLeave"
!insertmacro MUI_PAGE_DIRECTORY

Var Checkbox

Function DirectoryShow
  ${NSD_CreateCheckbox} 0 0 50% 6% "CheckboxTest"
  Pop $Checkbox
  ${NSD_Check} $Checkbox
FunctionEnd

Function DirectoryLeave
  ${NSD_GetState} $Checkbox $0
  ${If} $0 <> 0
    MessageBox mb_ok "Checkbox checked."
  ${EndIf}
FunctionEnd

NSD_Create* is only supported on nsDialog pages (Welcome and Finish MUI pages) but not on a internal NSIS page like Directory.

To create a control on internal pages you must manually call the CreateWindowEx Windows API function. Some of the other NSD helper macros can still be used on these controls:

!include MUI2.nsh
!include nsDialogs.nsh
!define MUI_PAGE_CUSTOMFUNCTION_SHOW "DirectoryShow"
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE "DirectoryLeave"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English

Var Checkbox
Var CheckState ; Stored globally so we remember the choice if the user presses the back button and goes back to our page
!define CheckHeight 28

!ifmacrondef _Z= 
!error "NSIS 2.51 or 3.0rc1 or later required!"
!endif
!macro CreateNativeControl hParent cls style exstyle x y w h text ; Note: Only supports pixel coordinates
System::Call 'USER32::CreateWindowEx(i ${exstyle}, t "${cls}", ts, i ${style}, i ${x}, i ${y}, i ${w}, i ${h}, p ${hParent}, i0, i0, i0)p.s' "${text}"
!macroend

Function DirectoryShow
  ; Create some extra space by reducing the height of the top text:
  System::Call *(i,i,i,i)p.r0 ; NSIS 2.51+
  System::Call 'USER32::GetWindowRect(p$mui.DirectoryPage.Text, pr0)'
  System::Call 'USER32::MapWindowPoints(i0,p$mui.DirectoryPage,p$0,i2)'
  System::Call '*$0(i.r2,i.r3,i.r4,i.r5)'
  System::Free $0
  IntOp $5 $5 - ${CheckHeight}
  System::Call 'USER32::SetWindowPos(i$mui.DirectoryPage.Text,i,i,i,i$4,i$5,i0x6)'

  ; Create and initialize the checkbox
  IntOp $5 $3 + $5 ; y = TextTop + TextHeight
  !insertmacro CreateNativeControl $mui.DirectoryPage ${__NSD_CheckBox_CLASS} "${__NSD_CheckBox_STYLE}" "${__NSD_CheckBox_EXSTYLE}" 0 $5 300 ${CheckHeight} "CheckboxTest"
  Pop $Checkbox
  SendMessage $mui.DirectoryPage ${WM_GETFONT} 0 0 $0 ; Get the dialog font
  SendMessage $Checkbox ${WM_SETFONT} $0 1 ; and apply it to the new control
  System::Call 'USER32::SetWindowPos(i$Checkbox,i0,i,i,i,i,i0x33)' ; Make sure it is on top of the tab order
  ${IfThen} $CheckState == "" ${|} StrCpy $CheckState 1 ${|} ; Set the default if this is our first time
  ${NSD_SetState} $Checkbox $CheckState
FunctionEnd

Function DirectoryLeave
  ${NSD_GetState} $Checkbox $CheckState
  ${If} $CheckState <> 0
    MessageBox mb_ok "Checkbox checked."
  ${EndIf}
FunctionEnd

Alternatively, instead of creating the control at run-time you can just modify the actual page (...\\NSIS\\Contrib\\UIs\\modern.exe) with Resource Hacker and apply the new UI file with MUI_UI ( ChangeUI )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM