按变量选择部分

时间:2019-09-05 08:37:05

标签: nsis

!include "MUI2.nsh"
!include "FileFunc.nsh"
!include "LogicLib.nsh"
;---------------------------------------------------------------------------
;---------------------------------------------------------------------------
ShowInstDetails show
RequestExecutionLevel admin
;---------------------------------------------------------------------------
Name "Test"
Outfile "Test.exe"
;---------------------------------------------------------------------------
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
;---------------------------------------------------------------------------
!insertmacro MUI_LANGUAGE "English"
;---------------------------------------------------------------------------



LangString DESC_sec1 ${LANG_ENGLISH} "sec1 files"
Section /o "sec1" sec1

SectionEnd

LangString DESC_sec2 ${LANG_ENGLISH} "sec2 files"
Section /o "sec2" sec2

SectionEnd

LangString DESC_sec3 ${LANG_ENGLISH} "sec3 files"
Section /o "sec3" sec3

SectionEnd


Var test
Function .OnInit
  StrCpy $test "sec2"
  !insertmacro SelectSection $test

FunctionEnd

如何在运行时通过节名称选择节? 在示例中,始终选择第一部分(认为是一个错误)

但是如果我这样重写

!insertmacro SelectSection ${sec2}

一切正常...

是否可以从变量中按名称选择部分?

一些长文本一些长文本一些长文本一些长文本 一些长文本一些长文本一些长文本一些长文本

1 个答案:

答案 0 :(得分:0)

部分通过其ID(在编译时设置的数字)进行访问:

Section "Foo"
SectionEnd
Section "Bar" SID_BAR
SectionEnd
Section "Baz"
SectionEnd

Page Components
Page InstFiles

!include Sections.nsh

Function .onInit
  !insertmacro UnselectSection ${SID_BAR}
FunctionEnd

如果要使用显示名称,则必须手动枚举以下部分:

Section "Foo"
SectionEnd
Section "Bar"
SectionEnd
Section "Baz"
SectionEnd

Page Components
Page InstFiles

!macro GetSectionIdFromName name outvar
Push "${name}"
Call GetSectionIdFromName 
Pop ${outvar} ; ID of section, "" if not found
!macroend
Function GetSectionIdFromName 
Exch $1 ; Name
Push $2
Push $3
StrCpy $2 -1
ClearErrors
loop:
    IntOp $2 $2 + 1
    SectionGetText $2 $3
    IfErrors fail
    StrCmp $3 $1 "" loop
    StrCpy $1 $2
    Goto done
fail:
StrCpy $1 ""
done:
Pop $3
Pop $2
Exch $1
FunctionEnd


!include Sections.nsh
!include LogicLib.nsh

Function .onInit
  StrCpy $1 "Bar" ; The name we are looking for
  !insertmacro GetSectionIdFromName $1 $0
  ${If} $0 != ""
      !insertmacro UnselectSection $0
  ${EndIf}
FunctionEnd