Example Script:
Code: Select all
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; MsgBox Class Example in AutoHotkey - 2018 Ian Pride ;
; Began - 5:04 PM Monday, April 2, 2018 ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; This is an example using my MsgBox class for AutoHotkey. ;
; This should showcase all usage - at least for the most part ;
; ;
; The default MsgBox in AutoHotkey is great for simple use and, of course, I am grat- ;
; eful for it, but sometimes I need more control and so I wrote this class. ;
; With MsgBox class you can: ;
; - set all styles used by Windows MSDN MessageBox found here: ;
; https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx ;
; - create quick normal AHK MsgBox with a way to change its' styles with the ;
; separate Debug() function - single use - not stored ;
; - create indvidual MsgBox objects that can be altered at any time ;
; - Change the MsgBox's Owner ID, Title, Text, and Styles ;
; - Dislpay the MsgBox at any time with the MsgBox.Dislpay() method ;
; - All forms of calling can return an option Eg: YES/NO = 6/7 respectively ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Init - Directives
#SingleInstance,Force
;#Persistent
;OnExit,Leave
;#MaxThreads,255
; Init - Performance - not always needed - habit for me
SetBatchLines,-1
SetKeyDelay,-1
SetWinDelay,0
; Init - Vars
TITLE:="MsgBox Class Example"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SCRIPT_BOX:=New MsgBox(TITLE " Info",,,"MB_OK|MB_ICONINFORMATION") ; | delim can be
; set @ the 5th param
; Create new object for reusable MsgBox
; You only need to pass a title, but can set everything to begin
; with.
; Styles can be words, hex, or number converted hex if set from
; New. Otherwise, MsgBox.Style:= must be a number/hex, but can
; can be called from the associated MsgBox.Styles[<STYLE NAME>]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SCRIPT_BOX.Message:="This message was set at line:`n`n" A_LineNumber ; Set Message
. "`n`nof file:`n`n" A_ScriptFullPath "`n`n1st Message set."
SCRIPT_BOX.Owner:=A_ScriptHwnd ; Set the MsgBox's Owner ID to this script
s:=SCRIPT_BOX.Styles ; Create shorter object from styles, not necessary
SCRIPT_BOX.Display() ; 1st call to display the message box
SCRIPT_BOX.Message:="Would you like to exit this script?`n[No] to continue script..." ; Change message again
SCRIPT_BOX.Style:=s["MB_ICONQUESTION"] + s["MB_YESNO"] ; or 0x24 or 0x20 + 0x04 or 36
; Change the box styles
SCRIPT_BOX.Owner:=WinExist("ahk_class Shell_TrayWnd") ; Change boxs owner to the Taskbar
SCRIPT_BOX_OPTION:=SCRIPT_BOX.Display() ; Call message box and return the option
If (SCRIPT_BOX_OPTION=6) ; If option is [Yes] then exit this app
Goto,Leave
ExitLoop:
SCRIPT_BOX.Message:="You didn't exit last time.`nWould you like to exit this script?`n[No] to continue script..."
Loop
SCRIPT_BOX_OPTION:=SCRIPT_BOX.Display() ; call again
Until SCRIPT_BOX_OPTION=6 ; Loop the message box until [Yes]
; Functions
#Include, MB_AHK.ahk
; Subs
Leave:
If (Debug(TITLE " Info"
,"Are you sure you want to exit?`n[No] to continue script..."
,"MB_ICONQUESTION|MB_YESNO"
,,A_ScriptHwnd)=7) ; Different way to check
Goto,ExitLoop
ExitApp
Code: Select all
; MsgBox Class for AutoHotkey
Debug(title,text,styles:=0,delim:="|",hwnd*){
Return New MsgBox(title,text
, !hwnd[1]
? WinExist(A_ScriptName)
: !IsNum(hwnd[1])
? WinExist(hwnd[1],hwnd[2],hwnd[3],hwnd[4])
: hwnd[1]
, styles,delim).Display()
}
IsNum(n){
Return (n+0)
}
HexToDec(hex){
chars:="0123456789abcdef"
If ! sub:=(SubStr(hex,1,2)="0x")
Return
newHex:=SubStr(hex,3)
Loop % StrLen(newHex)
If !InStr(chars,SubStr(newHex,A_Index,1))
Return
Return (SubStr(hex,1,2)="0x")?hex + 0:("0x" hex) + 0
}
LoadLib(lib){
If FileExist(lib)
Return DllCall("LoadLibrary","Str",lib,"Ptr")
}
Class MsgBox {
__New(_title,_msg:="",_hwnd:=0,_styles:="",delim:="|"){
this.Owner:=_hwnd
this.Title:=_title?_title:A_ScriptName
this.Message:=_msg
If (_styles!="" And !IsNum(_styles)){
SetFormat,Integer,H
hex_array:=[]
Loop,Parse,_styles,%delim%
hex_array.Push(HexToDec(this.Styles[A_LoopField]))
For idx, hex in hex_array
new+=hex
this.Style:=new
SetFormat,Integer,D
} Else If IsNum(_styles)
this.Style:=_styles
Else this.Style := ( this.Styles["MB_OK"]
+ this.Styles["MB_ICONINFORMATION"] )
}
Clear(){
this.Remove("",Chr(255))
this.SetCapacity(0)
this.base:=""
Return ! IsObject(this.base)
}
Display(_title:="",_msg:="",_hwnd:="",_styles:=""){
If lib:=LoadLib(A_WinDir . "\System32\User32.dll"){
ret:=DllCall("MessageBox" ,"Uint", _hwnd?_hwnd:this.Owner
,"Str", _msg?_msg:this.Message
,"Str", _title?_title:this.Title
,"Uint", _styles?_styles:this.Style)
DllCall("FreeLibrary","Ptr",lib)
Return ret
}
}
Styles:= {"MB_ABORTRETRYIGNORE":"0x00000002","MB_CANCELTRYCONTINUE":"0x00000006"
,"MB_HELP":"0x00004000","MB_OK":"0x00000000"
,"MB_OKCANCEL":"0x00000001","MB_RETRYCANCEL":"0x00000005"
,"MB_YESNO":"0x00000004","MB_YESNOCANCEL":"0x00000003"
,"MB_ICONEXCLAMATION":"0x00000030","MB_ICONWARNING":"0x00000030"
,"MB_ICONINFORMATION":"0x00000040","MB_ICONASTERISK":"0x00000040"
,"MB_ICONQUESTION":"0x00000020","MB_ICONSTOP":"0x00000010"
,"MB_ICONERROR":"0x00000010","MB_ICONHAND":"0x00000010"
,"MB_DEFBUTTON1":"0x00000000","MB_DEFBUTTON2":"0x00000100"
,"MB_DEFBUTTON3":"0x00000200","MB_DEFBUTTON4":"0x00000300"
,"MB_APPLMODAL":"0x00000000","MB_SYSTEMMODAL":"0x00001000"
,"MB_TASKMODAL":"0x00002000","MB_DEFAULT_DESKTOP_ONLY":"0x00020000"
,"MB_RIGHT":"0x00080000","MB_RTLREADING":"0x00100000"
,"MB_SETFOREGROUND":"0x00010000","MB_TOPMOST":"0x00040000"
,"MB_SERVICE_NOTIFICATION":"0x00200000"}
}