在函数中设置InstallDir的值,或以某种方式设置自动填充值?

时间:2012-01-02 23:21:00

标签: nsis

我正在使用NSIS创建安装程序。此安装程序实际上在同一安装程序中的两个不同目录中安装了两个程序。我这样做是使用现代用户界面(MUI)页面,只需调用MUI_PAGE_DIRECTORY两次指定不同的起始参数,并捕获LEAVE宏中的目录。我想知道的是,我可以以某种方式在函数中调用InstallDir,还是在函数中设置自动目录填充值?或者甚至可能在从?

返回浏览按钮后调用函数

我想这样做的原因是,当用户单击两个目录页面中的任何一个中的浏览按钮时,在选择目录后,将附加在InstallDir中指定的finnal目录的名称。

例如: 程序1的InstallDir值:c:\ client 程序2的InstallDir值:c:\ program files \ server

用户单击浏览程序1并选择c:\ temp,生成的路径为c:\ temp \ client

用户单击浏览程序2并选择c:\,无论生成的路径是c:\ whatever \ server

这里参考的是我所拥有的代码片段,但不处理自动追加浏览按钮的行为:

!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ClientDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY

!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ServerDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY

; Setup the page display for the client install page
Function ShowPageClient
  !insertmacro MUI_HEADER_TEXT "Client" "Client"
  !insertmacro MUI_INNERDIALOG_TEXT 1006 "Client"

  ; setup intal directory
  Push $0
  StrCpy $0 $PROGRAMFILES 2 #
  ; CLIENT_FOLDER_NAME is defined as a folder, but this would basicaly
  ; result in C:\Client as the first 2 characters of $PROGRAMFILES
  ; is the hard drive with program files installed on it
  StrCpy $INSTDIR "$0\${CLIENT_FOLDER_NAME}"
  Pop $0

    ; set the inital value of the directory text box  
    !insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR

    ; find and disable the directory selection box 
    ; We do not want users to type in this box
    FindWindow $R0 "#32770" "" $HWNDPARENT
    GetDlgItem $R1 $R0 1019 ;Text Box
    EnableWindow $R1 0
FunctionEnd


; Setup the page display for the server install location page
Function ShowPageServer
  !insertmacro MUI_HEADER_TEXT "Server" "Server"
  !insertmacro MUI_INNERDIALOG_TEXT 1006 "Server"

  ; setup intal directory
  ; SERVER_FOLDER_NAME is defined as a folder, but this would basicaly
  ; result in C:\Program Files\Server 
  StrCpy $INSTDIR "$PROGRAMFILES\${SERVER_FOLDER_NAME}"

  ; set the inital value of the directory text box  
  !insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR

  ; find and disable the directory selection box 
  ; We do not want users to type in this box
  FindWindow $R0 "#32770" "" $HWNDPARENT
  GetDlgItem $R1 $R0 1019 ;Text Box
  EnableWindow $R1 0

FunctionEnd

注意:我可以使浏览按钮适用于其中一个目录页面,但是当我在第二页时,自动填充实际自动填充错误

2 个答案:

答案 0 :(得分:4)

好的,我终于明白了。基本上,在单击浏览按钮后,有一个函数被称为“验证”路径。如果需要,我将这个功能附加到目录手册中。为此,我创建了一个新变量,并将其设置在页面显示时调用的函数中,如下所示:

; Client Directory
!define MUI_PAGE_CUSTOMFUNCTION_SHOW ShowPageClient
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ClientDirectoryLeave
!insertmacro MUI_PAGE_DIRECTORY

; Setup the page display for the client install page
Function ShowPageClient
  ; setup intal directory
  Push $0
  StrCpy $0 $PROGRAMFILES 2 #
  StrCpy $INSTDIR "$0\${CLIENT_FOLDER_NAME}"
  Pop $0

  !insertmacro MUI_INNERDIALOG_TEXT 1019 $INSTDIR

  FindWindow $R0 "#32770" "" $HWNDPARENT
  GetDlgItem $R1 $R0 1019 ;Text Box
  EnableWindow $R1 0

  ; Setup the client or server variable to indicate that
  ; we're in the client install part to signal the .onVerifyInstDir function
  ; to put the correct directory
  StrCpy $CLIENT_OR_SERVER "client"
FunctionEnd

在browse之后调用的函数是.onVerifyInstDir,这样我就可以检查CLIENT_OR_SERVER变量并适当地设置路径

; This function is special and is called any time a
; path is validated on returning from the browse button
; need to append the correct directory if it does not already exists
; in the install directory path
Function .onVerifyInstDir
  ; save the current $0 register, as it is used in this function
  Push $0

  ${If} $CLIENT_OR_SERVER == "client"
    ; in the client stage so directory must contain CLIENT_FOLDER_NAME
    ${StrContains} $0 "${CLIENT_FOLDER_NAME}" "$INSTDIR"
    ${If} $0 == ""
      ; the install dir does not contain the folder so append it
      StrCpy $INSTDIR "$INSTDIR\${CLIENT_FOLDER_NAME}"
    ${EndIf}
  ${Else}
    ; in the server stage so directory must contain SERVER_FOLDER_NAME
    ${StrContains} $0 "${SERVER_FOLDER_NAME}" "$INSTDIR"
    ${If} $0 == ""
      ; the install dir does not contain the folder so append it
      StrCpy $INSTDIR "$INSTDIR\${SERVER_FOLDER_NAME}"
    ${EndIf}
  ${EndIf}

  ; pop the saved register value
  Pop $0
FunctionEnd

情侣笔记: 我使用的StrContains函数在这里找到: http://nsis.sourceforge.net/StrContains

可以在此处找到对.onVerifyInstDir函数的进一步参考: http://nsis.sourceforge.net/Docs/Chapter4.html#4.7.2.1.10

答案 1 :(得分:4)

附加的文件夹名称是常量并在编译时设置,有一个bug report与此相关。

我的建议是放弃追加功能,让用户完全控制两个目的地:

Name "NSIS Test"
InstallDir ""
!include MUI.nsh
Var DirClient
Var DirServer

Function .onInit
;Set default destinations
StrCpy $DirClient "$ProgramFiles\$(^Name)\Client"
StrCpy $DirServer "$ProgramFiles\$(^Name)\Server"
FunctionEnd

!macro ConfigureMyDirPage type var
!define MUI_DIRECTORYPAGE_VARIABLE ${var}
!define MUI_PAGE_HEADER_SUBTEXT "Choose the folder in which to install $(^NameDA) ${type}"
!define MUI_DIRECTORYPAGE_TEXT_TOP "Setup will install $(^NameDA) ${type} in the following folder. To install in a different folder, click Browse and select another folder. $_CLICK"
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION "${type} $(^DirSubText)"
!macroend

!insertmacro ConfigureMyDirPage "Client" $DirClient
!insertmacro MUI_PAGE_DIRECTORY

!insertmacro ConfigureMyDirPage "Server" $DirServer
!insertmacro MUI_PAGE_DIRECTORY

!insertmacro MUI_PAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English"

Section
DetailPrint DirClient=$DirClient
DetailPrint DirServer=$DirServer
SectionEnd