CoordMode Mouse, "Screen" VS "Relative"

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Cr8zy_Ivan
Posts: 131
Joined: 30 Mar 2019, 18:20

CoordMode Mouse, "Screen" VS "Relative"

06 Jan 2020, 01:17

I'm trying to Minimize a Window, that is Not Active, by Right-Clicking on the Titlebar. The problem is with "Relative" CoordMode, where it takes 2 Right-Clicks to minimize. When using "Screen" CoordMode, this function works as it should (with 1 Right-Click). I've tried WinActivate, I've verified if the WinID is the right one (it is). For the life of me, I do not know why Relative Coord Mode does not work properly. Can anybody help?

Code: Select all

#If MouseIsOverTitlebar()
RButton::
{
	MouseGetPos,,, WinID
	WinMinimize, ahk_id %WinID%
	Return
}
#If

MouseIsOverTitlebar() 
{
		; CoordMode Mouse, Relative
	CoordMode Mouse, Screen
	MouseGetPos,, MouseY, WinID
	CaptionBarSize:=30

		; WinActivate, ahk_id %WinID%
		; msgbox, WinID %WinID%

	If (MouseY <= CaptionBarSize && MouseY >= 0)
	{
		Return True
	}
}
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: CoordMode Mouse, "Screen" VS "Relative"

06 Jan 2020, 01:48

I could be wrong, but my guess is that "Relative" is referring to the *active* window. Whereas "Screen" is referring to the entire screen and any/all windows on it.

After testing the code, it indeed works on the active window (when using Relative), but not an inactive window. Screen works on both.

But the funny thing I noticed is that when using Relative, it only works on the active window - except when right-clicking on the title bar of any Google Chrome window - which in that case, it *does* work. Strange.



*Edit - and now on a third try, it works intermittently on any window when using Relative. I can't see a pattern either, it's just random...
User avatar
Cr8zy_Ivan
Posts: 131
Joined: 30 Mar 2019, 18:20

Re: CoordMode Mouse, "Screen" VS "Relative"

06 Jan 2020, 03:52

Hehe, hanks for the feedback!
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: CoordMode Mouse, "Screen" VS "Relative"

06 Jan 2020, 09:48

sorry, hopefully one of the experts here can chime in. When using Relative, and if the active window is something other than Chrome, then right-clicking on a Chrome window will minimize that Chrome window. But if the active window is a notepad window, then right-clicking on any other notepad window (or explorer window, etc) will not work. That part is strange.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: CoordMode Mouse, "Screen" VS "Relative"

06 Jan 2020, 14:31

Any function that's executed to evaluate for the down event of a conditional hotkey (such as RButton::) will evaluate prior to passing through the event (assuming there's no difference in process elevation). So when RButton is pressed, the window it hovers over isn't activated yet, so the position relative to the active window is determined, that's not necessarily the window the cursor is hovering over.
If MouseIsOverTitlebar() evalutes to false, RButton passes through, the window actives, the next RButton press will evaluate to true and minimize the window.

The occasional successful minimize of an inactive window must mean MouseIsOverTitlebar() evaluated to true. So the titlebars are likely lined up horizontally, since you're only checking the y coordinate.

What's keeping you from using CoordMode Mouse, Screen?
User avatar
Cr8zy_Ivan
Posts: 131
Joined: 30 Mar 2019, 18:20

Re: CoordMode Mouse, "Screen" VS "Relative"

06 Jan 2020, 15:38

It's because I have 2 rows of monitors. When clicking on the title bar of windows that are on the lower row, then the coordinates become 1080 (which I could also implement mind you). The thing is, sometimes your windows are all over the place, putting your titlebar all over the place as well. That's why I'd like to use Relative... or some other function or fix, if anyone knows of one.

Cheers!
just me
Posts: 9559
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: CoordMode Mouse, "Screen" VS "Relative"

07 Jan 2020, 06:30

