Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Window Management: KDE-like resizing & dragging + some m


  • Please log in to reply
18 replies to this topic
aurelian
  • Guests
  • Last active:
  • Joined: --
Hi out there,

I discovered AHK few days ago, but I am already amazed how mighty this handy little tool is. Here is part of my Init script that I thought you might be interested in. I know such stuff has been posted before, but maybe you want to give it a try. :-)

; Smart Window Management Script

; This script is based on the Easy Window Dragging Script in the Showcase
; (http://www.autohotkey.com/docs/scripts/EasyWindowDrag.htm),
; but goes much further. Of course you are free to change the combinations to
; anything you like.
; 
; Author: Johannes Loehnert
; Contact: [email protected]
; Last Change: 2005-01-25
; AHK version: 1.0.25
;
; The script establishes the following combinations:
; - Lwin  + LB: Hold and drag to move window
; - TB    + LB: same
; - Lwin  + RB: Hold and drag to resize window (resize corner which is closest to cursor)
; - TB    + RB: same
; - LCtrl + LWin + LB: Minimize window
; - LCtr  + LWin + RB: Close Window
; - TB    + MB: click to minimize window, hold for 0.5 sec to close window
; All commands apply to the window under the mouse cursor.
; Abbreviations: LB, MB, RB, TB = left, middle, right, thumb button of mouse.
;
; Flaws so far:
;  - Thumb button stops working in normal way; disable TB combinations if you need.
;  - Code might be prettier :-)


;Minimize Window; when held over 0.5 sec: close window
XButton2 & MButton::      
CoordMode, Mouse
MouseGetPos, SWM_MouseStartX, SWM_MouseStartY, SWM_MouseWin
SetTimer, SMW_MidButtonClose, 500   ;<-- TIMER FOR TB+MB CLOSE
SMW_MidButtonCloseTimer=1
loop
{
	GetKeyState, SMW_MButtonState, MButton, P
	if SMW_MidButtonCloseTimer=0
		return
	if SMW_MButtonState=U
	{
		SetTimer, SMW_MidButtonClose, Off
		WinMinimize, ahk_id %SWM_MouseWin%
		SMW_MidButtonCloseTimer=0
		return
	}
}


SMW_MidButtonClose:
SMW_MidButtonCloseTimer=0
SetTimer, SMW_MidButtonClose, Off
WinClose, ahk_id %SWM_MouseWin%
return

; Move window on Lwin+LB or TB+LB; minimize window on LCtrl+Lwin+LB or LCtrl+TB+LB
XButton2 & LButton::
LWin & LBUtton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, SWM_MouseStartX, SWM_MouseStartY, SWM_MouseWin
GetKeyState, SMW_LCtrlState, LCtrl
if SMW_LCtrlState=D
	WinMinimize, ahk_id %SWM_MouseWin%
else
	SetTimer, SWM_WatchMouse_Move, 10 ; Track the mouse as the user drags it.
return

; resize window on Lwin+RB or TB+RB; minimize window on LCtrl+Lwin+RB or LCtrl+TB+RB
XButton2 & RButton:: ;resize or close
LWin & RButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, SWM_MouseStartX, SWM_MouseStartY, SWM_MouseWin
WinGetPos, SWM_WinX, SWM_WinY, SWM_WinW, SWM_WinH, ahk_id %SWM_MouseWin%
GetKeyState, SMW_LCtrlState, LCtrl
if SMW_LCtrlState=D
{
	WinClose, ahk_id %SWM_MouseWin%
	return
}	
SWM_ResizeTypeX=0
SWM_ResizeTypeY=0
if (SWM_MouseStartX < SWM_WinX+SWM_WinW/2)
	SWM_ResizeTypeX=1
if (SWM_MouseStartY < SWM_WinY+SWM_WinH/2)
	SWM_ResizeTypeY=1
SetTimer, SWM_WatchMouse_Resize, 10 ; Track the mouse as the user drags it.
return


SWM_WatchMouse_Move:
GetKeyState, SMW_LButtonState, LButton, P

