MsgBox Class

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Lateralus138
Posts: 49
Joined: 30 Aug 2015, 20:52
Location: Decatur, IL.
Contact:

MsgBox Class

02 Apr 2018, 20:24

Hello all, I've seen and created some MsgBox functions for a little more control, but thought I would write a class with more functionality. If you have something better, great, but I think some might find this useful and more options are always better I say. Below you will find an example script and the function script. Explanations are in the example, but, of course, feel free to question or comment.

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
Function Script

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"}
}
Last edited by Lateralus138 on 03 Apr 2018, 10:17, edited 3 times in total.

K̴̡̛̻̮̼͕̬̑̋̀̂͆͛̍̑͢ȩ̮̞͍̩̯̋̈͒͌̕ę̶͓̗͖͔̹̪͗̂̈͛̓͘p̠͉̙̟̒̊͌̐͘͘͟͡͞ S̸͖̖̮̞̥͇̖̓̌͛̽̿̓̊̓̾̚͜w͇̮͓̱͇̘̯͆̓͑̋̇̉͜͝i̢͔̝̳̻̱̋̾͐̾͗͊̀̕͜͡͡n̷̡͔̦̤̝̼̩̎͌̈́̀͛̄͆̎͠ǵ̸̘̝̭̦̠̗͖͌͐͑̑̿̅̈͜͜ḯ̡̬̥̙̩̼̪̑͆̿̌́n̛̼͎̲̬͇̲͉̗̞͊̓̃̂̈͝g̸͕̜͖̪͉͔̩̓̃̀̃͌̑̋̕͘.̪̜̜̜̯̂͂̈́͛̆͗̇̍̇.̟͔͍̙̜̫̗̂̿͛͋͋̈́̾̾̿͑.̡̣̟̝̭͉̦̪́̓̀͛̑̓̐̈͘͘

burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: MsgBox Class

03 Apr 2018, 07:44

Thank you for this,it looks useful. I use MagicBox by alguimist quite a bit at the moment.
Regards,
burque505
User avatar
Lateralus138
Posts: 49
Joined: 30 Aug 2015, 20:52
Location: Decatur, IL.
Contact:

Re: MsgBox Class

03 Apr 2018, 09:10

burque505 wrote:Thank you for this,it looks useful. I use MagicBox by alguimist...
You're welcome, I hadn't seen that before, but looks fill-featured, I'll have to check it out. I try to build my own stuff first before I use other's that's why I wrote this, but like I said; it's always good to have more choices :D Thanks for the feedback and option.

K̴̡̛̻̮̼͕̬̑̋̀̂͆͛̍̑͢ȩ̮̞͍̩̯̋̈͒͌̕ę̶͓̗͖͔̹̪͗̂̈͛̓͘p̠͉̙̟̒̊͌̐͘͘͟͡͞ S̸͖̖̮̞̥͇̖̓̌͛̽̿̓̊̓̾̚͜w͇̮͓̱͇̘̯͆̓͑̋̇̉͜͝i̢͔̝̳̻̱̋̾͐̾͗͊̀̕͜͡͡n̷̡͔̦̤̝̼̩̎͌̈́̀͛̄͆̎͠ǵ̸̘̝̭̦̠̗͖͌͐͑̑̿̅̈͜͜ḯ̡̬̥̙̩̼̪̑͆̿̌́n̛̼͎̲̬͇̲͉̗̞͊̓̃̂̈͝g̸͕̜͖̪͉͔̩̓̃̀̃͌̑̋̕͘.̪̜̜̜̯̂͂̈́͛̆͗̇̍̇.̟͔͍̙̜̫̗̂̿͛͋͋̈́̾̾̿͑.̡̣̟̝̭͉̦̪́̓̀͛̑̓̐̈͘͘

User avatar
Lateralus138
Posts: 49
Joined: 30 Aug 2015, 20:52
Location: Decatur, IL.
Contact:

Re: MsgBox Class

03 Apr 2018, 09:46

2 edits to the script above in the main class MsgBox (changes reflected in the OP): _styles:=0 to _styles:="" and Ok to OK
__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"] )
}

I'm also going to add the ability to change properties on Obj.Display() call.

