2012-01-02 35 views
5

Tôi đang tạo trình cài đặt bằng NSIS. Trình cài đặt này thực sự cài đặt hai chương trình trong hai thư mục khác nhau trong cùng một trình cài đặt. Tôi đang làm điều này bằng cách sử dụng các trang giao diện người dùng hiện đại (MUI) và chỉ cần gọi MUI_PAGE_DIRECTORY hai lần xác định các tham số bắt đầu khác nhau và chụp thư mục trong macro LEAVE. Những gì tôi tự hỏi là, bằng cách nào đó tôi có thể gọi InstallDir trong một chức năng, hoặc thiết lập giá trị tự động điền vào thư mục trong một chức năng? Hoặc thậm chí có thể gọi một hàm sau khi nút duyệt đã được trả lại từ? Lý do tôi muốn làm điều này là như vậy khi người dùng nhấp vào nút duyệt ở một trong hai trang thư mục, sau khi họ chọn một thư mục, tên của thư mục finnal được chỉ định trong InstallDir sẽ được nối thêm vào.Đặt giá trị của InstallDir trong một hàm, hoặc đặt tự động điền giá trị bằng cách nào đó?

Ví dụ: InstallDir giá trị cho chương trình 1: c: \ client InstallDir giá trị cho chương trình 2: c: \ program files \ server

dùng nhấp duyệt trên chương trình 1 và chọn c: \ temp kết quả đường dẫn c: \ temp \ client

dùng nhấp duyệt trên chương trình 2 và chọn c: \ bất cứ con đường dẫn là c: \ bất cứ điều gì \ server

Để tham khảo ở đây là những snipits mã của những gì tôi có mà hoạt động nhưng không xử lý hành vi tự động thêm nút duyệt qua:

!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 

Lưu ý: tôi có thể làm công việc nút duyệt cho một trong những trang thư mục, nhưng sau đó khi tôi đang trên trang thứ hai, tự động cư tự động thực tế populates sai

Trả lời

4

Được rồi cuối cùng tôi đã tìm nó ngoài. Về cơ bản có một hàm được gọi là "xác minh" đường dẫn sau khi nút duyệt được nhấp. Tôi gắn vào chức năng này một phụ lục hướng dẫn thư mục nếu cần thiết. Để thực hiện điều này, tôi đã tạo ra một biến mới và đặt nó trong một chức năng gọi là khi trang được hiển thị như sau:

; 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 

Chức năng đó được gọi là sau khi browse là .onVerifyInstDir vì vậy đó là nơi tôi kiểm tra các biến CLIENT_OR_SERVER và thiết lập đường dẫn một cách thích hợp

; 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 

Couple ghi chú: các StrContains chức năng tôi đã sử dụng được tìm thấy ở đây: http://nsis.sourceforge.net/StrContains

Tham khảo thêm vào .onVerifyInstD chức năng ir có thể được tìm thấy ở đây: http://nsis.sourceforge.net/Docs/Chapter4.html#4.7.2.1.10

+0

Vô hiệu hóa nhập văn bản không phải là IMHO mát – Anders

4

Tên thư mục được nối tiếp không đổi và được đặt tại thời điểm biên dịch, có bug report liên quan đến điều này.

Lời khuyên của tôi là phải từ bỏ tính năng phụ thêm và cho phép người dùng có toàn quyền kiểm soát hai khu:

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 
Các vấn đề liên quan