if SMW_LButtonState = U ; Button has been released, so drag is complete.
{
	SetTimer, SWM_WatchMouse_Move, off
	return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, SWM_MouseX, SWM_MouseY
SWM_DeltaX = %SWM_MouseX%
SWM_DeltaX -= %SWM_MouseStartX%
SWM_DeltaY = %SWM_MouseY%
SWM_DeltaY -= %SWM_MouseStartY%
SWM_MouseStartX = %SWM_MouseX%  ; Update for the next timer call to this subroutine.
SWM_MouseStartY = %SWM_MouseY%
WinGetPos, SWM_WinX, SWM_WinY,,, ahk_id %SWM_MouseWin%
SWM_WinX += %SWM_DeltaX%
SWM_WinY += %SWM_DeltaY%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %SWM_MouseWin%,, %SWM_WinX%, %SWM_WinY%
return


SWM_WatchMouse_Resize:
GetKeyState, SMW_RButtonState, RButton, P

if SMW_RButtonState = U ; Button has been released, so drag is complete.
{
	SetTimer, SWM_WatchMouse_Resize, off
	return
}
; Otherwise, resize the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, SWM_MouseX, SWM_MouseY
SWM_DeltaX = %SWM_MouseX%
SWM_DeltaX -= %SWM_MouseStartX%
SWM_DeltaY = %SWM_MouseY%
SWM_DeltaY -= %SWM_MouseStartY%
SWM_MouseStartX = %SWM_MouseX%  ; Update for the next timer call to this subroutine.
SWM_MouseStartY = %SWM_MouseY%
WinGetPos, SWM_WinX, SWM_WinY, SWM_WinW, SWM_WinH, ahk_id %SWM_MouseWin%
if SWM_ResizeTypeX
{
	SWM_WinX += %SWM_DeltaX%
	SWM_WinW -= %SWM_DeltaX%
}
else
	SWM_WinW += %SWM_DeltaX%
if SWM_ResizeTypeY
{
	SWM_WinY += %SWM_DeltaY%
	SWM_WinH -= %SWM_DeltaY%
}
else
	SWM_WinH += %SWM_DeltaY%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %SWM_MouseWin%,, %SWM_WinX%, %SWM_WinY%, %SWM_WinW%, %SWM_WinH%
return

; End of Smart Window Management Script

Hope somebody may find it helpful, and looking forward to feedback :-)

cu, aurelian

aurelian
  • Members
  • 3 posts
  • Last active: Jan 26 2005 02:32 PM
  • Joined: 25 Jan 2005
I am registered now! *wide smile*

Adding the following to the code:
~XButton2::
return
seems to fix the problem with the thumb button. :-)

Also, note that the move & resize functionality does not work with WinAmp (at least not with version 5.05).

-aurelian

BoBo
  • Guests
  • Last active:
  • Joined: --
@ aurelian (Johannes)
Einer ist immer der Erste! Könntest du deinem Script noch eine deutschsprachige kurze Beschreibung (der übliche längere Satz) voranstellen/hinzufügen ? Chris ist bereits aufgefallen, das der Anteil der deutschsprachigen AHK-User ständig zunimmt, könnte also helfen.

Danke und Gruß nach DD Hellerau, Prolis, Lockwitz, ... ? :wink:

Nemroth
  • Members
  • 278 posts
  • Last active: Dec 31 2011 10:53 PM
  • Joined: 07 Sep 2004
En allemand dans le texte... :D

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
I've lost count of how many scripts there are to emulate KDE's window moving/resizing. At least three I know of. Pretty handy to have. :wink:

aurelian
  • Members
  • 3 posts
  • Last active: Jan 26 2005 02:32 PM
  • Joined: 25 Jan 2005
Klar, mach ich doch gern. Leider kann ich den Beitrag oben nicht editieren, deswegen stell ichs hier noch mal in voller Pracht zum rauskopieren hin. Habe gleich mal noch etwas reduntanten Code entfernt.

Dresden-Pillnitz :-)
~
Sure, no problem. Unfortunately I can not edit the first post, so I'll paste the whole thing once again. Also I removed some reduntant code.

