首页 > 解决方案 > 按变量选择部分

问题描述

!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}

一切正常...

有没有办法从变量中按名称选择部分?

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

标签: nsis

解决方案


部分通过其 ID 访问,该 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

推荐阅读