Save window size and move maximized windows as well Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
_Rapid_
Posts: 20
Joined: 19 Apr 2020, 10:40

Save window size and move maximized windows as well

22 Apr 2020, 03:42

Hello,

I have found a script on the AHK website that I find very interesting. This script enables the Alt Drag functionality known from KDE Linux. This is very handy for my everyday life.

Now I have two questions:
1.) I noticed that when I use the script to resize a window and close this resized window again, the new window does not keep the size. To go a little further: I opened the Explorer window and used the script to resize the window. If I close this window and then open the explorer again, the resized window does not stay permanently, so the window has its original size again. My question, is it possible to save the size? Because if I resize an explorer window as usual over the corners, windows remembers this. But why doesn't it do that with the script? I mean reduced in size is reduced in size, what makes the difference?

2.) Apparently the script does not work with maximized windows, is it possible to move maximized windows with the functionality? I tried this by commenting out the following line:

Code: Select all

if EWD_WinState = 0  ; Only if the window isn't maximized
But this did not help...


Code: Select all

; Easy Window Dragging (requires XP/2k/NT)
; http://www.autohotkey.com
; Normally, a window can only be dragged by clicking on its title bar.
; This script extends that so that any point inside a window can be dragged.
; To activate this mode, hold down CapsLock or the middle mouse button while
; clicking, then drag the window to a new position.

; Note: You can optionally release CapsLock or the middle mouse button after
; pressing down the mouse button rather than holding it down the whole time.
; This script requires v1.0.25+.

~MButton & LButton::
CapsLock & LButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin% 
if EWD_WinState = 0  ; Only if the window isn't maximized 
	SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return

