Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Send window to bottom


  • Please log in to reply
15 replies to this topic
schandl
  • Members
  • 28 posts
  • Last active: Mar 28 2015 04:03 PM
  • Joined: 26 Apr 2005
I wanted to write a script which sends the active window to the background when I click with the right mouse button on the title bar (similar to some Linux window managers). It turned out to be much more complicated than I thought. Especially I am not sure about detecting the Windows task bar (see TB_Class). If you find any problems with or possible improvements for the following script, let me know:

; ---------------------------------------------------------------------
; Name:        TitleBack v0.1
; Author:      Bernd Schandl
; Date:        04.05.2005
;
; Explanation:
; ---------------------------------------------------------------------
; When the title bar of the active window is clicked with the right 
; mouse button, the window is sent to the background.

RButton::
  ; get height of title bar (4 is SM_CYCAPTION)
  SysGet, TB_TitleBarHeight, 4
  ; get mouse position relative to the active window and get the 
  ; window ID under the cursor (not necessarily active!) 
  MouseGetPos, TB_X, TB_Y, TB_ID
  ; get the class of the window under the cursor
  WinGetClass, TB_Class, ahk_id %TB_ID%
  ; stop if window not active
  IfWinNotActive, ahk_id %TB_ID% 
    Send, {RButton}
  ; also stop if not in title bar
  else if ( TB_Y > TB_TitleBarHeight )
    Send, {RButton}
  ; also stop if in task bar
  else if ( TB_Class = "Shell_TrayWnd" )
    Send, {RButton}
  ; ok, we are in the title bar of the active window which is not
  ; the task bar
  Else
    WinSet, Bottom,, ahk_id %TB_ID%
return

Bernd

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Some of the best macros are short ones. I think this falls into that category. Thanks for sharing it.

TeknoMusicMan
  • Members
  • 186 posts
  • Last active: Jan 17 2007 10:17 PM
  • Joined: 14 Apr 2005
Work's great, but I found 2 little issues.

If i want to just keep right clicking to cycle through the windows I can't. The reason is that when the window i just sent back goes to the back the next window in line isn't activated so if i just right click again, that window's "rightclick titlebar menu" pops up.

If you wrote the script to activate the next window in line that would make it perfect.

2nd My "rocker" mouse gestures don't work because the script doesn't account for if the mouse is being held down.

Maybe if you made the script activate on a mouse up event that would solve that problem?
Posted Image
"Make it idiot-proof, and someone will make a better idiot."

skrommel
  • Members
  • 193 posts
  • Last active: Jun 07 2010 08:30 AM
  • Joined: 30 Jul 2004
Just a slight improvement, to make inactive windows go to the background, too.

Skrommel


;SendRightBack
;RightClick in the titlebar of a window to send it to the background

CoordMode,Mouse,Screen
SysGet,titlebarheight,4

RButton::
click=1
MouseGetPos,mx,my,window
WinGetClass,class,ahk_id %window%
WinGetPos,wx,wy,,,ahk_id %window%
my-=%wy%

If my>=0
  If my<=%titlebarheight%
    click=0
If class=WorkerW
  click=1
If class=Progman
  click=1
If class=Shell_TrayWnd
  click=1

If click=1
  Send,{RButton}
Else
  WinSet,Bottom,,ahk_id %window%
Return


skrommel
  • Members
  • 193 posts
  • Last active: Jun 07 2010 08:30 AM
  • Joined: 30 Jul 2004
And if you put it into Laszlo's excellent script from http://www.autohotke...opic.php?t=3424, it should solve the right-drag, too.

Skrommel


;SendRightBack
;RightClick in the titlebar of a window to send it to the background

CoordMode,Mouse,Screen
SysGet,titlebarheight,4

$RButton:: 
  MouseGetPos,x0,y0             ; starting mouse position 
  Loop 
  { 
    Sleep,20                 ; give time to others 
    GetKeystate,state,RButton,P 
    MouseGetPos,x,y            ; new position 
    moved := x-x0>5 || x0-x>5 || y-y0>5 || y0-y>5 
    If ( state = "D" && Not moved)  ; keep waiting for change 
      continue 
    If ( state = "D" && moved )    ; right drag 
    { 
      HotKey,$Rbutton,off 
      MouseClick,Right,x0,y0,1,0,D 
      Break 
    } 
    If ( state = "U" && Not moved )  ; right click 
    { 
      click=1
      MouseGetPos,mx,my,window
      WinGetClass,class,ahk_id %window%
      WinGetPos,wx,wy,,,ahk_id %window%
      my-=%wy%
      
      If my>=0
        If my<=%titlebarheight%
         click=0
      If class=WorkerW
        click=1
      If class=Progman
        click=1
      If class=Shell_TrayWnd
        click=1
      
      If click=1
        Send,{RButton}
      Else
        WinSet,Bottom,,ahk_id %window%
      Break 
    } 
    If ( state = "U" && moved )    ; click & move 
    { 
      MouseClick,Right,x,y,1,0 
      Break 
    } 
  } 
