forked from chaohershi/cclose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CClose.ahk
404 lines (373 loc) · 14 KB
/
CClose.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#NoEnv ; recommended for performance and compatibility with future AutoHotkey releases
#SingleInstance ignore ; allow only one instance of this script to be running
SendMode Input ; recommended for new scripts due to its superior speed and reliability
SetWorkingDir %A_ScriptDir% ; ensures a consistent starting directory
ScriptName := "CClose"
ScriptVersion := "1.3.11.0"
CopyrightNotice := "Copyright (c) 2018-2019 Chaohe Shi"
ConfigDir := A_AppData . "\" . ScriptName
ConfigFile := ConfigDir . "\" . ScriptName . ".ini"
LangFile := "lang.ini"
; set the script language
if (A_Language == "0804") ; https://autohotkey.com/docs/misc/Languages.htm
{
Language := "Chinese"
}
else ; use English by default
{
Language := "English"
}
; set the script texts
IniRead, TEXT_Suspend, %LangFile%, %Language%, TEXT_Suspend, Suspend
IniRead, TEXT_Settings, %LangFile%, %Language%, TEXT_Settings, Settings
IniRead, TEXT_Help, %LangFile%, %Language%, TEXT_Help, Help
IniRead, TEXT_About, %LangFile%, %Language%, TEXT_About, About
IniRead, TEXT_Exit, %LangFile%, %Language%, TEXT_Exit, Exit
IniRead, TEXT_Update, %LangFile%, %Language%, TEXT_Update, Update
IniRead, TEXT_Close, %LangFile%, %Language%, TEXT_Close, Close
IniRead, TEXT_Checking_For_Updates, %LangFile%, %Language%, TEXT_Checking_For_Updates, Checking for updates...
IniRead, TEXT_Updater_Not_Found, %LangFile%, %Language%, TEXT_Updater_Not_Found, Updater not found!
IniRead, TEXT_Always_On_Top, %LangFile%, %Language%, TEXT_Always_On_Top, Always on top
IniRead, TEXT_Not_Always_On_Top, %LangFile%, %Language%, TEXT_Not_Always_On_Top, Not always on top
IniRead, TEXT_MenuAutostart, %LangFile%, %Language%, TEXT_MenuAutostart, Run %ScriptName% on system startup
IniRead, TEXT_MenuTitleBarMiddleClick, %LangFile%, %Language%, TEXT_MenuTitleBarMiddleClick, Middle click on title bar to close window
IniRead, TEXT_MenuTitleBarRightClick, %LangFile%, %Language%, TEXT_MenuTitleBarRightClick, Right click on title bar to minimize window
IniRead, TEXT_MenuTitleBarHoldLeftClick, %LangFile%, %Language%, TEXT_MenuTitleBarHoldLeftClick, Hold left click on title bar to toggle window always on top
IniRead, TEXT_MenuEscKeyDoublePress, %LangFile%, %Language%, TEXT_MenuEscKeyDoublePress, Double press Esc key to close active window
IniRead, TEXT_MenuTaskbarButtonRightClick, %LangFile%, %Language%, TEXT_MenuTaskbarButtonRightClick, Right click on taskbar button to move pointer to "Close window"
IniRead, TEXT_HelpMsg1, %LangFile%, %Language%, TEXT_HelpMsg1, Middle click + title bar = close window
IniRead, TEXT_HelpMsg2, %LangFile%, %Language%, TEXT_HelpMsg2, Right click + title bar = minimize window
IniRead, TEXT_HelpMsg3, %LangFile%, %Language%, TEXT_HelpMsg3, Hold left click + title bar = toggle window always on top
IniRead, TEXT_HelpMsg4, %LangFile%, %Language%, TEXT_HelpMsg4, Double press + Esc key = close active window
IniRead, TEXT_HelpMsg5, %LangFile%, %Language%, TEXT_HelpMsg5, Right click + taskbar button = move pointer to "Close window"
TEXT_HelpMsg := TEXT_HelpMsg1 . "`n" . TEXT_HelpMsg2 . "`n" . TEXT_HelpMsg3 . "`n" . TEXT_HelpMsg4 . "`n" . TEXT_HelpMsg5
TEXT_AboutMsg := ScriptName . " " . ScriptVersion . "`n`n" . CopyrightNotice
; add the tray menu
Menu, Tray, NoStandard ; remove the standard menu items
Menu, Tray, Add, %TEXT_Suspend%, SuspendProgram
Menu, Tray, Default, %TEXT_Suspend% ; set the default menu item
Menu, Tray, Add
Menu, SettingMenu, Add, %TEXT_MenuAutostart%, AutostartProgram
Menu, SettingMenu, Add
Menu, SettingMenu, Add, %TEXT_MenuTitleBarMiddleClick%, ConfigSetting
Menu, SettingMenu, Add, %TEXT_MenuTitleBarRightClick%, ConfigSetting
Menu, SettingMenu, Add, %TEXT_MenuTitleBarHoldLeftClick%, ConfigSetting
Menu, SettingMenu, Add, %TEXT_MenuEscKeyDoublePress%, ConfigSetting
Menu, SettingMenu, Add, %TEXT_MenuTaskbarButtonRightClick%, ConfigSetting
Menu, Tray, Add, %TEXT_Settings%, :SettingMenu
Menu, Tray, Add
Menu, Tray, Add, %TEXT_Help%, ShowHelpMsg
Menu, Tray, Add, %TEXT_About%, ShowAboutMsg
Menu, Tray, Add
Menu, Tray, Add, %TEXT_Exit%, ExitProgram
Menu, Tray, Tip, %ScriptName% ; change the tray icon's tooltip
; store hotkeys and their corresponding menu items in an associative array
Hotkey1 := {KeyName: "MButton"
, KeyScope: "MouseIsOverTitlebar()"
, KeySettingName: "EnableTitleBarMiddleClick"
, KeySettingValue: EnableTitleBarMiddleClick := 1
, MenuItemName: TEXT_MenuTitleBarMiddleClick}
Hotkey2 := {KeyName: "RButton"
, KeyScope: "MouseIsOverTitlebar()"
, KeySettingName: "EnableTitleBarRightClick"
, KeySettingValue: EnableTitleBarRightClick := 1
, MenuItemName: TEXT_MenuTitleBarRightClick}
Hotkey3 := {KeyName: "~LButton"
, KeyScope: "MouseIsOverTitlebar()"
, KeySettingName: "EnableTitleBarHoldLeftClick"
, KeySettingValue: EnableTitleBarHoldLeftClick := 1
, MenuItemName: TEXT_MenuTitleBarHoldLeftClick}
Hotkey4 := {KeyName: "~Esc"
, KeyScope: "true"
, KeySettingName: "EnableEscKeyDoublePress"
, KeySettingValue: EnableEscKeyDoublePress := 1
, MenuItemName: TEXT_MenuEscKeyDoublePress}
Hotkey5 := {KeyName: "~RButton"
, KeyScope: "MouseIsOver(""ahk_class Shell_TrayWnd"")"
, KeySettingName: "EnableTaskbarButtonRightClick"
, KeySettingValue: EnableTaskbarButtonRightClick := 1
, MenuItemName: TEXT_MenuTaskbarButtonRightClick}
Hotkeys := [Hotkey1, Hotkey2, Hotkey3, Hotkey4, Hotkey5]
; retrieve the general settings
for index, element in Hotkeys
{
IniRead, KeySettingValue, %ConfigFile%, General, % element.KeySettingName, 1
element.KeySettingValue := KeySettingValue
}
; apply the general settings
for index, element in Hotkeys
{
if (!element.KeySettingValue)
{
Hotkey, If, % element.KeyScope
Hotkey, % element.KeyName, Off
}
else
{
Menu, SettingMenu, Check, % element.MenuItemName
}
}
; retrieve the autostart setting
IniRead, IsAutostart, %ConfigFile%, Autostart, EnableAutostart, 1
; apply the autostart setting if possible
if A_IsAdmin ; if run the script as administrator, apply the autostart setting
{
if IsAutostart
{
Menu, SettingMenu, Check, %TEXT_MenuAutostart% ; check the autostart menu item
RegWrite, REG_SZ, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, %ScriptName%, %A_ScriptFullPath% ; enable autostart
}
else
{
RegDelete, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, %ScriptName% ; disable autostart
}
}
else ; else update the autostart setting
{
RegRead, RegValue, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, %ScriptName% ; retrieve autostart status
if (RegValue == A_ScriptFullPath) ; if autostart is enabled
{
Menu, SettingMenu, Check, %TEXT_MenuAutostart% ; check the autostart menu item
IsAutostart := 1
}
else
{
IsAutostart := 0
}
Gosub, EnsureConfigDirExists
IniWrite, %IsAutostart%, %ConfigFile%, Autostart, EnableAutostart ; update the autostart setting
}
Return ; end of the auto-execute section
; ensure ConfigDir exists
EnsureConfigDirExists:
if !InStr(FileExist(ConfigDir), "D")
{
FileCreateDir, %ConfigDir%
}
Return
; config and apply the settings
ConfigSetting(ItemName, ItemPos, MenuName)
{
global ; use assume-global mode to access global variables
Menu, %MenuName%, ToggleCheck, %ItemName%
for index, element in Hotkeys
{
if (ItemName == element.MenuItemName)
{
Hotkey, If, % element.KeyScope
Hotkey, % element.KeyName, Toggle
element.KeySettingValue := !element.KeySettingValue
Gosub, EnsureConfigDirExists
IniWrite, % element.KeySettingValue, %ConfigFile%, General, % element.KeySettingName
}
}
}
AutostartProgram:
IsAutostart := !IsAutostart
Gosub, EnsureConfigDirExists
IniWrite, %IsAutostart%, %ConfigFile%, Autostart, EnableAutostart
if A_IsAdmin ; if run the script as administrator, apply the autostart setting
{
Menu, SettingMenu, ToggleCheck, %TEXT_MenuAutostart%
if IsAutostart
{
RegWrite, REG_SZ, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, %ScriptName%, %A_ScriptFullPath% ; enable autostart
}
else
{
RegDelete, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, %ScriptName% ; disable autostart
}
}
; try restart and run the script as administrator
; https://autohotkey.com/docs/commands/Run.htm#RunAs
full_command_line := DllCall("GetCommandLine", "str")
if !(A_IsAdmin || RegExMatch(full_command_line, " /restart(?!\S)"))
{
try
{
if A_IsCompiled
{
Run *RunAs "%A_ScriptFullPath%" /restart
}
else
{
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
}
; if run as administrator failed, rollback the autostart setting
if !A_IsAdmin
{
IsAutostart := !IsAutostart
Gosub, EnsureConfigDirExists
IniWrite, %IsAutostart%, %ConfigFile%, Autostart, EnableAutostart
}
Return
SuspendProgram:
Menu, Tray, ToggleCheck, %TEXT_Suspend%
Suspend, Toggle
Return
ShowHelpMsg:
Process, Exist
DetectHiddenWindows, On
if WinExist(TEXT_Help . " ahk_class #32770 ahk_pid " . ErrorLevel) ; if the help message already exists
{
WinShow ; show the message window if it is hidden
WinActivate
}
else ; else display the help message
{
MsgBox, 0, %TEXT_Help%, %TEXT_HelpMsg%
}
Return
ShowAboutMsg:
Process, Exist
DetectHiddenWindows, On
if WinExist(TEXT_About . " ahk_class #32770 ahk_pid " . ErrorLevel) ; if the about message already exists
{
WinShow ; show the message window if it is hidden
WinActivate
}
else ; else display the about message
{
OnMessage(0x44, "WM_COMMNOTIFY") ; https://autohotkey.com/board/topic/56272-msgbox-button-label-change/?p=353457
MsgBox, 257, %TEXT_About%, %TEXT_AboutMsg%
IfMsgBox, OK
{
if FileExist("Updater.exe")
{
TrayTip, %ScriptName%, %TEXT_Checking_For_Updates%
Run %A_ScriptDir%\Updater.exe /A
Sleep, 1000
WinWait, ahk_exe Updater.exe, , 20
HideTrayTip() ; https://autohotkey.com/docs/commands/TrayTip.htm#Remarks
}
else
{
MsgBox, 48, %ScriptName%, %TEXT_Updater_Not_Found%
}
}
}
Return
ExitProgram:
ExitApp
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
Return
WM_COMMNOTIFY(wParam)
{
global ; use assume-global mode to access global variables
if (wParam == 1027) ; AHK_DIALOG
{
Process, Exist
DetectHiddenWindows, On
if WinExist(TEXT_About . " ahk_class #32770 ahk_pid " . ErrorLevel)
{
ControlSetText, Button1, &%TEXT_Update%
ControlSetText, Button2, &%TEXT_Close%
}
}
}
HideTrayTip()
{
TrayTip ; attempt to hide the TrayTip in the normal way
if (SubStr(A_OSVersion, 1, 3) == "10.") ; if the OS version is Windows 10
{
; temporarily removing the tray icon to hide the TrayTip
Menu, Tray, NoIcon
Sleep, 100
Menu, Tray, Icon
}
}
MouseIsOver(WinTitle)
{
MouseGetPos, , , win
Return, WinExist(WinTitle . " ahk_id " . win)
}
MouseIsOverTitlebar()
{
static WM_NCHITTEST := 0x84, HTCAPTION := 2
CoordMode, Mouse, Screen
MouseGetPos, x, y, win
if WinExist("ahk_class Shell_TrayWnd ahk_id " . win) ; exclude the taskbar
{
Return
}
SendMessage, WM_NCHITTEST, , x | (y << 16), , ahk_id %win%
WinExist("ahk_id " . win) ; set the last found window for convenience
Return, (ErrorLevel == HTCAPTION)
}
#If MouseIsOver("ahk_class Shell_TrayWnd") ; apply the following hotkey only when the mouse is over the taskbar
~RButton:: ; when right clicked
CoordMode, Mouse, Screen
MouseGetPos, xOld, yOld
Sleep, 500 ; wait for the Jump List to pop up, n.b., this line also helps to provide a uniform waiting experience
MouseGetPos, xNew, yNew
CoordMode, Mouse, Window
if (Abs(xNew - xOld) < 8 && Abs(yNew - yOld) < 8) ; if the mouse did not move much
{
Loop 6
{
if WinActive("ahk_class Windows.UI.Core.CoreWindow") ; if the Jump List pops up (right clicked on the taskbar app buttons)
{
WinGetPos, , , width, height ; get the size of the last found window (Jump List)
MouseMove, (width / 2), (height - 3 * width / 32), 1 ; move the mouse to the bottom of the Jump List ("Close window")
break
}
Sleep, 250 ; wait for more time
}
}
Return
; https://autohotkey.com/board/topic/82066-minimize-by-right-click-titlebar-close-by-middle-click/#entry521659
#If MouseIsOverTitlebar() ; apply the following hotkey only when the mouse is over title bars
MButton::PostMessage, 0x112, 0xF060 ; alternative to WinClose, as WinClose is a somewhat forceful method, e.g., if multiple Microsoft Excel instances exist, WinClose will close them all at once. 0x112 = WM_SYSCOMMAND, 0xF060 = SC_CLOSE https://autohotkey.com/docs/commands/WinClose.htm#Remarks
RButton::
KeyWait, RButton, T0.4 ; wait for the right mouse button to be released with timeout set to 0.4 second
if (ErrorLevel == 0) ; if the right mouse button is released during the timeout period, minimize the window
{
Send {Click} ; left click once to remove the remnant right click menu caused by previous clicks, n.b., do not use Send {LButton}, as it would behave inconsistently if the primary and secondary button have been swapped via the system's control panel
PostMessage, 0x112, 0xF020 ; alternative to WinMinimize
}
else ; else send a normal right click
{
Send {Click, Right} ; n.b., do not use Send {RButton}
}
Return
~LButton::
CoordMode, Mouse, Screen
MouseGetPos, xOld, yOld
WinGet, ExStyle, ExStyle ; get the extended window style
if (ExStyle & 0x8) ; 0x8 is WS_EX_TOPMOST
{
ExStyle := TEXT_Not_Always_On_Top
}
else
{
ExStyle := TEXT_Always_On_Top
}
KeyWait, LButton, T1 ; wait for the left mouse button to be released with timeout set to 1 second
MouseGetPos, xNew, yNew
if (xOld == xNew && yOld == yNew && ErrorLevel == 1) ; if the mouse did not move during the timeout period
{
Winset, Alwaysontop, Toggle, A ; toggle window always on top
ToolTip, %ExStyle%, , 0 ; display a tooltip with current topmost status
SetTimer, RemoveToolTip, 1000 ; remove the tooltip after 1 second
}
Return
#If, true ; apply the following hotkey with no conditions
~Esc::
WinGet, idOld, ID, A ; get the window id
KeyWait, Esc ; wait for the Esc key to be released
KeyWait, Esc, D, T0.4 ; wait for the Esc key to be pressed again
if (ErrorLevel == 0)
{
WinGet, idNew, ID, A ; get the window id after the Esc key has being pressed again
WinGetClass, class, A
if (idOld == idNew && class != "Shell_TrayWnd" && class != "Progman" && class != "WorkerW") ; if the current window is the same one as before and is not taskbar or desktop
{
Send !{F4}
}
}
Return