MoveWindowUnderMouseCursor() with a hotkey

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

MoveWindowUnderMouseCursor() with a hotkey

28 Aug 2020, 06:56

The script:

Code: Select all

MoveWindowUnderMouseCursor(Except:="Progman WorkerW Shell_TrayWnd") {  ;    By SKAN on D38S/D38S
Local                                            ; @ autohotkey.com/boards/viewtopic.php?t=80416
  MouseGetPos,,, hWnd
  WinGetClass, Class, % "ahk_id" . WinExist("ahk_id" . hWnd)
  If ( DllCall("IsZoomed", "Ptr",hWnd)  ||  InStr(" " . Except . " ", " " . Class . " ", True) )
    Return
  WinActivate
  WinWaitActive,,, 0
  PostMessage, 0x112, 0xF010                                       ;      WM_SYSCOMMAND, SC_MOVE
  SendEvent ^{Down}
}

#NoEnv
#SingleInstance, Force
^#RButton:: MoveWindowUnderMouseCursor()
 
Usage: 
Run the script.
Use Hotkey Ctrl+Win+Right click when the mouse cursor is (anywhere) over your target window.
Release keys/mouse button and move mouse to relocate the window.
Left click (or press Enter) to confirm or Esc to Cancel.
 
Notes:
This script can move windows including those that don't have a caption or are fullscreen.
To move window of an elevated process, the script should also be run elevated.
User avatar
SirSocks
Posts: 208
Joined: 26 Oct 2018, 08:14

Re: MoveWindowUnderMouseCursor() with a hotkey

28 Aug 2020, 09:54

This function seems very beneficial for building applications that include ease of access. For me, It seems like it doesn't work on windows that are maximized. For example, If Chrome is maximized the script will do nothing. If Chrome is restored the script will function as intended.
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: MoveWindowUnderMouseCursor() with a hotkey

28 Aug 2020, 11:08

SirSocks wrote:
28 Aug 2020, 09:54
It seems like it doesn't work on windows that are maximized. For example, If Chrome is maximized the script will do nothing. If Chrome is restored the script will function as intended.
 

This was the intended effect.
The DllCall("IsZoomed", "Ptr",hWnd) in the code checks if the target window is maximized.
Non-DllCall equivalent will be

Code: Select all

WinGet, OutputVar, MinMax, ahk_id %hWnd%
If (OutputVar=1) ; Window is Maximized
  Return
You may remove it, but I don't recommend. For me, MS Edge browser refuses to budge when it is maximized.
 

For any normal window with caption, pressing key combo Alt+Space will reveal its System Menu.
If you press M thereby selecting Move from the menu, you may move the window with arrow keys.
But if you Maximize a window, (for eg. Notepad) and press key combo Alt+Space, you will see that menu item Move is grayed-out.
 
  
WinMax.png
WinMax.png (7.5 KiB) Viewed 312 times
 
We can't access the System menu of a window that doesn't have a caption, for example:

Code: Select all

Gui -Caption +Border
Gui, Add, Edit, w200 h100, Hello World
Gui, Show
This is where MoveWindowUnderMouseCursor() will really help.
The function calls PostMessage, 0x112, 0xF010 which simply tells a window that Move was selected from its system menu.

:thumbup: :)
User avatar
SirSocks
Posts: 208
Joined: 26 Oct 2018, 08:14

Re: MoveWindowUnderMouseCursor() with a hotkey

28 Aug 2020, 11:26

Excellence clarification. Thank you!
need4speed
Posts: 132
Joined: 22 Apr 2016, 06:50

Re: MoveWindowUnderMouseCursor() with a hotkey

28 Aug 2020, 17:29

for me the implementation of the move command in Windows is a complete failure (bug).

In Linux after selecting move, a window can be moved.
for years, already in Win3 (i guess) - Win10, invoking the move command changes only the mouse icon, the window does not follow the mouse movement.
Image

after pressing once the cursor left/right/up/down, the window is tied to the mouse.
now correct me if I'm wrong, but this behavior makes no sense. :headwall:
the same applies to (re)size.
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: MoveWindowUnderMouseCursor() with a hotkey

28 Aug 2020, 18:42

need4speed wrote:
28 Aug 2020, 17:29
correct me if I'm wrong, but this behavior makes no sense. :headwall:
the same applies to (re)size.
 
 
Raymond Chen wrote:Why do I have to hit an arrow key before a keyboard-initiated Move operation will follow the mouse?
https://devblogs.microsoft.com/oldnewthing/20140318-00/?p=1483


I don’t know, but I think it’s just a bug. Mind you, it’s a bug with extraordinary seniority (probably going as far back as Windows 1.0).

The Move and Size commands from the system menu are operated by the same common function, and the keyboard-initiated Size command requires you to hit an arrow in order to specify which edge you are trying to resize. The Move command doesn’t need to let you pick a side (since moving is independent of sides), but the common helper function waits for the arrow key regardless of the underlying operation.
zhotkey
Posts: 11
Joined: 21 Jun 2017, 08:22

Re: MoveWindowUnderMouseCursor() with a hotkey

29 Aug 2020, 11:33

This doesn't work for moving fullscreen windows for me.
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: MoveWindowUnderMouseCursor() with a hotkey

29 Aug 2020, 11:47

zhotkey wrote:
29 Aug 2020, 11:33
This doesn't work for moving fullscreen windows for me.
Which window? Doesn't work for me kiosk modes of Ms-edge or File explorer.
I had success only with media players like vlc and mpc-hc.
zhotkey
Posts: 11
Joined: 21 Jun 2017, 08:22

Re: MoveWindowUnderMouseCursor() with a hotkey

29 Aug 2020, 12:04

MS Edge, Google Chrome, File Explorer, MS Word, OneNote, VLC all don't work fullscreen on my 2 monitor setup. It also doesn't matter which monitor.
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: MoveWindowUnderMouseCursor() with a hotkey

29 Aug 2020, 12:19

zhotkey wrote:
29 Aug 2020, 12:04
MS Edge, Google Chrome, File Explorer, MS Word, OneNote, VLC
Wow.. Too many!.
I have redacted fullscreen from my post. Thanks for the feedback.

PS: I'm able to move VLC in fullscreen.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: AlexProof and 24 guests