Odd behavior for WinRestore and WinMaximize

Get help with using AutoHotkey and its commands and hotkeys
ippon
Posts: 3
Joined: 08 Jul 2017, 19:07

Odd behavior for WinRestore and WinMaximize

08 Jul 2017, 19:27

I am using AutoHotKey v1.1.14.04 on Windows 7. I am having a problem with WinMaximize and WinMinimize (as well as the PostMessage equivalents) for windows of a particular piece of software (eSignal 12). The problem does not happen for windows of any other software that I have tested (Outlook, Sonos, Chrome, etc). Also, it did not happen with the previous version of this software. I realize that this means it is very likely a bug with the software, and not an issue with the behavior of AutoHotKey.

Before outlining the problem, here are my goals in asking the question:
1) Has anyone run into similar behavior?
2) Are there any known workarounds?
3) This is so bizarre that I'm not even sure what information I could provide to the software vendor to help them discover the bug. Any ideas?

I have stripped the script down to the function that is causing the problem, and then stripped the function down to the bare bones. The function is supposed to toggle a window between the maximized state and the restored state. Here is the function:

Code: Select all

TestScript3()
{
	SetTitleMatchMode, 2
	CoordMode, Mouse, Relative

	varWinTitle := "GBPUSD 240"

	WinGet MX, MinMax, %varWinTitle%
	If MX=1
	{
		;WinRestore A
  		PostMessage, 0x112, 0xF120,,, %varWinTitle% ;restore

	}	
	Else if MX=0
	{ 
		;WinMaximize 
  		PostMessage, 0x112, 0xF030,,, %varWinTitle% ;maximize
	}

	return 1
}

F9::
TestScript3()
return
Expected behavior: If the window is not maximized and I press F9, then it maximizes. If the window is already maximized and I press F9, then it restores.

Actual behavior: If the window is not maximized and I press F9, it maximizes. If I manually restore the window (using my mouse), it will properly restore. Likewise, if I manually maximize the window (using my mouse), I hit F9 and it properly restores. However, if I press F9 to maximize the window, and then press F9 again later to restore the window, it does not restore-- it appears to try to restore and fail. The window very briefly goes to the restored size and then comes back to the maximized size. It is not actually maximized now, though, although it is the same size-- it appears to have resized to the size of the screen.

Again, I have tested this with other applications and it works as expected. This also worked fine on previous versions of the software. So, this is almost certain to be some very odd bug in the software.

Does anyone have any suggestions on a workaround? Also, is there any diagnostic/troubleshooting information I could look for (in regards to window state etc) that would be helpful to the software vendor? I have not contacted them yet because I realize without specific diagnostic/troubleshooting data points that isolate the software as the cause, and not AHK, they are very likely to blame AHK.

Thanks for any help/direction that anyone can provide!
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Odd behavior for WinRestore and WinMaximize

08 Jul 2017, 20:00

Welcome! :D

Here is a thought: I have run in the past across programs that change their window title when shown restored/minimized. It may also be possible to find programs that do that when maximized/minimized. Judging from how well your post is presented, I am certain you would have noticed if such an obvious behaviour were the case here. Did two similar looking (unicode?) character get mixed up here? I don't know if that is even possible with "GBPUSD 240", maybe a different blank char?

Anyhow, you could check if you can resolve the issue by using ahk_exe eSignal12.exe (assuming the name of the executable file).
I doubt it will help, but it is quick to check. Good luck!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Odd behavior for WinRestore and WinMaximize

08 Jul 2017, 20:37

Other possibilities are to include 'DetectHiddenWindows, On' in the script and/or the function.

Also, it might be that 'minimising' causes a window to be 'minimised to the system tray', the window would then be hidden. Some AutoHotkey commands only work on visible windows, unless DetectHiddenWindows is turned on (at which point those commands can handle hidden windows also).

==================================================

[EDIT:] OK font was a bit small, I misread maximise/minimise ...

I would play around with code like this, until I felt I understood what the problem was, and came up with a consistent workaround/hack.

