-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_MsgBox.ahk
252 lines (199 loc) · 4.59 KB
/
class_MsgBox.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
global $MsgBoxActiveWindowID
global $setAlwaysOnTop
/** MsgBox
@method message( $params* ) show MsgBox
@method confirm( $params* ) show confirm box
@method input( $params* ) show input box
@method exit( $params* ) show MsgBox then exitScript
@method alwaysOnTop( Toggle:=true ) set msg box always on top, return this
*/
Class MsgBox
{
_params := []
_title := ""
_title_default := ""
_message := ""
_timeout := ""
_button := ""
_options := {}
_defaults := { "x":"", "y":"", "w":"", "h":128, "default":"" }
__New()
{
$setAlwaysOnTop := true
}
/** Show message box centered to window
@params
$title
$message
$timeout
*/
message( $params* )
{
this._setParams($params)
this._centerToWindow()
MsgBox,262144, % this._title , % this._message, % this._timeout
return this
}
/** Show confirm box centered to window
@param string $title
@param string $message
@param string $button 'yes|no'
@param integer $timeout
*/
confirm( $params* )
{
this._setParams($params)
this._centerToWindow()
if(RegExMatch( this._button, "i)yes" ))
MsgBox, 4, % this._title , % this._message, % this._timeout
else
MsgBox,260, % this._title , % this._message, % this._timeout
IfMsgBox, Yes
return true
}
/** Show input box
@param string $title
@param string $message
@param object $options ; { "x":int, "y":int, "w":int, "h"int:, "default":string, "font":string }
@param integer $timeout
@return string value || false if canceled
*/
input( $params* )
{
this._setParams($params)
this._centerToWindow()
InputBox, $value, % this._title, % this._message,, % this._options.w, % this._options.h, % this._options.x, % this._options.y,, % this._timeout, % this._options.default
if ErrorLevel
return false
else
return %$value%
}
/** message before script exit
*/
exit( $params* )
{
this._findParamsTitleAndMessage()
if( ! this._title )
this._title_default := "ERROR - " A_ScriptName ; set default error title
this.message($params*)
ExitApp
}
/**
*/
alwaysOnTop($toggle:=true)
{
$setAlwaysOnTop := $toggle
return this
}
/**
*/
_setParams( $params )
{
this._params := $params
this._setParamsDefaults()
this._findParamsTitleAndMessage()
this._findParamTimeout()
this._setButtonParam()
this._findOptionsObject()
}
/**
*/
_setParamsDefaults()
{
this._title := RegExReplace( A_ScriptName, "i)\.(ahk|exe)$", "" )
this._timeout := 0
this._button := "yes"
this._options := this._defaults
}
/** Set title and message
If 1 parameters then it is title
If 2 parameters then 1st is title, 2nd is message
*/
_findParamsTitleAndMessage()
{
$length := this._params.Length()
if($length==1)
this._message := this._params[1]
if($length>1){
this._title := this._isString(this._params[1]) ? this._params[1] : ""
this._message := this._isString(this._params[2]) ? this._params[2] : ""
}
/* If title is not defined
*/
if( this._title && ! this._message ){
this._message := this._title
this._title := this._title_default
}
}
/** Find options object
It is object in parameters
*/
_findParamTimeout()
{
For $i, $param in this._params
if $param is integer
this._timeout := $param
}
/** Find options object
It is integer in parameters
*/
_findOptionsObject()
{
For $i, $param in this._params
if( IsObject($param) )
this._options := $param
this._setOptionsDefaults()
}
/**
*/
_setOptionsDefaults()
{
For $option, $value in this._defaults
if( ! this._options[$option] )
this._options[$option] := this._defaults[$option]
}
/** set which button is selected
parameter is "yes|no"
*/
_setButtonParam()
{
For $i, $param in this._params
if( RegExMatch( $param, "i)^(yes|no)$", $button ) )
this._button := $button
}
/**
*/
_centerToWindow()
{
WinGet, $MsgBoxActiveWindowID, ID, A
OnMessage(0x44, "centerMsgToWinow")
}
/**
*/
_isString( $param )
{
if $param is number
return false
if ( IsObject($param) )
return false
return true
}
}
/* OnMessage Callback
*/
centerMsgToWinow($wParam)
{
;MsgBox,262144,wParam, %$wParam%,3
WinGetPos, $X, $Y, $W, $H, ahk_id %$MsgBoxActiveWindowID%
if ($wParam == 1027 || $wParam == 1031) { ; AHK_DIALOG
Process, Exist
DetectHiddenWindows, On
if WinExist("ahk_class #32770 ahk_pid " . ErrorLevel) {
WinGetPos,,, $mW, $mH, A
WinMove, ($W-$mW)/2 + $X, ($H-$mH)/2 + $Y -128
if( $setAlwaysOnTop )
WinSet, AlwaysOnTop, On, A
}
}
}