; Smart Window Management Script
; 
; Erlaubt es, Fenster auf komfortable Weise zu bewegen, zu vergroessern / 
; verkleinern, zu minimieren und zu schliessen. Das Skript basiert auf dem
; Easy Window Dragging Script im Showcase
; (http://www.autohotkey.com/docs/scripts/EasyWindowDrag.htm).
; Definierte Tastenkombinationen siehe unten.
; ~
; Allows window moving, resizing, minimizing and closing in a very comfortable
; manner. This script is based on the Easy Window Dragging Script in the 
; Showcase (Link above).
; 
; Author     : Johannes Loehnert
; Contact    : [email protected]
; Last Change: 2005-01-25
; AHK version: 1.0.25
;
; Folgende Tastenkombinationen werden definiert:
; The script establishes the following combinations:
;
; - LWin  + LB: Hold and drag to move window / Halten und ziehen, um Fenster zu verschieben
; - TB    + LB: same / gleich
; - Lwin  + RB: Hold and drag to resize window (resize corner which is closest to cursor) /
;               Halten und ziehen, um Groesse zu aendern.
; - TB    + RB: same / gleich
; - LCtrl + LWin + LB: Minimize window / Fenster minimieren
; - LCtr  + LWin + RB: Close Window / Fenster schliessen
; - TB    + MB: click to minimize window, hold for 0.5 sec to close window /
;               nur klicken, um Fenster zu minimieren; 0.5 sec halten, um Fenster zu schliessen.
;
; All commands apply to the window under the mouse cursor.
; Abbreviations: LB, MB, RB, TB = left, middle, right, thumb button of mouse.
;
; Alle Kombinationen wirken auf das Fenster, in welchem die Maus sich befindet.
; Abkuerzungen: LB, MB, RB, TB: linke, mittlere, rechte, Daumen-Taste der Maus.


; pass through single clicks of thumb button
~XButton2::
return


;Minimize Window; when held over 0.5 sec: close window
XButton2 & MButton::      
CoordMode, Mouse
MouseGetPos, SWM_MouseStartX, SWM_MouseStartY, SWM_MouseWin
SetTimer, SMW_MidButtonClose, 500   ;<-- TIMER FOR TB+MB CLOSE
SMW_MidButtonCloseTimer=1
loop
{
	GetKeyState, SMW_MButtonState, MButton, P
	if SMW_MidButtonCloseTimer=0
		return
	if SMW_MButtonState=U
	{
		SetTimer, SMW_MidButtonClose, Off
		WinMinimize, ahk_id %SWM_MouseWin%
		SMW_MidButtonCloseTimer=0
		return
	}
}


SMW_MidButtonClose:
SMW_MidButtonCloseTimer=0
SetTimer, SMW_MidButtonClose, Off
WinClose, ahk_id %SWM_MouseWin%
return


; Move window on Lwin+LB or TB+LB; minimize window on LCtrl+Lwin+LB or LCtrl+TB+LB
XButton2 & LButton::
LWin & LBUtton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, SWM_MouseStartX, SWM_MouseStartY, SWM_MouseWin
GetKeyState, SMW_LCtrlState, LCtrl
if SMW_LCtrlState=D
	WinMinimize, ahk_id %SWM_MouseWin%
else
	SetTimer, SWM_WatchMouse_Move, 10 ; Track the mouse as the user drags it.
return


; resize window on Lwin+RB or TB+RB; minimize window on LCtrl+Lwin+RB or LCtrl+TB+RB
XButton2 & RButton:: ;resize or close
LWin & RButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, SWM_MouseStartX, SWM_MouseStartY, SWM_MouseWin
WinGetPos, SWM_WinX, SWM_WinY, SWM_WinW, SWM_WinH, ahk_id %SWM_MouseWin%
GetKeyState, SMW_LCtrlState, LCtrl
if SMW_LCtrlState=D
{
	WinClose, ahk_id %SWM_MouseWin%
	return
}	
SWM_ResizeTypeX=0
SWM_ResizeTypeY=0
if (SWM_MouseStartX < SWM_WinX+SWM_WinW/2)
	SWM_ResizeTypeX=1
if (SWM_MouseStartY < SWM_WinY+SWM_WinH/2)
	SWM_ResizeTypeY=1
SetTimer, SWM_WatchMouse_Resize, 10 ; Track the mouse as the user drags it.
return


SWM_WatchMouse_Move:
GetKeyState, SMW_LButtonState, LButton, P
if SMW_LButtonState = U ; Button has been released, so drag is complete.
{
	SetTimer, SWM_WatchMouse_Move, off
	return
}
Gosub SWM_GetMouseAndWindowPos
SWM_WinX += %SWM_DeltaX%
SWM_WinY += %SWM_DeltaY%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %SWM_MouseWin%,, %SWM_WinX%, %SWM_WinY%
return


SWM_WatchMouse_Resize:
GetKeyState, SMW_RButtonState, RButton, P
if SMW_RButtonState = U ; Button has been released, so drag is complete.
{
	SetTimer, SWM_WatchMouse_Resize, off
	return
}
Gosub SWM_GetMouseAndWindowPos
if SWM_ResizeTypeX
{
	SWM_WinX += %SWM_DeltaX%
	SWM_WinW -= %SWM_DeltaX%
}
else
	SWM_WinW += %SWM_DeltaX%
if SWM_ResizeTypeY
{
	SWM_WinY += %SWM_DeltaY%
	SWM_WinH -= %SWM_DeltaY%
}
else
	SWM_WinH += %SWM_DeltaY%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %SWM_MouseWin%,, %SWM_WinX%, %SWM_WinY%, %SWM_WinW%, %SWM_WinH%
return


SWM_GetMouseAndWindowPos:
CoordMode, Mouse
MouseGetPos, SWM_MouseX, SWM_MouseY
SWM_DeltaX = %SWM_MouseX%
SWM_DeltaX -= %SWM_MouseStartX%
SWM_DeltaY = %SWM_MouseY%
SWM_DeltaY -= %SWM_MouseStartY%
SWM_MouseStartX = %SWM_MouseX%  ; Update for the next timer call to this subroutine.
SWM_MouseStartY = %SWM_MouseY%
WinGetPos, SWM_WinX, SWM_WinY, SWM_WinW, SWM_WinH, ahk_id %SWM_MouseWin%
return

; End of Smart Window Management Script


BoBo
  • Guests
  • Last active:
  • Joined: --
Danke. Thx. Merci. :D

  • Guests
  • Last active:
  • Joined: --
@BoBo
De rien. De nada. It was my pleasure. Sorry, in German I know only one word : kartoffel (and that's (almost) true...)

Nemroth
  • Members
  • 278 posts
  • Last active: Dec 31 2011 10:53 PM
  • Joined: 07 Sep 2004
The precedent post was mine...

BoBo
  • Guests
  • Last active:
  • Joined: --
Kartoffel = pomme de terre. Tres bien, mon amie :wink:
Eh, j'ai ne parle pas francais :roll:

BoBo
  • Guests
  • Last active:
  • Joined: --
Je ne parle pas francais (c'est correct, oui ?)

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
Et "mon amie" = "mon ami"

Watch those genders! :wink:

(Assuming Nemroth's of the masculine orientation)

Nemroth
  • Members
  • 278 posts
  • Last active: Dec 31 2011 10:53 PM
  • Joined: 07 Sep 2004

Je ne parle pas francais (c'est correct, oui ?)

It's perfectly correct

Et "mon amie" = "mon ami"
Watch those genders! :wink:
(Assuming Nemroth's of the masculine orientation)

Oui, Nemroth est un ami et non une amie.
Congratulations, french is not a simple language. :) :) :)

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004

Adding the following to the code:

~XButton2::
return
seems to fix the problem with the thumb button. :-)

I think you could achieve the same effect more concisely by including the tilde (~) here instead:
~XButton2 & MButton::

And thanks for posting another great KDE window helper. I imagine this or one of the other KDE scripts should be put into the showcase. Perhaps they can all be merged somehow into one "ultimate and configurable" KDE script.

aurelian
  • Members
  • 3 posts
  • Last active: Jan 26 2005 02:32 PM
  • Joined: 25 Jan 2005

I think you could achieve the same effect more concisely by including the tilde (~) here instead:
~XButton2 & MButton::


As I unterstand it, this would forward the XBUtton2 event to the window in any case (MButton pressed or not), and that is not how I wanted it to work. If you minimize the window by Xbutton2&MButton, the XButton2 event should not be sent to the window. (Maybe I'll try out your suggestion when I am home today.)