event hooks and blocking min/max/restore/close

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

event hooks and blocking min/max/restore/close

19 Jul 2017, 19:10

Is it possible via event hooks or some other method to block min/max/restore/close, when clicking on the title bar buttons, or to block max/restore when double-clicking on the title bar?

I know that you can write LButton/RButton hotkeys to capture the clicks but I want to avoid that. [See links in post lower down.]

I also know that you can change a window's style, to block min/max/restore/close etc, but I want to avoid that also. [See code in post lower down.]

In general I'm interested in what event hooks can actually do that's useful, or techniques for blocking the close button, or making close act like minimise.

One thing that event hooks can do is detect when a window is being moved, or when a window is activated, allowing you to then respond to this in some way, e.g. if a window is activated, you can remove any system tray icons you created for it.

Some links I've found re. event hooks:
Detect ALL NEW Windows INCLUDING All CHILD Windows - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=34719
ACC.AHK - Get IAccessible for Context menu - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 81#p159781
I need to make any window become transparent only when moved - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=28353
Snapping windows to screen edges - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 52#p122252
Last edited by jeeswg on 19 Jul 2017, 20:17, edited 4 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: event hooks and blocking min/max/restore/close

19 Jul 2017, 19:40

Shell hooks or event hooks won't allow you to do that.
What you need is to create a context sensitive hotkey (an #if expression) which actives only in the case the mouse is over the title bar or a given button in the title bar.
You can detect that with the WM_NCHITTEST message.

For example, you can block the double click on the title bar this way:

Code: Select all

mouseIsOverTitleBar(){
	CoordMode Mouse, Screen
	MouseGetPos X, Y, winUnderMouseID
	SendMessage WM_NCHITTEST, , X | (Y << 16), , ahk_id %winUnderMouseID%
	return ErrorLevel = HTCAPTION ? true : false
}

#if mouseIsOverTitleBar()
	LButton::
		;DO YOUR CUSTOM ACTION HERE
	return
#if
It's not a working example, but that's the basic structure.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: event hooks and blocking min/max/restore/close

19 Jul 2017, 20:01

Code to enable/disable the min/max/close buttons (and to toggle the max/restore state).

Code: Select all

q:: ;toggle enable/disable min button
WinSet, Style, ^0x20000, A ;WS_MINIMIZEBOX := 0x20000
return

w:: ;toggle enable/disable max button
;(and toggle enable/disable double-click to max/restore window)
WinSet, Style, ^0x10000, A ;WS_MAXIMIZEBOX := 0x10000
return

e:: ;toggle enable/disable close button
;SC_CLOSE := 0xF060, MF_BYCOMMAND := 0x0
;MF_ENABLED := 0x0, MF_GRAYED := 0x1, MF_DISABLED := 0x2
WinGet, hWnd, ID, A
hMenu := DllCall("GetSystemMenu", Ptr,hWnd, Int,0, Ptr)
vState := DllCall("GetMenuState", Ptr,hMenu, UInt,0xF060, UInt,0, UInt)
if (vState = 4294967295) ;0xFFFFFFFF
	DllCall("GetSystemMenu", Ptr,hWnd, Int,1, Ptr), vState := 0x0
vIsEnabled := !(vState & 0x2)
DllCall("EnableMenuItem", Ptr,hMenu, UInt,0xF060, UInt,vIsEnabled?0x3:0x0)
DllCall("DrawMenuBar", Ptr,hWnd)
if !vIsEnabled
	DllCall("GetSystemMenu", Ptr,hWnd, Int,1, Ptr)
return

r:: ;toggle max/restore style on/off
WinSet, Style, ^0x1000000, A ;WS_MAXIMIZE := 0x1000000
return
Cheers Getfree.

I hadn't thought of using the #If command, I'm so used to not using it. I would usually do something like: do a hit test, and then if the conditions aren't met, send LButton down and use KeyWait to emulate normal mouse activity, hence the reluctance to use LButton::. I suppose by using #If, AHK might do something tidier than that. E.g.:

Is it possible to catch the close button and minimize the window instead? AutoHotKey - Stack Overflow
https://stackoverflow.com/questions/398 ... o/41325497
AutoHotKey: Run code on Window Event (Close) - Stack Overflow
https://stackoverflow.com/questions/351 ... e/41404715
Send a window to fit on screen corners with ahk - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 40#p126340

Btw I'm not sure if WM_NCHITTEST is reliable these days, but this script can do something equivalent:
AccViewer Basic - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=32039
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: andymbody, bartolllomeo, Bing [Bot], Google [Bot], HiSoKa, rawskull7, Skrell, twiz, whereyomomsat_ and 49 guests