InstallOptions 2介紹InstallOptions 是一個可以建立自定義安裝程式頁面的 NSIS 插件,它可以提供一些訊息的輸入輸出等高級功能。 InstallOptions 將會在 NSIS 視窗內建立一個對話。對話上的所有控件由 INI 檔案定義。 NSIS 2 有新的頁面系統,它使你把自定義的頁面新增到自己的安裝程式而不會導致上一步、下一步功能混亂。在新的插件系統裡,你也不用去管插件的釋放、刪除,這些 NSIS 都替你準備好了。如果你把 INI 檔案儲存在插件目錄,安裝結束時 NSIS 也會一樣幫你自動刪除。 這個新的 InstallOptions 是為 NSIS 2 設計的。它支援自定義用戶介面和自定義字型和 DPI 設定。 INI 檔案這個 INI 檔案有一個必需的區段。這個區段包含了要建立的控件的數量和慣用的視窗屬性。INI 檔案也包含了一些帶變量的 Field x 區段用來定義要建立的控件及其屬性。 必須的區段名稱必須為 "Settings"。他可以包含下列值:
每個區段都為"Field #" 形式,這裡 # 是一個從 1 到 NumFields 的連續的數字。每一個區段可以包含下列值:
如何使用Modern UI關於在 Modern UI 裡使用 InstallOptions 的更多訊息請觀看 Modern UI 文件。 釋放 INI 檔案首先,你需要在 .onInit 函數里把對話的 INI 檔案釋放出來。 Function .onInit InitPluginsDir File /oname=$PLUGINSDIR\test.ini test.ini FunctionEnd 調用 DLL你可以在 page 函數里調用 InstallOptions,關於頁面系統的更多訊息請觀看 NSIS 文件。例如: Page custom SetCustom ValidateCustom InstallOptions DLL 有三個函數:
通常,你只需要 dialog 函數就可以了: Function SetCustom ;FunctionName defined with Page command ;Display the Install Options dialog Push $R0 InstallOptions::dialog $PLUGINSDIR\test.ini Pop $R0 FunctionEnd 取得輸入要取得用戶的輸入,可以用 ReadINIStr 來讀取某個 Field 的 State 值: ReadINIStr $R0 "$PLUGINSDIR\test.ini" "Field 1" "State" 一些 InstallOptions 的值已被轉義(類似於 C 字串)來使得一些特定字元可以在 INI 檔案裡使用。受影響的值為:
轉義的字元如下:
下面的函數可以用來轉換這些字元:
; 轉換 NSIS 字串到 InstallOptions 字串
; 使用:
; Push <NSIS-字串>
; Call Nsis2Io
; Pop <IO-字串>
Function Nsis2Io
Exch $0 ; The source
Push $1 ; The output
Push $2 ; Temporary char
StrCpy $1 "" ; Initialise the output
loop:
StrCpy $2 $0 1 ; Get the next source char
StrCmp $2 "" done ; Abort when none left
StrCpy $0 $0 "" 1 ; Remove it from the source
StrCmp $2 "\" "" +3 ; Back-slash?
StrCpy $1 "$1\\"
Goto loop
StrCmp $2 "$\r" "" +3 ; Carriage return?
StrCpy $1 "$1\r"
Goto loop
StrCmp $2 "$\n" "" +3 ; Line feed?
StrCpy $1 "$1\n"
Goto loop
StrCmp $2 "$\t" "" +3 ; Tab?
StrCpy $1 "$1\t"
Goto loop
StrCpy $1 "$1$2" ; Anything else
Goto loop
done:
StrCpy $0 $1
Pop $2
Pop $1
Exch $0
FunctionEnd
; 轉換 InstallOptions 字串到 NSIS 字串
; Usage:
; Push <IO-字串>
; Call Io2Nsis
; Pop <NSIS-字串>
Function Io2Nsis
Exch $0 ; The source
Push $1 ; The output
Push $2 ; Temporary char
StrCpy $1 "" ; Initialise the output
loop:
StrCpy $2 $0 1 ; Get the next source char
StrCmp $2 "" done ; Abort when none left
StrCpy $0 $0 "" 1 ; Remove it from the source
StrCmp $2 "\" +3 ; Escape character?
StrCpy $1 "$1$2" ; If not just output
Goto loop
StrCpy $2 $0 1 ; Get the next source char
StrCpy $0 $0 "" 1 ; Remove it from the source
StrCmp $2 "\" "" +3 ; Back-slash?
StrCpy $1 "$1\"
Goto loop
StrCmp $2 "r" "" +3 ; Carriage return?
StrCpy $1 "$1$\r"
Goto loop
StrCmp $2 "n" "" +3 ; Line feed?
StrCpy $1 "$1$\n"
Goto loop
StrCmp $2 "t" "" +3 ; Tab?
StrCpy $1 "$1$\t"
Goto loop
StrCpy $1 "$1$2" ; Anything else (should never get here)
Goto loop
done:
StrCpy $0 $1
Pop $2
Pop $1
Exch $0
FunctionEnd
驗證輸入如果你想驗證頁面的輸入,例如,你需要知道用戶是否填寫了文字框,當驗證失敗時可以使用頁面的 leave 函數並調用 Abort 來阻止用戶繼續:
Function ValidateCustom
ReadINIStr $R0 "$PLUGINSDIR\test.ini" "Field 1" "State"
StrCmp $R0 "" 0 +3
MessageBox MB_ICONEXCLAMATION|MB_OK "請輸入一個名稱。"
Abort
FunctionEnd
返回值在你調用 InstallOptions 之後,InstallOptions 會新增一個字串到堆棧,新增的字串可能是下列之一:
通常,你不需要檢測這些值,但是你應該把它們從堆棧移除 (看上面的例子)。 僅當你需要的時候才回去檢測返回值,比如檢測用戶按了什麼按鈕。 保留檔案如果你使用 BZIP2 或 LZMA (固實) 壓縮的話,那麼在頁面初始化階段或頁面函數里釋放檔案就變得很值得注意了,你應該把這些檔案保留在一個資料區塊裡,這會使你的安裝程式運行不會變慢。 如果在上面提到的階段裡使用了 File 命令的話,請在區段和函數之外新增 ReserveFile 命令:
ReserveFile "test.ini"
ReserveFile "${NSISDIR}\Plugins\InstallOptions.dll"
字型和顏色如果你想在 InstallOptions 對話裡使用自定義的字型和顏色,你應該使用 initDialog 和 show 函數。initDialog 會在記憶體裡建立頁面但還沒有顯示。在調用了 initDialog 之後你可以設定字型和顏色,然後調用 show 來顯示對話。initDialog 會把 HWND 壓入堆棧。要取得控件的句柄請使用: GetDlgItem (輸出變量) (自定義頁面的視窗句柄) (1200 + Field number - 1) 使用自定義顏色的例子:
Function FunctionName ;FunctionName defined with Page command
;Display the Install Options dialog
Push $R0
Push $R1
Push $R2
InstallOptions::initDialog /NOUNLOAD $PLUGINSDIR\test.ini
Pop $R0
GetDlgItem $R1 $R0 1200 ;1200 + Field number - 1
;$R1 contains the HWND of the first field
CreateFont $R2 "Tahoma" 10 700
SendMessage $R1 ${WM_SETFONT} $R2 0
InstallOptions::show
Pop $R0
Pop $R2
Pop $R1
Pop $R0
FunctionEnd
版本歷史
開發團隊Original version by Michael Bishop 許可協議Original version Copyright © 2001 Michael Bishop DLL version 1 Copyright © 2001-2002 Nullsoft, Inc., ORTIM DLL version 2 Copyright © 2003-2005 Amir Szekely, Joost Verburg, Dave Laundon This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any distribution. |