K̴̡̛̻̮̼͕̬̑̋̀̂͆͛̍̑͢ȩ̮̞͍̩̯̋̈͒͌̕ę̶͓̗͖͔̹̪͗̂̈͛̓͘p̠͉̙̟̒̊͌̐͘͘͟͡͞ S̸͖̖̮̞̥͇̖̓̌͛̽̿̓̊̓̾̚͜w͇̮͓̱͇̘̯͆̓͑̋̇̉͜͝i̢͔̝̳̻̱̋̾͐̾͗͊̀̕͜͡͡n̷̡͔̦̤̝̼̩̎͌̈́̀͛̄͆̎͠ǵ̸̘̝̭̦̠̗͖͌͐͑̑̿̅̈͜͜ḯ̡̬̥̙̩̼̪̑͆̿̌́n̛̼͎̲̬͇̲͉̗̞͊̓̃̂̈͝g̸͕̜͖̪͉͔̩̓̃̀̃͌̑̋̕͘.̪̜̜̜̯̂͂̈́͛̆͗̇̍̇.̟͔͍̙̜̫̗̂̿͛͋͋̈́̾̾̿͑.̡̣̟̝̭͉̦̪́̓̀͛̑̓̐̈͘͘

burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: MsgBox Class

03 Apr 2018, 10:07

Thanks for the update!
User avatar
Lateralus138
Posts: 49
Joined: 30 Aug 2015, 20:52
Location: Decatur, IL.
Contact:

Re: MsgBox Class

03 Apr 2018, 10:24

Obj.Display() is now editable (reflected above):

Code: Select all

MsgBox.Display(        "New Title"           ; New title
               ,       "New  Text"          ; New text
               ,       WinExist("A")       ; New owner 
               ,       MsgBox.Styles["MB_YESNO] + MsgBox.Styles["MB_ICONQUESTION"]) ; New styles

K̴̡̛̻̮̼͕̬̑̋̀̂͆͛̍̑͢ȩ̮̞͍̩̯̋̈͒͌̕ę̶͓̗͖͔̹̪͗̂̈͛̓͘p̠͉̙̟̒̊͌̐͘͘͟͡͞ S̸͖̖̮̞̥͇̖̓̌͛̽̿̓̊̓̾̚͜w͇̮͓̱͇̘̯͆̓͑̋̇̉͜͝i̢͔̝̳̻̱̋̾͐̾͗͊̀̕͜͡͡n̷̡͔̦̤̝̼̩̎͌̈́̀͛̄͆̎͠ǵ̸̘̝̭̦̠̗͖͌͐͑̑̿̅̈͜͜ḯ̡̬̥̙̩̼̪̑͆̿̌́n̛̼͎̲̬͇̲͉̗̞͊̓̃̂̈͝g̸͕̜͖̪͉͔̩̓̃̀̃͌̑̋̕͘.̪̜̜̜̯̂͂̈́͛̆͗̇̍̇.̟͔͍̙̜̫̗̂̿͛͋͋̈́̾̾̿͑.̡̣̟̝̭͉̦̪́̓̀͛̑̓̐̈͘͘

Haxt0r Baxtor

Re: MsgBox Class

04 Apr 2018, 02:56

timing was amazing on this i'm working on a project and this is perfect big time saver
User avatar
Lateralus138
Posts: 49
Joined: 30 Aug 2015, 20:52
Location: Decatur, IL.
Contact:

Re: MsgBox Class

04 Apr 2018, 09:35

Haxt0r Baxtor wrote:timing was amazing on this i'm working on a project and this is perfect big time saver
Glad I could help.

K̴̡̛̻̮̼͕̬̑̋̀̂͆͛̍̑͢ȩ̮̞͍̩̯̋̈͒͌̕ę̶͓̗͖͔̹̪͗̂̈͛̓͘p̠͉̙̟̒̊͌̐͘͘͟͡͞ S̸͖̖̮̞̥͇̖̓̌͛̽̿̓̊̓̾̚͜w͇̮͓̱͇̘̯͆̓͑̋̇̉͜͝i̢͔̝̳̻̱̋̾͐̾͗͊̀̕͜͡͡n̷̡͔̦̤̝̼̩̎͌̈́̀͛̄͆̎͠ǵ̸̘̝̭̦̠̗͖͌͐͑̑̿̅̈͜͜ḯ̡̬̥̙̩̼̪̑͆̿̌́n̛̼͎̲̬͇̲͉̗̞͊̓̃̂̈͝g̸͕̜͖̪͉͔̩̓̃̀̃͌̑̋̕͘.̪̜̜̜̯̂͂̈́͛̆͗̇̍̇.̟͔͍̙̜̫̗̂̿͛͋͋̈́̾̾̿͑.̡̣̟̝̭͉̦̪́̓̀͛̑̓̐̈͘͘

carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: MsgBox Class

22 Jul 2018, 10:16

Can you incorporate all the features in "MagicBox by alguimist" into your class?
https://sourceforge.net/projects/magicbox-factory/

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 139 guests