我正在使用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
Run Code Online (Sandbox Code Playgroud)
注意:我可以使浏览按钮适用于其中一个目录页面,但是当我在第二页时,自动填充实际自动填充错误
好吧我终于想通了。基本上有一个函数被调用来“验证”单击浏览按钮后的路径。如果需要的话,我将这个函数与附加的目录手册联系起来。为此,我创建了一个新变量,并将其设置在页面显示时调用的函数中,如下所示:
; 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
Run Code Online (Sandbox Code Playgroud)
浏览后调用的函数是 .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
Run Code Online (Sandbox Code Playgroud)
几点说明:我使用的 StrContains 函数在这里找到: http: //nsis.sourceforge.net/StrContains
对 .onVerifyInstDir 函数的进一步参考可以在这里找到: http://nsis.sourceforge.net/Docs/Chapter4.html#4.7.2.1.10
| 归档时间: |
|
| 查看次数: |
6500 次 |
| 最近记录: |