I once had a similar issue with Media Player Classic, and hiding/showing the playlist, in the sense that it was quite awkward and counterintuitive, but eventually I worked out some successful hotkeys. Cheers.

Code: Select all

;auto-execute section
WinGet, hWnd, ID, Untitled - Notepad
;MsgBox, % hWnd
return

q::
WinGet, vWinMinMax, MinMax, % "ahk_id " hWnd
MsgBox, % vWinMinMax
return

w::WinMinimize, % "ahk_id " hWnd
e::WinMaximize, % "ahk_id " hWnd
r::WinRestore, % "ahk_id " hWnd
You may also want to use WinGetPos as a way to get the min/max/restore state. Possibly a two-stage restore/max action would be useful, or to use WinMove at some point, or possibly there is a way to set the window to max/restore state, prior to using WinMaximize/WinRestore.

I believe I have had issues at some point, to do with with max/restore, i.e. it looks like it's one state but it's actually the other, or something. Perhaps the window isn't getting notified properly of the change in state.

If all else fails, maybe another way might be to use the Acc library to try to invoke the max/restore button.

[EDIT:] Checking the source code, the following dll functions are used (which can be used via DllCall):
- IsZoomed/IsIconic to get the state
- ShowWindow to min/max/restore

Btw have you tried applying a max twice, or a restore twice?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Odd behavior for WinRestore and WinMaximize

08 Jul 2017, 21:26

OK here's something I would definitely consider doing, while experimenting, setting the max state on/off:

Code: Select all

q:: ;get window style/ExStyle
WinGet, vWinStyle, Style, A
WinGet, vWinExStyle, ExStyle, A
Clipboard := vWinStyle " " vWinExStyle
return

;tested on Notepad (Windows 7):
;WS_MAXIMIZE := 0x1000000 ;WS_MINIMIZE := 0x20000000
;0x14CF0000 0x00000110 ;normal state
;0x15CF0000 0x00000110 ;maximised state
;0x34CF0000 0x00000110 ;minimised state

w:: ;toggle maximised state (observe the max/restore icon change)
WinSet, Style, ^0x1000000, A ;WS_MAXIMIZE := 0x1000000
return
Last edited by jeeswg on 09 Jul 2017, 01:12, edited 1 time 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
lexikos
Posts: 8598
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Odd behavior for WinRestore and WinMaximize

08 Jul 2017, 23:16

Posting WM_SYSCOMMAND, SC_MAXIMIZE/SC_RESTORE, 0, as you do, should be exactly equivalent to selecting maximize/restore from the window's system menu (which you can normally activate with Alt+Space or by clicking its window icon). Normally, the system posts this message as a result of user input, and the application responds either with custom handling, or by passing the message to DefWindowProc, which then maximizes or restores the window.

Does the issue occur if you maximize/restore using the window's system menu (e.g. Alt+Space, x or r)?

When you click the maximize or restore buttons, lParam contains the position of the mouse. If clicking the buttons works but selecting the menu items with the keyboard does not, I would suggest passing the mouse position (in screen coordinates), combined into a DWORD: (x & 0xFFFF) | (y << 16).

Are the conditions the same when you press the hotkey as when you click the maximize/restore button? For instance, is the window active?
ippon
Posts: 3
Joined: 08 Jul 2017, 19:07

Re: Odd behavior for WinRestore and WinMaximize

10 Jul 2017, 10:04

Thanks for all the responses! I have gone through the recommended advice and it appears that here is the problem:
lexikos wrote:Posting WM_SYSCOMMAND, SC_MAXIMIZE/SC_RESTORE, 0, as you do, should be exactly equivalent to selecting maximize/restore from the window's system menu (which you can normally activate with Alt+Space or by clicking its window icon). Normally, the system posts this message as a result of user input, and the application responds either with custom handling, or by passing the message to DefWindowProc, which then maximizes or restores the window.

Does the issue occur if you maximize/restore using the window's system menu (e.g. Alt+Space, x or r)?

When you click the maximize or restore buttons, lParam contains the position of the mouse. If clicking the buttons works but selecting the menu items with the keyboard does not, I would suggest passing the mouse position (in screen coordinates), combined into a DWORD: (x & 0xFFFF) | (y << 16).

