編寫NSIS指令碼語言實現常用鍵
很多開發工具中都提供了外掛程式方便使用者實現一些常見的功能,減少程式員的負擔。今天小編就來跟大家介紹如何調用ButtonLinker外掛程式實現常用鍵效果。
/*
#指令碼範例#
編寫:X-Star, zhfi
描述:利用外掛程式與標頭檔建立常用鍵&連結&菜單,無需修改UI即可實現
資源:外掛程式 "ButtonEvent.dll"、"linker.dll"、"System.dll" 及標頭檔 "ButtonLinkerLib.nsh"
*/
!AddPluginDir "."
!AddIncludeDir "."
!include MUI2.nsh
!include ButtonLinkerLib.nsh
;!include UsefulLib.nsh
; --------------------------------------------------
; General settings.
Name "ButtonLinker Example"
OutFile "ButtonLinkerMUI.exe"
SetCompressor /SOLID lzma
;ReserveFile "ButtonLinker.dll"
; --------------------------------------------------
; MUI interface settings.
# We want to use our own UI with custom buttons!
# The event handler for our parent button is added in MyGUIInit.
!define MUI_CUSTOMFUNCTION_GUIINIT MyGUIInit
# Don't skip to the finish page.
!define MUI_FINISHPAGE_NOAUTOCLOSE
; --------------------------------------------------
; Insert MUI pages.
; Installer pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE ButtonLinker.nsi
!insertmacro MUI_PAGE_INSTFILES
; UnInstaller pages
!define MUI_CUSTOMFUNCTION_UnGUIINIT Un.MyGUIInit
!insertmacro MUI_UnPAGE_WELCOME
!insertmacro MUI_UnPAGE_INSTFILES
; --------------------------------------------------
; Languages.
!insertmacro MUI_LANGUAGE "SimpChinese"
/*定義要建立的控制項ID*/
;注意:ID不能重複,否則會產生未知錯誤!
;定義關於按鈕
!ifndef IDC_BUTTON
!define IDC_BUTTON 1190
!endif
;定義連結
!ifndef IDC_LINKER
!define IDC_LINKER 1200
!endif
;定義複選框CheckBox2
!ifndef IDC_CheckBox2
!define IDC_CheckBox2 1210
!endif
;定義複選框CheckBox3
!ifndef IDC_CheckBox3
!define IDC_CheckBox3 1220
!endif
;定義關於菜單1
!ifndef IDM_ABOUT1
!define IDM_ABOUT1 1300
!endif
;定義關於菜單2
!ifndef IDM_ABOUT2
!define IDM_ABOUT2 1301
!endif
; --------------------------------------------------
; 建立公用函數
!macro MYMACRO un
Function ${un}AboutButton
MessageBox MB_OK|MB_ICONINFORMATION "關於按鈕!"
FunctionEnd
Function ${un}AboutMenu1
MessageBox MB_OK|MB_ICONINFORMATION "關於菜單1!"
FunctionEnd
Function ${un}AboutMenu2
MessageBox MB_OK|MB_ICONINFORMATION "關於菜單2!"
FunctionEnd
Function ${un}CheckBox3
/*擷取控制項狀態*/
;用法:${GetBoxState} 輸出變數(0/1/error) 父視窗視窗控制代碼 控制項ID
${GetBoxState} $0 $HWNDPARENT ${IDC_CheckBox3}
; MessageBox MB_OK|MB_ICONINFORMATION "$0"
${if} $0 == 0
MessageBox MB_yesno|MB_ICONINFORMATION "取消選擇?" idyes true idno false
true:
${SetBoxState} 0 $HWNDPARENT ${IDC_CheckBox3}
goto End
false:
${SetBoxState} 1 $HWNDPARENT ${IDC_CheckBox3}
goto End
${elseif} $0 == 1
MessageBox MB_yesno|MB_ICONINFORMATION "選擇?" idyes true2 idno false2
true2:
${SetBoxState} 1 $HWNDPARENT ${IDC_CheckBox3}
goto End
false2:
${SetBoxState} 0 $HWNDPARENT ${IDC_CheckBox3}
goto End
${else}
MessageBox MB_OK|MB_ICONINFORMATION "$0!"
${endif}
End:
FunctionEnd
Function ${un}MyGUIInit
InitPluginsDir
Pop $0
Pop $1
Pop $2
Pop $3
/*建立常用鍵*/
;擷取“取消”按鈕的位置
${GetDlgItemRect} $0 $1 $2 $3 $HWNDPARENT ${IDC_CANCEL}
IntOp $2 $3 - $1
;用法:${CreateButton2} 顯示文本 X軸位置 Y軸位置 寬度 高度 父視窗視窗控制代碼 控制項ID 目標函數
;參考“取消”的位置來建立常用鍵
${CreateButton2} "關於(&A)" 20 $1 80 $2 $HWNDPARENT ${IDC_BUTTON} ${un}AboutButton
/*建立超級連結*/
;擷取“取消”按鈕的位置
${GetDlgItemRect} $0 $1 $2 $3 $HWNDPARENT ${IDC_CANCEL}
IntOp $2 $3 - $1
IntOp $1 $1 + 4
IntOp $2 $2 - 1
;用法:${CreateLinker2} 顯示文本 X軸位置 Y軸位置 寬度 高度 父視窗視窗控制代碼 控制項ID 連結地址
;參考“取消”的位置來建立超級連結
${CreateLinker2} "訪問我的首頁" 120 $1 80 $2 $HWNDPARENT ${IDC_LINKER} "http://hi.baidu.com/xstar2008"
/*建立超級複選框*/
;擷取“取消”按鈕的位置
${GetDlgItemRect} $0 $1 $2 $3 $HWNDPARENT ${IDC_CANCEL}
IntOp $2 $3 - $1
IntOp $1 $1 + 4
IntOp $2 $2 - 1
;用法:${CreateCheckBox2} 顯示文本 X軸位置 Y軸位置 寬度 高度 父視窗視窗控制代碼 控制項ID 控制項狀態(0/1)
;參考“取消”的位置來建立超級複選框
${CreateCheckBox2} "CheckBox2" 200 $1 50 $2 $HWNDPARENT ${IDC_CheckBox2} 1
;用法:${CreateCheckBox3} 顯示文本 X軸位置 Y軸位置 寬度 高度 父視窗視窗控制代碼 控制項ID 控制項狀態(0/1) 目標函數
;參考“取消”的位置來建立超級複選框
${CreateCheckBox3} "CheckBox3" 250 $1 50 $2 $HWNDPARENT ${IDC_CheckBox3} 0 ${un}CheckBox3
/*建立超級菜單*/
;用法:${CreateMenu2} 顯示文本 菜單類型 父視窗視窗控制代碼 菜單ID 目標函數
${CreateMenu2} 0 ${MF_SEPARATOR} $HWNDPARENT 0 ${un}MyGUIInit
${CreateMenu2} "關於菜單1(&A)" ${MF_STRING} $HWNDPARENT ${IDM_ABOUT1} ${un}AboutMenu1
${CreateMenu2} "關於菜單2(&A)" ${MF_STRING} $HWNDPARENT ${IDM_ABOUT2} ${un}AboutMenu2
FunctionEnd
Function ${un}.onGUIEnd
ButtonEvent::unload
Linker::unload
FunctionEnd
; --------------------------------------
!macroend
; 插入安裝/卸載函數
!insertmacro MYMACRO ""
!insertmacro MYMACRO "un."
!insertmacro MYMACRO "un"
Section "Dummy" SecDummy
/*擷取控制項狀態*/
;用法:${GetBoxState} 輸出變數(0/1/error) 父視窗視窗控制代碼 控制項ID
${GetBoxState} $0 $HWNDPARENT ${IDC_CheckBox2}
StrCmp $0 1 0 +2
Messagebox MB_OK "你想幹嘛?"
Sleep 1000
WriteUninstaller "$Exediruninst.exe"
SectionEnd
Section Uninstall
Delete "$InstdirUninst.exe"
SectionEnd