Return 

~$RButton Up::                 ; re-enable hotkey 
  HotKey $RButton,on 
Return


schandl
  • Members
  • 28 posts
  • Last active: Mar 28 2015 04:03 PM
  • Joined: 26 Apr 2005
With the help of your ideas, I made a new version with the following changes:
- Inactive windows are send to the background as well, see the first script by skrommel. I left out the check for the class WorkerW; what is this? This should also fix TeknoMusicMan's problem cycling through the windows. I did not know how to activate the topmost window anyway (unless it is for sure under the cursor which it is not always).
- Dragging with the right mouse button should work. This should also fix some of TeknoMusicMan's gesture problems though for sure not all.

This is similar to skrommel's second script above but does not use $RButton and it fixes an issue (which I did not completly understand) I had with releasing the button. Frankly, by now the script has so many cases to take into account that I will probably stick with my patched version of Winroll.

Any further suggestions for improvments and simplifications are welcome anyway.

Bernd

; ---------------------------------------------------------------------
; Name:        TitleBack v0.3
; Author:      Bernd Schandl
; Date:        05.05.2005
;
; Explanation:
; ---------------------------------------------------------------------
; When the title bar of a window is clicked with the right 
; mouse button, the window is sent to the background.

; get height of title bar (4 is SM_CYCAPTION)
SysGet, TB_TitleBarHeight, 4

RButton::
  ; get mouse coordinates relative to the screen for this hotkey
  CoordMode, Mouse, Screen
  ; get mouse position relative to the screen 
  MouseGetPos, TB_X1, TB_Y1
  Loop
  {
    ; give time to others
    Sleep, 20
    ; get state of right mouse button
    GetKeyState, TB_State, RButton, P
    ; get new position of the cursor and the window underneath
    MouseGetPos, TB_X2, TB_Y2, TB_ID
    ; check whether mouse has moved
    TB_HasMoved := TB_X1 <> TB_X2 or TB_Y1 <> TB_Y2
    if ( TB_State = "D" && not TB_HasMoved )   
      ; keep waiting for change
      continue
    if ( TB_State = "D" && TB_HasMoved )
    {
      ; right drag
      TB_Dragging := true
      HotKey, Rbutton, off
      MouseClick, Right, TB_X1, TB_Y1,, 0, D
      Break
    }
    if ( TB_State = "U" && not TB_HasMoved )
    {
      ; right click
      ; get the class of the window under the cursor
      WinGetClass, TB_Class, ahk_id %TB_ID%
      ; stop if in task bar or on desktop
      if ( TB_Class = "Shell_TrayWnd" or TB_Class = "Progman" )
      {
        MouseClick, Right, TB_X1, TB_Y1,, 0
        Break
      }
      ; get the position of the window under the cursor
      WinGetPos,, TB_WinY,,, ahk_id %TB_ID%
      ; calculate TB_Y1 relative to window
      TB_Y1 -= %TB_WinY%
      ; to background if in title bar
      if TB_Y1 <= %TB_TitleBarHeight% 
        WinSet, Bottom,, ahk_id %TB_ID%
      else
        MouseClick, Right, TB_X1, TB_Y1,, 0
      Break
    }
    if ( TB_State = "U" && TB_HasMoved )
    {
      ; click and move
      MouseClick, Right, TB_X1, TB_Y1,, 0
      Break
    }
  }
Return 

RButton Up::
  if TB_Dragging
  {
    ; turn off dragging
    TB_Dragging := false
    ; re-enable hotkey
    HotKey, RButton, on 
    ; release the key
    MouseClick, Right,,,, 0, U
  }
return


skrommel
  • Members
  • 193 posts
  • Last active: Jun 07 2010 08:30 AM
  • Joined: 30 Jul 2004
I don't know what Workerw is, either, but it appeared while I was making the scripts above, and rightclicking on the desktop won't always work without it.

Skrommel

MisterW
  • Members
  • 65 posts
  • Last active: Jun 18 2007 11:14 AM
  • Joined: 20 Jul 2005
I like this script and it has given me quite a bit of understanding on how to deal with button clicks in a compatible way.

I don't know if anyone is using it on a regular basis but it has a bug which sends right clicks thru to the wrong place.

When testing if the click was in the title bar it modifies the original position of the click and then sends that to the wrong place

this code:

      TB_Y1 -= %TB_WinY%
      ; to background if in title bar
      if TB_Y1 <= %TB_TitleBarHeight%
        WinSet, Bottom,, ahk_id %TB_ID%
      else
        MouseClick, Right, TB_X1, TB_Y1,, 0