Are the conditions the same when you press the hotkey as when you click the maximize/restore button? For instance, is the window active?
When I use Alt+Space to access the window's system menu, and maximize/restore from there, the window does NOT behave normally. It behaves just like the script I pasted-- the window does not restore properly. So, double clicking the window's title bar (which is what I normally do to manually maximize/restore) must not actually be maximizing/restoring. It must be doing something different.

Since maximize/restore is obviously broken on the application, I'll contact the software vendor and let them know. This gives me something very concrete to go on that is completely independent of AHK, so it will be very easy to get them to fix it.

In the meantime I'll modify my script to send a double click to the title bar of the application. I had played with this initially but found it to be a little quirky at times-- this function is used to quickly maximize several windows on different monitors, so I may need to play with sleep settings.

Thanks again for all the help.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Odd behavior for WinRestore and WinMaximize

10 Jul 2017, 14:45

So the w and e scripts below, give the same results?

Code: Select all

q:: ;toggle enable/disable double-click to maximise/restore window
;i.e. when WS_MAXIMIZEBOX is off, you cannot toggle the maximised state by double-clicking the title bar
;WS_MAXIMIZEBOX := 0x10000
WinSet, Style, ^0x10000, A
return

;==================================================

w:: ;toggle maximised state via WinRestore/WinMaximize commands
WinGet, hWnd, ID, A
WinGet, vWinMinMax, MinMax, % "ahk_id " hWnd
if (vWinMinMax = 1)
	WinRestore, % "ahk_id " hWnd
else
	WinMaximize, % "ahk_id " hWnd
return

;==================================================

e:: ;toggle maximised state via WM_SYSCOMMAND
WinGet, hWnd, ID, A
WinGet, vWinMinMax, MinMax, % "ahk_id " hWnd
if (vWinMinMax = 1)
	;SC_RESTORE := 0xF120
	PostMessage, 0x112, 0xF120,,, % "ahk_id " hWnd ;WM_SYSCOMMAND := 0x112
else
	;SC_MAXIMIZE := 0xF030
	PostMessage, 0x112, 0xF030,,, % "ahk_id " hWnd ;WM_SYSCOMMAND := 0x112
return

;==================================================

r:: ;toggle maximised state manually
WinGet, hWnd, ID, A
WinGet, vWinMinMax, MinMax, % "ahk_id " hWnd
if (vWinMinMax = 1)
{
	WinSet, Style, -0x1000000, A ;WS_MAXIMIZE := 0x1000000
	WinMove, A, , 82, 20, 1138, 652
}
else
{
	WinSet, Style, +0x1000000, A ;WS_MAXIMIZE := 0x1000000
	WinMove, A, , -8, -8, 1296, 696
}
return
Btw using a window event hook might me useful.

This:
Change action upon double-clicking title bar - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/6656 ... title-bar/
Led to this:
python - windows: disable maximize window on double-click title bar, and minimize on clicking task bar - Stack Overflow
https://stackoverflow.com/questions/508 ... ze-on-clic
remove the WS_MINIMIZEBOX and WS_MAXIMIZEBOX styles.
Although it appears that only removing the WS_MAXIMIZEBOX style is necessary.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ippon
Posts: 3
Joined: 08 Jul 2017, 19:07

Re: Odd behavior for WinRestore and WinMaximize

13 Jul 2017, 11:26

jeeswg wrote:So the w and e scripts below, give the same results?
Sorry, I forgot to respond to this. Yes, the w and e scripts gave the same results -- the window maximizes, and then when it tries to restore it fails (instead appears to resize the window to a maximized state, so even a manual restore after that doesn't work, I have to manually resize etc). It's the same behavior as using "Restore" from the window menu, and the same result I was getting with my original script.

I've contacted the software vendor and haven't heard back yet, but at this point it's pretty obvious that it's an issue with the software so doubt they'll fix the issue.

Return to “Ask For Help”

Who is online

Users browsing this forum: andymbody, bartolllomeo, Google [Bot], HiSoKa, vaulopitor and 51 guests