如何在NSIS中创建sectiongroup所需的section?

如何在NSIS中创建sectiongroup所需的section?,nsis,Nsis,我想在sectiongroup中创建一个section,该section group需要该section group,但如果未选中该group,则整个安装不需要该section。我尝试过“SectionIn R0”,但这使得整个安装都需要该部分,我只希望在他们选择组本身时需要该部分 SectionGroup "group" Section "required for section group" SectionIn RO SectionEnd Section "option

我想在sectiongroup中创建一个section,该section group需要该section group,但如果未选中该group,则整个安装不需要该section。我尝试过“SectionIn R0”,但这使得整个安装都需要该部分,我只希望在他们选择组本身时需要该部分

SectionGroup "group"
  Section "required for section group"
    SectionIn RO
  SectionEnd

  Section "optional"
  SectionEnd
SectionGroupEnd

您不能仅使用属性来执行此操作,您需要一些实际的代码:

!include Sections.nsh
!include LogicLib.nsh

SectionGroup /e "group"
  Section "required for section group" SEC_REQ
    SectionIn RO
  SectionEnd
  Section "optional" SEC_OPT
  SectionEnd
SectionGroupEnd

Function .onSelChange
${If} ${SectionIsSelected} ${SEC_OPT}
    !define /math MYSECTIONFLAGS ${SF_SELECTED} | ${SF_RO}
    !insertmacro SetSectionFlag ${SEC_REQ} ${MYSECTIONFLAGS} 
    !undef MYSECTIONFLAGS
${Else}
    !insertmacro ClearSectionFlag ${SEC_REQ} ${SF_RO}
${EndIf}
FunctionEnd
请注意,取消选中节组本身时存在一个“bug”,要解决这个问题,您需要保持一些全局状态,因为nsis不会告诉您哪个节生成了通知。以下代码具有更好的逻辑:

!include Sections.nsh
!include LogicLib.nsh

SectionGroup /e "group" SEC_GRP
  Section "required for section group" SEC_REQ
    SectionIn RO
  SectionEnd
  Section "optional" SEC_OPT
  SectionEnd
  Section "" PRIVSEC_TOGGLESTATE ;hidden section to keep track of state
  SectionEnd
SectionGroupEnd

Function .onSelChange
!define /math SECFLAGS_SELRO ${SF_SELECTED} | ${SF_RO}
${IfNot} ${SectionIsSelected} ${PRIVSEC_TOGGLESTATE}
${AndIf} ${SectionIsReadOnly} ${SEC_REQ}
    !insertmacro ClearSectionFlag ${SEC_REQ} ${SECFLAGS_SELRO}
${EndIf}
${If} ${SectionIsSelected} ${SEC_OPT}
    !insertmacro SetSectionFlag ${SEC_REQ} ${SECFLAGS_SELRO} 
${Else}
    !insertmacro ClearSectionFlag ${SEC_REQ} ${SF_RO}
${EndIf}
${If} ${SectionIsSelected} ${SEC_REQ}
    !insertmacro SelectSection ${PRIVSEC_TOGGLESTATE}
${Else}
    !insertmacro UnselectSection ${PRIVSEC_TOGGLESTATE}
${EndIf}
!undef SECFLAGS_SELRO
FunctionEnd

您不能仅使用属性来执行此操作,您需要一些实际的代码:

!include Sections.nsh
!include LogicLib.nsh

SectionGroup /e "group"
  Section "required for section group" SEC_REQ
    SectionIn RO
  SectionEnd
  Section "optional" SEC_OPT
  SectionEnd
SectionGroupEnd

Function .onSelChange
${If} ${SectionIsSelected} ${SEC_OPT}
    !define /math MYSECTIONFLAGS ${SF_SELECTED} | ${SF_RO}
    !insertmacro SetSectionFlag ${SEC_REQ} ${MYSECTIONFLAGS} 
    !undef MYSECTIONFLAGS
${Else}
    !insertmacro ClearSectionFlag ${SEC_REQ} ${SF_RO}
${EndIf}
FunctionEnd
请注意,取消选中节组本身时存在一个“bug”,要解决这个问题,您需要保持一些全局状态,因为nsis不会告诉您哪个节生成了通知。以下代码具有更好的逻辑:

!include Sections.nsh
!include LogicLib.nsh

SectionGroup /e "group" SEC_GRP
  Section "required for section group" SEC_REQ
    SectionIn RO
  SectionEnd
  Section "optional" SEC_OPT
  SectionEnd
  Section "" PRIVSEC_TOGGLESTATE ;hidden section to keep track of state
  SectionEnd
SectionGroupEnd

Function .onSelChange
!define /math SECFLAGS_SELRO ${SF_SELECTED} | ${SF_RO}
${IfNot} ${SectionIsSelected} ${PRIVSEC_TOGGLESTATE}
${AndIf} ${SectionIsReadOnly} ${SEC_REQ}
    !insertmacro ClearSectionFlag ${SEC_REQ} ${SECFLAGS_SELRO}
${EndIf}
${If} ${SectionIsSelected} ${SEC_OPT}
    !insertmacro SetSectionFlag ${SEC_REQ} ${SECFLAGS_SELRO} 
${Else}
    !insertmacro ClearSectionFlag ${SEC_REQ} ${SF_RO}
${EndIf}
${If} ${SectionIsSelected} ${SEC_REQ}
    !insertmacro SelectSection ${PRIVSEC_TOGGLESTATE}
${Else}
    !insertmacro UnselectSection ${PRIVSEC_TOGGLESTATE}
${EndIf}
!undef SECFLAGS_SELRO
FunctionEnd

还可以使用SectionSetFlags命令将这些节设置为强制节。只需将其添加到.onInit函数中,如下所示:

SectionGroup "group" Sec01
  Section "required for section group"
  SectionIn RO
  SectionEnd

  Section "optional" Sec02
  SectionEnd
SectionGroupEnd

Function .onInit
  SectionSetFlags ${Sec01} 17
FunctionEnd

这将灰显“组”,但它将按预期进行选择和安装。

您也可以使用SectionSetFlags命令将这些节设置为必填节。只需将其添加到.onInit函数中,如下所示:

SectionGroup "group" Sec01
  Section "required for section group"
  SectionIn RO
  SectionEnd

  Section "optional" Sec02
  SectionEnd
SectionGroupEnd

Function .onInit
  SectionSetFlags ${Sec01} 17
FunctionEnd

这将灰显“组”,但它将按预期进行选择和安装。

比公认的答案简单得多。然而,我认为这是一个错误,你有“Sec01”两次。很好发现-我修正了它。比公认的答案简单得多。然而,我认为这是一个错误,你有“Sec01”两次。很好的发现-我修复了它。