Should probably become something like:

      ; to background if in title bar
      if (TB_Y1 - TB_WinY <= TB_TitleBarHeight)
        WinSet, Bottom,, ahk_id %TB_ID%
      else
        MouseClick, Right, TB_X1, TB_Y1,, 0

Works much better, now.

bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011
I just incorporated ( :lol: ) the above snippet of code and it really works wonderfully!

; http://www.autohotkey.com/forum/topic3452.html
; TitleBack

; ---------------------------------------------------------------------
; Name:        TitleBack v0.3
; Author:      Bernd Schandl
; Date:        05.05.2005
;
; Explanation:
; ---------------------------------------------------------------------
; When the title bar of a window is clicked with the right
; mouse button, the window is sent to the background.

; get height of title bar (4 is SM_CYCAPTION)
SysGet, TB_TitleBarHeight, 4

RButton::
  ; get mouse coordinates relative to the screen for this hotkey
  CoordMode, Mouse, Screen
  ; get mouse position relative to the screen
  MouseGetPos, TB_X1, TB_Y1
  Loop
  {
    ; give time to others
    Sleep, 20
    ; get state of right mouse button
    GetKeyState, TB_State, RButton, P
    ; get new position of the cursor and the window underneath
    MouseGetPos, TB_X2, TB_Y2, TB_ID
    ; check whether mouse has moved
    TB_HasMoved := TB_X1 <> TB_X2 or TB_Y1 <> TB_Y2
    if ( TB_State = "D" && not TB_HasMoved )   
      ; keep waiting for change
      continue
    if ( TB_State = "D" && TB_HasMoved )
    {
      ; right drag
      TB_Dragging := true
      HotKey, Rbutton, off
      MouseClick, right, TB_X1, TB_Y1,, 0, D
      Break
    }
    if ( TB_State = "U" && not TB_HasMoved )
    {
      ; right click
      ; get the class of the window under the cursor
      WinGetClass, TB_Class, ahk_id %TB_ID%
      ; stop if in task bar or on desktop
      if ( TB_Class = "Shell_TrayWnd" or TB_Class = "Progman" )
      {
        MouseClick, right, TB_X1, TB_Y1,, 0
        Break
      }
      ; get the position of the window under the cursor
      WinGetPos,, TB_WinY,,, ahk_id %TB_ID%
      ; calculate TB_Y1 relative to window
      ; to background if in title bar
      if (TB_Y1 - TB_WinY <= TB_TitleBarHeight)
        WinSet, Bottom,, ahk_id %TB_ID%
      else
        MouseClick, right, TB_X1, TB_Y1,, 0
      Break
    }
    if ( TB_State = "U" && TB_HasMoved )
    {
      ; click and move
      MouseClick, right, TB_X1, TB_Y1,, 0
      Break
    }
  }
Return

RButton Up::
  if TB_Dragging
  {
    ; turn off dragging
    TB_Dragging := false
    ; re-enable hotkey
    HotKey, RButton, on
    ; release the key
    MouseClick, right,,,, 0, U
  }
return


zhotkey
  • Members
  • 34 posts
  • Last active: Feb 04 2016 02:06 PM
  • Joined: 02 Dec 2010
bruno's code doesn't work correctly when right dragging an icon with Windows 7, it just moves it instead of bringing up a dialog box. You also can't right click on taskbar icons. What need to be adjusted to work with Windows 7?

SupremeLeader
  • Guests
  • Last active:
  • Joined: --
Just a guess, maybe bruno's right and left buttons on his mouse are reversed? (Button Configuration: Switch primary and secondary buttons)

SupremeLeader
  • Guests
  • Last active:
  • Joined: --
I just tested bruno's code. It works perfectly well once you switch primary and secondary buttons on your mouse as explained above! bruno's code can be modified for default button configuration, however.

SupremeLeader
  • Guests
  • Last active:
  • Joined: --
I found a problem when right/left-clicking on taskbar icons (depending on your mouse button configuration)!

zhotkey
  • Members
  • 34 posts
  • Last active: Feb 04 2016 02:06 PM
  • Joined: 02 Dec 2010

bruno's code doesn't work correctly when right dragging an icon with Windows 7, it just moves it instead of bringing up a dialog box. You also can't right click on taskbar icons. What need to be adjusted to work with Windows 7?


I am using a left handed mouse, if I switch the primary and secondary buttons in Windows XP Mouse it works as advertised. However, the code should be able to handle a left handed mouse. I tried switching the definitions in Bruno's script but that doesn't work.

  • Guests
  • Last active:
  • Joined: --
Did you change all 5 occurences of Rbutton to Lbutton? (just checking)