1. 程式人生 > >NSIS製作安裝檔案

NSIS製作安裝檔案

NSIS 大體佈局如下:
• 預設引數(包括外部壓縮器選擇、編譯選項、巨集定義以及檔案包含等)
• 普通安裝設定
• 自定義函式
• 安裝程式區域內容
• 安裝程式回撥函式及其相關函式定義
• 解除安裝程式區域內容
• 解除安裝程式回撥函式及其相關函式定義
一個示例指令碼

; study.nsi
;
; 以下指令碼用以生成 study.exe 檔案
; 它包含有授權協議
;
; 安裝時若發現應用程式正在執行,安裝程式會提示並替使用者關閉應用程式
; 安裝成功後會自動執行應用程式
;
; 啟用新的 Modern UI 的巨集指令語法,原先的部分指令將被禁止以防止重複
; 我會盡量展示如何基本指令的用法。當然巨集指令也會與一些基本指令發生重
; 復定義的情況,不過 NSIS
照樣能編譯成功. ; 其中的一些路徑、檔案你可以根據需要更改 ; 啟用外部壓縮工具(這裡我們不用,原因看第 10 步) ;!define HAVE_UPX ; 若啟用了外部壓縮工具,這進行可執行檔案頭的壓縮 !ifdef HAVE_UPX ; 用 UPX 壓縮可執行檔案頭(用其他的相關工具也行) !packhdr tmp.dat "E:UPX\upx --best tmp.dat" !endif ; 編譯選項 ; 設定覆蓋標記 SetOverwrite on ; 設定壓縮選項 SetCompress auto ; 選擇壓縮方式 SetCompressor bzip2 ; 設定資料塊優化 SetDatablockOptimize
on ; 設定資料寫入時間 SetDateSave on ; 相關資料預設 ; 軟體的名字,後面的${NAME}呼叫這個名字 !define NAME "NSIS" !define NAME_FULL "全方位掌握 NSIS 的使用" !verbose 3 ; 包含視窗訊息定義檔案 !include "${NSISDIR}\Examples\WinMessages.nsh" ; 包含新介面的巨集指令檔案 !include "${NSISDIR}\Examples\Modern UI\ModernUI.nsh" ; 包含標牌點陣圖巨集指令檔案 !include "${NSISDIR}\Examples\branding.nsh"
!verbose 4 !define CURRENTPAGE $9 !define TEMP1 $R0 !define TEMP2 $R1 ; 插入安裝介面巨集定義(這裡我禁用了,啟用也沒問題。編譯照樣通過。後面我都定義過了) ;!insertmacro MUI_INTERFACE "modern2.exe" "adni18-installer-C-no48xp.ico" "adni18-uninstall-C-no48xp.ico" "modern.bmp" "smooth" ; 安裝程式設定 ; 輸出檔案 OutFile "F:\study.exe" ; 安裝程式名稱 Name /LANG=2052 "NSIS" ; 替換對話方塊樣式 ChangeUI all "${NSISDIR}\Contrib\UIs\modern2.exe" ; 啟用 WindowsXP 的視覺樣式 XPstyle on ;設定字型 SetFont 宋體 9 ; 標題名稱 Caption /LANG=2052 "全方位掌握 NSIS 的使用" ; 標牌的內容 BrandingText /LANG=2052 /TRIMCENTER "Shao Hao" ; 安裝程式圖示 Icon "F:\12\QCDIcon.ico" ; 安裝程式顯示標題 WindowIcon on ; 新增標牌點陣圖(放在頂部用 TOP 引數。後面接高度) AddBrandingImage LEFT 105 ; 設定漸變背景 BGGradient off ; 設定安靜安裝模式 ;SilentInstall normal ; 設定安靜解除安裝模式 ;SilentUnInstall normal ; 執行 CRC 檢查(最好用 on 開啟。關閉是因為還要修改安裝程式,看第 10 步) CRCCheck off ; 載入相應的語言檔案 LoadLanguageFile "${NSISDIR}\Contrib\Language files\SimpChinese.nlf" ; 替換各個對話方塊標題的文字 SubCaption /LANG=2052 0 "授權協議" SubCaption /LANG=2052 1 "安裝選項" SubCaption /LANG=2052 2 "安裝目錄" SubCaption /LANG=2052 3 "正在安裝" SubCaption /LANG=2052 4 "完成安裝" ; 替換預設按鈕的文字 MiscButtonText /LANG=2052 "< 上一步" "下一步 >" "取消" "關閉" ; 替換“安裝”按鈕的文字 ;InstallButtonText /LANG=2052 "安裝" ; 替換當檔案無法被寫入時的警告窗的文字 FileErrorText /LANG=2052 "無法寫入檔案$\r$\n$0$\r$\n請確認檔案屬性不是隻讀且未被使用中!" ; 預設的安裝目錄 InstallDir "$PROGRAMFILES\QCD 3" ; 如果可能的化從登錄檔中監測安裝路徑 InstallDirRegKey HKLM \ "Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS" \ "UninstallString" ; 授權協議頁面 LicenseText /LANG=2052 "NSIS 是免費軟體。安裝前請先閱讀以下協議條款" "我同意" ; 使用授權協議文字 LicenseData /LANG=2052 "F:\12\License.txt" ; 設定授權協議頁面的背景色 ;LicenseBkColor 000000 ; 選擇要按安裝的元件 ComponentText /LANG=2052 "現在將安裝 ${NAME_FULL}到您的計算機:" "請選擇安裝型別" "或選擇您希望安裝的元件" InstType "完全安裝(都在了)" InstType "典型安裝" InstType /LANG=2052 /CUSTOMSTRING=自定義? ;InstType /LANG=2052 /NOCUSTOM ;InstType /COMPONENTSONLYONCUSTOM CheckBitmap "${NSISDIR}\Contrib\Icons\modern.bmp" ; 替換顯示磁碟空間資訊的文字 SpaceTexts /LANG=2052 "所需空間:" "可用空間:" ; 提示使用者輸入目錄的文字 ; DirShow hide ; 替換顯示選擇安裝目錄的文字 DirText /LANG=2052 "請選擇 ${NAME} 的安裝路徑:" "選擇目錄用以安裝 ${NAME}:" "瀏覽..." ; 是否允許安裝在根目錄下 AllowRootDirInstall false ; 安裝資訊的顏色 ;InstallColors 000000 FFFFFF ; 安裝進度條顯示方式 InstProgressFlags smooth colored ; 完成後自動關閉安裝程式 AutoCloseWindow true ; 顯示“顯示詳細細節”對話方塊 ShowInstDetails hide ; 替換“顯示細節”按鈕的文字 DetailsButtonText /LANG=2052 "顯示細節" ; 替換“完成”按鈕的文字 CompletedText /LANG=2052 "已完成" ; 解除安裝程式設定 ; 替換解除安裝程式的文字 UninstallText /LANG=2052 "現在將從你的系統中解除安裝 ${NAME}:" "解除安裝目錄:" ; 解除安裝程式圖示 UninstallIcon "F:\12\UnQCDIcon.ico" ; 替換解除安裝程式標題的文字 UninstallCaption /LANG=2052 "解除安裝 ${NAME}" ; 替換解除安裝程式每頁按鈕的文字 UninstallSubCaption /LANG=2052 0 ":確認" UninstallSubCaption /LANG=2052 1 ":正在刪除檔案" UninstallSubCaption /LANG=2052 2 ":完成" ; 解除安裝程式顯示方式 ShowUninstDetails hide ; 替換“解除安裝”按鈕的文字 UninstallButtonText /LANG=2052 "解除安裝" ; 安裝程式內容及其相關的回撥函式 ; 安裝程式內容 Section /e "!主要程式(必須安裝)" SecCore SectionIn 1 2 RO ; 設定輸出路徑,每次使用都會改變 SetOutPath $INSTDIR ; 迴圈包含目錄下全部內容 File /r "F:\12\1\*.*" ; 只包含一個檔案 File "F:\12\gf.gif" ; 處理動態連線庫檔案 IfFileExists "$INSTDIR\1.dll" 0 NoFile1 ; 取消元件註冊 UnRegDll "$INSTDIR\1.dll" Delete "$INSTDIR\1.dll" NoFile1: File "F:\12\1.dll" ; 註冊元件 RegDLL "$INSTDIR\QCDIconMgr.dll" ; 寫入軟體註冊鍵值 WriteRegStr HKLM "Software\NSIS\NSIS" "" "$INSTDIR" ; 為 Windows 解除安裝程式寫入鍵值 WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS" "DisplayName" "NSIS(只用於移除)" WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS" "UninstallString" '"$INSTDIR\uninst.exe"' ; 寫入生成解除安裝程式(可多次使用) WriteUninstaller "$INSTDIR\uninst.exe" SectionEnd Section /e "擴充套件(可選)" SecUpdate SectionIn 1 SetOutPath "$INSTDIR\Plugins" File "F:\12\qcdplus\Plugins\2.dll" File /r "F:\12\qcdplus\Plugins\3" SectionEnd SubSection /e "選項" SecOptions Section /e "建立桌面圖示" SecDesktopShortCut SectionIn 1 2 SetOutPath "$INSTDIR\Plugins" CreateShortCut "$DESKTOP\NSIS.lnk" "$INSTDIR\gf.gif" WriteUninstaller "$INSTDIR\uninst.exe" SectionEnd Section /e "建立快速啟動欄圖示" SecQuickbarShortCut SectionIn 1 2 SetOutPath "$INSTDIR\Plugins" CreateShortCut "$QUICKLAUNCH\NSIS.lnk" "$INSTDIR\gf.gif" WriteUninstaller "$INSTDIR\uninst.exe" SectionEnd Section /e "建立 NSIS 程式組" SecShortCutGroup SectionIn 1 2 SetOutPath "$INSTDIR\Plugins" CreateDirectory "$SMPROGRAMS\NSIS" CreateShortCut "$SMPROGRAMS\QCD Player\gf.lnk" "$INSTDIR\gf.gif" WriteUninstaller "$INSTDIR\uninst.exe" SectionEnd SubSectionEnd Section "-Run"; 執行? ExecWait '"$INSTDIR\2.exe" -p' WriteINIStr "$INSTDIR\NSIS.ini" "2" "Language" "Chinese" Exec "$INSTDIR\2.exe" ExecShell open "$INSTDIR\漢化說明.htm" SectionEnd Section "" ;Invisible section to display the Finish header !insertmacro MUI_FINISHHEADER SetHeader SectionEnd ; 安裝程式初始化設定 Function .onInit ; 發現有程式正在執行時提示退出後繼續執行 loop: FindWindow $R0 "NSIS" IntCmp $R0 0 done MessageBox MB_OKCANCEL \ "NSIS 正在執行。$\n點選“確定”按鈕會關閉 NSIS 然後繼續安裝,點選“取消”按鈕將退出安裝程式" \ IDOK NoAbort Abort NoAbort: SendMessage $R0 1029 0 0 ; 稍等後繼續檢測直至檢測不到或使用者選擇取消 Sleep 444 Goto loop done: ; 顯示 Logo 畫面 SetOutPath $TEMP File /oname=qcd_logo.bmp "F:\12\logo.bmp" File /oname=magiclime.exe "${NSISDIR}\Bin\magiclime.exe" ExecWait '"$TEMP\magiclime.exe" qcd_logo.bmp' Delete "$TEMP\magiclime.exe" Delete "$TEMP\qcd_logo.bmp" ; 初始化標牌點陣圖 !insertmacro BI_INIT $R0 FunctionEnd ; 安裝程式主介面的相關文字 Function SetHeader !insertmacro MUI_HEADER_INIT !insertmacro MUI_HEADER_START 1 !insertmacro MUI_HEADER_TEXT 2052 "授權協議" "安裝 ${NAME} 之前,請認真閱讀一下授權條款." !insertmacro MUI_HEADER_STOP 1 !insertmacro MUI_HEADER_START 2 !insertmacro MUI_HEADER_TEXT 2052 "選擇元件" "請選擇你要安裝的元件." !insertmacro MUI_HEADER_STOP 2 !insertmacro MUI_HEADER_START 3 !insertmacro MUI_HEADER_TEXT 2052 "選擇安裝位置" "為 ${NAME} 選擇一個安裝目錄." !insertmacro MUI_HEADER_STOP 3 !insertmacro MUI_HEADER_START 4 !insertmacro MUI_HEADER_TEXT 2052 "安裝" "${NAME} 已經安裝,請等待." !insertmacro MUI_HEADER_STOP 4 !insertmacro MUI_HEADER_START 5 !insertmacro MUI_HEADER_TEXT 2052 "完成" "安裝順利完成." !insertmacro MUI_HEADER_STOP 5 !insertmacro MUI_HEADER_END FunctionEnd ; 初始化安裝程式對話方塊的顯示文字 Function .onInitDialog !insertmacro MUI_INNERDIALOG_INIT !insertmacro MUI_INNERDIALOG_START 1 !insertmacro MUI_INNERDIALOG_TEXT 2052 1040 "如果你同意協議中的所有條款,選擇“我同樣”繼續安裝,如果你選擇了“取消”,安裝程式將終止,只有接受條款才能安裝 ${NAME}." !insertmacro MUI_INNERDIALOG_STOP 1 !insertmacro MUI_INNERDIALOG_START 2 !insertmacro MUI_INNERDIALOG_TEXT 2052 1042 "描述" !insertmacro MUI_INNERDIALOG_TEXT 2052 1043 "移動你的滑鼠到元件上,便可以見到相關的描述." !insertmacro MUI_INNERDIALOG_STOP 2 !insertmacro MUI_INNERDIALOG_START 3 !insertmacro MUI_INNERDIALOG_TEXT 2052 1041 "目標資料夾" !insertmacro MUI_INNERDIALOG_STOP 3 !insertmacro MUI_INNERDIALOG_END FunctionEnd ; 轉到下頁面時的處理 Function .onNextPage !insertmacro MUI_NEXTPAGE_OUTER !insertmacro MUI_NEXTPAGE SetHeader ; 標牌點陣圖設定 !insertmacro BI_NEXT FunctionEnd ; 轉到上一頁面時的處理 Function .onPrevPage !insertmacro MUI_PREVPAGE ; 標牌點陣圖設定 !insertmacro BI_PREV FunctionEnd ; 滑鼠移到指定元件時的顯示處理 Function .onMouseOverSection ; 用巨集指令設定安裝自己的註釋文字 !insertmacro MUI_DESCRIPTION_INIT !insertmacro MUI_DESCRIPTION_TEXT 2052 ${SecCore} "安裝主應用程式" !insertmacro MUI_DESCRIPTION_TEXT 2052 ${SecUpdate} "安裝 NSIS 的更新或新增外掛" !insertmacro MUI_DESCRIPTION_TEXT 2052 ${SecOptions} "選擇 NSIS 的其他安裝選項,例如:建立快捷方式和應用程式組的" !insertmacro MUI_DESCRIPTION_TEXT 2052 ${SecDesktopShortCut} "在使用者的桌面上建立 NSIS 的快捷方式" !insertmacro MUI_DESCRIPTION_TEXT 2052 ${SecQuickbarShortCut} "在使用者的快速欄裡建立 NSIS 快捷方式" !insertmacro MUI_DESCRIPTION_TEXT 2052 ${SecShortCutGroup} "在使用者的開始選單的程式中建立 NSIS 的快捷方式組" !insertmacro MUI_DESCRIPTION_END FunctionEnd ; 安裝成功完成後的設定 Function .onInstSuccess ; 自己加吧:) FunctionEnd ; 放棄安裝時警告視窗的顯示資訊 Function .onUserAbort !insertmacro MUI_ABORTWARNING 2052 "你確定要推出 ${NAME} 的安裝嗎?" !insertmacro MUI_ABORTWARNING_END FunctionEnd ; 安裝程式標牌點陣圖列表 !insertmacro BI_LIST !insertmacro BI_LIST_ADD "F:\12\Brand1.bmp" /RESIZETOFIT !insertmacro BI_LIST_ADD "F:\12\Brand2.bmp" /RESIZETOFIT !insertmacro BI_LIST_ADD "F:\12\Brand3.bmp" /RESIZETOFIT !insertmacro BI_LIST_END ; 解除安裝程式及其相關回調函式 ; 解除安裝程式內容 Section "Uninstall" ExecWait '"$INSTDIR\12.exe" /un' ; 迴圈刪除檔案直至刪除資料夾 RMDir /r "$SMPROGRAMS\NSIS" UnRegDLL "$INSTDIR\2.dll" Delete "$INSTDIR\QCDIconMgr.dll" Delete "$INSTDIR\*.*" DeleteRegKey HKLM "Software\NSIS" DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS" MessageBox MB_YESNO|MB_ICONQUESTION \ "是否連外掛目錄也要刪除?$\n(若您要保留這些檔案,請點選下面的“否”按鈕)" \ IDNO NoDelete ; 全刪光!!! RMDir /r "$INSTDIR" NoDelete: SectionEnd ; 定義解除安裝程式標牌點陣圖 !define BI_UNINSTALL !include "${NSISDIR}\Examples\branding.nsh" ; 初始化解除安裝程式對話方塊的設定 Function un.onInit ; 初始化標牌點陣圖 !insertmacro BI_INIT $R0 FunctionEnd ; 解除安裝程式主介面的相關文字 Function un.SetHeader !insertmacro MUI_HEADER_INIT !insertmacro MUI_HEADER_START 1 !insertmacro MUI_HEADER_TEXT 2052 "解除安裝 ${NAME}" "${NAME_FULL} 將從你的系統裡移除." !insertmacro MUI_HEADER_STOP 1 !insertmacro MUI_HEADER_START 2 !insertmacro MUI_HEADER_TEXT 2052 "解除安裝" "${NAME} 正在被解除安裝,請等待." !insertmacro MUI_HEADER_STOP 2 !insertmacro MUI_HEADER_START 3 !insertmacro MUI_HEADER_TEXT 2052 "完成" "${NAME_FULL} 已從你的系統中移除." !insertmacro MUI_HEADER_STOP 3 !insertmacro MUI_HEADER_END FunctionEnd ; 初始化解除安裝程式對話方塊時的處理 Function un.onInitDialog ; 自己加:) FunctionEnd ; 解除安裝程式轉到下一頁時的處理 Function un.onNextPage !insertmacro MUI_NEXTPAGE_OUTER !insertmacro MUI_NEXTPAGE un.SetHeader ; 標牌點陣圖設定 !insertmacro BI_NEXT FunctionEnd ; 解除安裝程式完成時的處理 Function un.onUninstSuccess ; 再加^_^ FunctionEnd ; 放棄解除安裝程式時的處理 Function un.onUserAbort FunctionEnd ; 解除安裝程式標牌點陣圖列表 !insertmacro BI_LIST !insertmacro BI_LIST_ADD "F:\12\UnBrand1.bmp" /RESIZETOFIT !insertmacro BI_LIST_ADD "F:\12\UnBrand2.bmp" /RESIZETOFIT !insertmacro BI_LIST_END ; 結束