EWD_WatchMouse:
GetKeyState, EWD_LButtonState, LButton, P
if EWD_LButtonState = U  ; Button has been released, so drag is complete.
{
	SetTimer, EWD_WatchMouse, Off
	return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D  ; Escape has been pressed, so drag is cancelled.
{
	SetTimer, EWD_WatchMouse, Off
	WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
	return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX  ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Save window size and move maximized windows as well

22 Apr 2020, 05:47

_Rapid_ wrote: is it possible to save the size? Because if I resize an explorer window as usual over the corners, windows remembers this. But why doesn't it do that with the script? I mean reduced in size is reduced in size, what makes the difference?
Apparently, Windows saves the new size of an Explorer window during a resizing event, which does not occur if you just resize it directly. So it recognizes when one of its corners or edges is being dragged, and that's when it saves the new size for the next time a window is opened. It might be possible to send the window the Windows messages to make it think it's been resized through dragging, but I think that would be difficult. One way you could get it to save the new size is to have the script actually resize the window by dragging one of its corners. First, get the current size and position of the window and calculate how much you'd have to drag one of the corners to get it to the size you want. Then use MouseClickDrag to drag one of the corners so it resizes it.
_Rapid_ wrote: is it possible to move maximized windows with the functionality?
You could detect if a window is maximized using the MinMax sub-command of WinGet, then use WinRestore to unmaximize it, which would then allow you to move it. You could even skip checking whether it's maximized or not and just always do a WinRestore first since it doesn't change anything if a window is already in a normal (unmaximized) state.
_Rapid_
Posts: 20
Joined: 19 Apr 2020, 10:40

Re: Save window size and move maximized windows as well

22 Apr 2020, 11:22

Thanks for your answer, so the last part was implemented like this and see it works!

To the first part I have another question: I found the command to change the window:

Code: Select all

#q::
	MouseClickDrag, left, 0, 200, 2, 2
My problem is that I don't quite know how to implement it in my process. The process for resizing the window is as follows: I press and hold Alt + left mouse button, then I can resize the window with the mouse, now it would be great if you knew that the two keys are no longer held down and then once e.g. MouseClickDrag is executed... So far I have solved this by programming a second hotkey which adjusts the selected window. But these are two hotkeys for one function...

For resizing of the window I used:

Code: Select all

!RButton::
If DoubleAlt
{
    MouseGetPos,,,KDE_id
    ; Toggle between maximized and restored state.
    WinGet,KDE_Win,MinMax,ahk_id %KDE_id%
    If KDE_Win
        WinRestore,ahk_id %KDE_id%
    Else
        WinMaximize,ahk_id %KDE_id%
    DoubleAlt := false
    return
}
MouseGetPos,KDE_X1,KDE_Y1,KDE_id
WinGet,KDE_Win,MinMax,ahk_id %KDE_id%
WinGetPos,KDE_WinX1,KDE_WinY1,KDE_WinW,KDE_WinH,ahk_id %KDE_id%
If (KDE_X1 < KDE_WinX1 + KDE_WinW / 2)
    KDE_WinLeft := 1
Else
    KDE_WinLeft := -1
If (KDE_Y1 < KDE_WinY1 + KDE_WinH / 2)
    KDE_WinUp := 1
Else
    KDE_WinUp := -1
Loop
{
    GetKeyState,KDE_Button,RButton,P ;
    If KDE_Button = U
        break
    MouseGetPos,KDE_X2,KDE_Y2 ; Get the current mouse position.
    WinGetPos,KDE_WinX1,KDE_WinY1,KDE_WinW,KDE_WinH,ahk_id %KDE_id%
    KDE_X2 -= KDE_X1 ; Obtain an offset from the initial mouse position.
    KDE_Y2 -= KDE_Y1
    ; Then, act according to the defined region.
    WinMove,ahk_id %KDE_id%,, KDE_WinX1 + (KDE_WinLeft+1)/2*KDE_X2  ; X of resized window
                            , KDE_WinY1 +   (KDE_WinUp+1)/2*KDE_Y2  ; Y of resized window
                            , KDE_WinW  -     KDE_WinLeft  *KDE_X2  ; W of resized window
                            , KDE_WinH  -       KDE_WinUp  *KDE_Y2  ; H of resized window
    KDE_X1 := (KDE_X2 + KDE_X1) ; Reset the initial position for the next iteration.
    KDE_Y1 := (KDE_Y2 + KDE_Y1)
}
return
I tried it with:

Code: Select all

GetKeyState,KDE_Button,RButton,P ; Break if button has been released.
    if (KDE_Button = "U")
	{
		MouseClickDrag, left, 0, 200, 2, 2
        break
	}
This almost works as well, but then it jumps to another window, because the changed window is no longer active when you right-click...
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Save window size and move maximized windows as well  Topic is solved

22 Apr 2020, 13:05

_Rapid_ wrote: To the first part I have another question: I found the command to change the window:

Code: Select all

#q::
	MouseClickDrag, left, 0, 200, 2, 2
How did you come to using the coordinates 0,200 and 2,2?

The way I think you should do it is after your code resizes the window however it normally does using WinMove, then just drag the corner of the active window a couple of pixels and drag it back. That way it was resized by dragging even though this part of the resizing had no net effect on the size. To do that, you could add this immediately after a WinMove command in your script:

Code: Select all

CoordMode, Mouse, Window
MouseClickDrag, Left, 0, 0, 2, 2, 0, R
MouseClickDrag, Left, 0, 0, -2, -2, 0, R
CoordMode, Mouse, Screen ; return mode to screen if that's what it was
It acts on whatever window is active, so whatever window you moved with WinMove, make sure to activate it before this.
_Rapid_
Posts: 20
Joined: 19 Apr 2020, 10:40

Re: Save window size and move maximized windows as well

24 Apr 2020, 04:44

@boiler big thanks to you! Your suggested code was something that came to my mind too and see it works! I have also improved the script a bit, for the case if one wants to resize a window which is in the backgrund (I make it active now). But one thing is not working as you suggested the "R" parameter in your code (MouseClickDrag, Left, 0, 0, 2, 2, 0, R) does not work in my implementation, but I dont know why...

Here is my final code, please let me know if one sees improvements here!

Code: Select all

; Description:
; Alt + Right Mouse Button --> Resizes a Window by holding key combo
!RButton::
	resizeWindow()
	return

; Description:
; Resizes a Window by holding key combo
; does not work for maximized windows!
resizeWindow()
{
	MouseGetPos,KDE_X1,KDE_Y1,KDE_id
	WinGet,KDE_Win,MinMax,ahk_id %KDE_id%
	WinActivate, ahk_id %KDE_id% ; if window is not active make it active
	;MsgBox, %KDE_id%
	WinGet, active_id, ID, A
	WinGetTitle, Title, A
	If (KDE_Win)  ; abort if the window is maximized
	{
		return
	}
	; Get the initial window position and size.
	WinGetPos,KDE_WinX1,KDE_WinY1,KDE_WinW,KDE_WinH,ahk_id %KDE_id%
	; Define the window region the mouse is currently in.
	; The four regions are Up and Left, Up and Right, Down and Left, Down and Right.
	If (KDE_X1 < KDE_WinX1 + KDE_WinW / 2)
		KDE_WinLeft := 1
	Else
		KDE_WinLeft := -1
	If (KDE_Y1 < KDE_WinY1 + KDE_WinH / 2)
		KDE_WinUp := 1
	Else
		KDE_WinUp := -1
	Loop
	{
		GetKeyState,KDE_Button,RButton,P ; Break if button has been released.
		if (KDE_Button = "U")
		{	
			CoordMode, Mouse, Window
			MouseClickDrag, Left, 0, 0, 2, 2, 0
			MouseClickDrag, Left, 0, 0, -2, -2, 0
			CoordMode, Mouse, Screen ; return mode to screen if that's what it was
			return
		}
		
		MouseGetPos,KDE_X2,KDE_Y2 ; Get the current mouse position.
		; Get the current window position and size.
		WinGetPos,KDE_WinX1,KDE_WinY1,KDE_WinW,KDE_WinH,ahk_id %KDE_id%
		KDE_X2 -= KDE_X1 ; Obtain an offset from the initial mouse position.
		KDE_Y2 -= KDE_Y1
		; Then, act according to the defined region.
		WinMove,ahk_id %KDE_id%,, KDE_WinX1 + (KDE_WinLeft+1)/2*KDE_X2  ; X of resized window
								, KDE_WinY1 +   (KDE_WinUp+1)/2*KDE_Y2  ; Y of resized window
								, KDE_WinW  -     KDE_WinLeft  *KDE_X2  ; W of resized window
								, KDE_WinH  -       KDE_WinUp  *KDE_Y2  ; H of resized window
		KDE_X1 := (KDE_X2 + KDE_X1) ; Reset the initial position for the next iteration.
		KDE_Y1 := (KDE_Y2 + KDE_Y1)
		
	}
	return
}
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Save window size and move maximized windows as well

24 Apr 2020, 07:15

Oh, yeah. I shouldn’t have included R to make it relative in this case because that would make it start dragging wherever the mouse pointer is, and it really is supposed to start at 0,0 relative to the active window. We know that’s a corner of the window, so dragging it will resize it.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, Google [Bot], RandomBoy, Rohwedder and 353 guests