简体   繁体   中英

NSIS ListView - Is possible to set checkbox as “disabled”?


I used ListView using the checkboxes in my win installer. Is any possibility to disable given checkbox (not possible to un/check)?

Thank you.

在此处输入图片说明

Windows itself does not actually have more than two states for the checkboxes in a listview. To get more states you would have to draw them yourself with the System plug-in in NSIS.

Another limitation is that the nsDialogs plug-in does not support return values when handling the WM_NOTIFY message so you cannot block state changes by handling LVN_ITEMCHANGING . You can however catch LVN_ITEMCHANGED and reapply the original state:

!include LogicLib.nsh
!include nsDialogs.nsh
!include WinMessages.nsh ; The 3rd-party CommCtrl.nsh file is buggy, include this first to make sure LVM_* is defined correctly
!ifndef LVM_GETITEMTEXT
!define /math LVM_GETITEMTEXTA ${LVM_FIRST} + 45
!define /math LVM_GETITEMTEXTW ${LVM_FIRST} + 115
${_NSIS_DEFAW} LVM_GETITEMTEXT
!endif
!include "CommCtrl.nsh"
!define /ifndef _COMMCTRL_NSH_VERBOSE ${_COMMCTRL_VERBOSE}


Page Custom MyPage

Var AllowCheckboxChanges

Function MyPage
nsDialogs::Create 1018
Pop $0

${NSD_CreateListView} 0u 0u 100% 100% "Listview"
Pop $0
SendMessage $0 ${LVM_SETEXTENDEDLISTVIEWSTYLE} 0 ${LVS_EX_CHECKBOXES}
IntOp $AllowCheckboxChanges $AllowCheckboxChanges + 1 ; Allow changes while we initialize
${NSD_OnNotify} $0 OnListNotify
${NSD_LV_InsertColumn} $0 0 200 "column 0"
${NSD_LV_InsertItem} $0 9 'Foo' ; read-only
${NSD_LV_SetCheckState} $0 0 1
${NSD_LV_InsertItem} $0 9 'Bar'
${NSD_LV_InsertItem} $0 9 'Baz' ; read-only
IntOp $AllowCheckboxChanges $AllowCheckboxChanges - 1 ; Block changes from now on

nsDialogs::Show
FunctionEnd

Function OnListNotify
Pop $1 ; HWND
Pop $2 ; Code
Pop $3 ; NMHDR*
${If} $2 = ${LVN_ITEMCHANGED}
${AndIf} $AllowCheckboxChanges = 0
    System::Call "*$3(p,p,i,i.r2,i,i.r5,i.r4,i,&i8,p)" ; NMLISTVIEW
    ; Block changes to items 0 and 2
    ${If} $2 = 0
    ${OrIf} $2 = 2
        IntOp $4 $4 & ${LVIS_STATEIMAGEMASK}
        IntOp $5 $5 & ${LVIS_STATEIMAGEMASK}
        IntOp $4 $4 >> 12
        IntOp $5 $5 >> 12
        ${If} $4 <> $5
            IntOp $4 $4 - 1
            IntOp $AllowCheckboxChanges $AllowCheckboxChanges - 1 ; Runs out of stack if we don't block recursive NSD_LV_SetCheckState messages
            ${NSD_LV_SetCheckState} $1 $2 $4
            IntOp $AllowCheckboxChanges $AllowCheckboxChanges + 1
        ${EndIf}
    ${EndIf}
${EndIf}
FunctionEnd

If you go the extra step and draw your own checkboxes then you could just look at the state image index to see if the item is read-only or not.

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