Code: Select all

#NoEnv
Return

#If MouseOnTitleBar()
RButton::
   WinGetTitle, Title ; uses the'last found window'.
   MsgBox, 0, Bingo!, And the winner is: %Title%
#If

MouseOnTitleBar() {
   ; WM_NCHITTEST -> docs.microsoft.com/en-us/windows/win32/inputdev/wm-nchittest
   ; HTCAPTION = 2
   CoordMode, Mouse, Screen
   MouseGetPos, MX, MY, HWND
   SendMessage, 0x0084, 0, (MX & 0xFFFF) | ((MY & 0xFFFF) << 16), , ahk_id %HWND%
   Return (ErrorLevel = 2) ? WinExist("ahk_id " . HWND) : 0 ; sets the 'last found window'.
}
?
User avatar
Cr8zy_Ivan
Posts: 131
Joined: 30 Mar 2019, 18:20

Re: CoordMode Mouse, "Screen" VS "Relative"

08 Jan 2020, 19:23

Oh Boy, the internet is smarter than I thought. You guys are probably going to hate me. But I'm already using this script. One thing I've noticed however, is this script does not work when MinMax = 0 and the mouse cursor is between y <= 4 && y >=0 (when the cursor changes to the resizing icon). And so, I'm trying to add a "patch script" to fill in that gap. It's nothing major, but I have a perfectionist nature... (which also plagues my life :shock:)

My whole script is this:

Code: Select all

#If MouseIsOverTitlebar()
RButton::
{
	MouseGetPos,, MouseYMin, WinID
	WinMinimize, ahk_id %WinID%
	Return
}

MButton::
{
	MouseGetPos,, MouseYCLS, WinID
	WinGet, PNameWinCLS, ProcessName, ahk_id %WinID%
	If (PNameWinCLS == "EXCEL.EXE" OR PNameWinCLS == "Winword.EXE" OR PNameWinCLS == "WINWORD.EXE")
	{
		Send !{F4}
		Return
	}
	Else If (PNameWinCLS == "NordVPN.exe")
	{
		SendMessage, 0x02	; WM_DESTROY
		Return
	}
	Else
	{
		WinClose, ahk_id %WinID%
		Return
	}
}
#If

MouseIsOverTitlebar()
{
	; CoordMode Mouse, Relative
	CoordMode Mouse, Screen
	MouseGetPos,, MouseY, WinID
	WinGet, PNameWinMin, ProcessName, ahk_id %WinID%
	WinGet, WinMMax, MinMax, ahk_id %WinID%
	Static WM_NCHITTEST := 0x84
	HTCAPTION := 2
	CaptionBarSize:=30

	If ((PNameWinMin == "lync.exe" OR PNameWinMin == "acadlt.exe" OR PNameWinMin == "acad.exe") AND (MouseY < CaptionBarSize && MouseY >= 0))
	{
		Return True
	}
	Else If (WinMMax == 0 AND MouseY == 0)
	; Else If (WinMMax == 0 AND (MouseY <= 4 && MouseY >= 0))
	; Else If (WinMMax == 0 AND MouseY == 0) ; (MouseY <= 4 && MouseY >= 0))
	{
		; msgbox, 1
		Return True
	}
	Else
	{
		; msgbox, 12
		CoordMode Mouse, Screen
		MouseGetPos x, y, w
		If WinExist("ahk_class Shell_TrayWnd ahk_id " w)  ; Exclude taskbar.
		{
			Return False
		}
		SendMessage WM_NCHITTEST,, (x & 0xFFFF) | ((y & 0xFFFF) << 16),, ahk_id %w%
		WinExist("ahk_id " w)  ; Set Last Found Window for convenience.
		Return ErrorLevel = HTCAPTION
	}
}

Some credit goes to seabreeze
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=63022&p=269207&hilit=titlebar+middle+click#p269207

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], mstrauss2021 and 105 guests