很多開發工具中都提供了插件方便用戶實現一些常見的功能,減少程序員的負擔。今天小編就來跟大家介紹如何調用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