Jump to content

Another version of ChildFollow


spytek_98
 Share

Recommended Posts

My first "script n scrap" so be gentle.

Based off Xenogis previous ChildFollow example I made another version that functions like you see in winamp and the playlist. I.e. playlist "snaps" onto the winamp within a certain range, also allows you to disconnect the parent and child windows by moving the child window away from it's parent.

Easier to run the example than to explain fully.

Enjoy

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)

Global Const $CF_NONE = -1
Global Const $CF_TOP = 0
Global Const $CF_LEFT = 1
Global Const $CF_RIGHT = 2
Global Const $CF_BOTTOM = 3

Global $gParentPos = ""
Global $childPosFlag = $CF_NONE

_main()
_exit()

Func _main()
    $parent = GUICreate("Parent", 200, 200, -1, -1, $WS_SIZEBOX + $WS_SYSMENU)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
    GUISetState()

    GuiCreate("Child",400,80,100,100, -1, -1, $parent)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
    GUISetState()

    While 1
        Sleep(25)
        _followParent()
    WEnd
EndFunc

Func _exit()
    Exit
EndFunc

Func _followParent()
    If WinActive("Parent") Then
        If $childPosFlag > -1 Then
            _moveWin("Parent", "Child", $childPosFlag)
        Else
            $gParentPos = ""
            _dragWin("Parent", "Child")
        EndIf
    EndIf
        
    If WinActive("Child") Then
        _dragWin("Parent", "Child")
    EndIf
EndFunc

Func _dragWin($parent, $child)
    $parentPos = WinGetPos($parent)
    $childPos = WinGetPos($child)

    $childX = $childPos[0]
    $childY = $childPos[1]
    $childRight = $childPos[0] + $childPos[2]

    $parentX = $parentPos[0]
    $parentY = $parentPos[1]
    $parentRight = $parentPos[0] + $parentPos[2]

    $childBottom = $childPos[1] + $childPos[3]
    $parentBottom = $parentPos[1] + $parentPos[3]

; Top / Bottom
    If ($childX > $parentX And $childRight < $parentRight) Or _
        ($parentX > $childX And $parentRight < $childRight) Or _
        ($childX > $parentX And $childX < $parentRight) Or _ 
        ($childRight > $parentX And $childRight < $parentRight) Then 
                    
                    
        If ($parentPos[1] - $childBottom >= -2 And $parentPos[1] - $childBottom <= 15) Then
            $childPosFlag = $CF_TOP
            _moveWin($parent, $child, $CF_TOP)
        ElseIf ($childPos[1] - $parentBottom >= 0 And $childPos[1] - $parentBottom <= 15) Then          
            $childPosFlag = $CF_BOTTOM
            _moveWin($parent, $child, $CF_BOTTOM)
        Else
            $childPosFlag = $CF_NONE
        EndIf

; Left / Right
    ElseIf ($childY > $parentY And $childBottom < $parentBottom) Or _
        ($parentY > $childY And $parentBottom < $childBottom) Or _
        ($childY > $parentY And $childY < $parentBottom) Or _ 
        ($childBottom > $parentY And $childBottom < $parentBottom) Then     
        
        If ($parentPos[0] - $childRight >= 0 And $parentPos[0] - $childRight <= 15) Then
            $childPosFlag = $CF_LEFT
            _moveWin($parent, $child, $CF_LEFT)
        ElseIf ($childPos[0] - $parentRight >= 0 And $childPos[0] - $parentRight <= 15) Then
            $childPosFlag = $CF_RIGHT
            _moveWin($parent, $child, $CF_RIGHT)
        Else
            $childPosFlag = $CF_NONE
        EndIf                           
    EndIf
EndFunc

Func _moveWin($parent, $child, $childSide=0)
    $childPos = WinGetPos($child)
    $parentPos = WinGetPos($parent)
       
    If $childSide = $CF_TOP Then
        $x = $childPos[0]
        
        If $gParentPos <> "" Then           
            $x = $x + ($parentPos[0] - $gParentPos[0])
        EndIf
        
        WinMove($child, "", $x, $parentPos[1] - ($childPos[3]-1))
    ElseIf $childSide = $CF_LEFT Then       
        $y = $childPos[1]
        
        If $gParentPos <> "" Then                       
            $y = $y + ($parentPos[1] - $gParentPos[1])          
        EndIf
                
        WinMove($child, "", $parentPos[0]-$childPos[2], $y)     
    ElseIf $childSide = $CF_RIGHT Then
        $y = $childPos[1]
        
        If $gParentPos <> "" Then                       
            $y = $y + ($parentPos[1] - $gParentPos[1])          
        EndIf
                
        WinMove($child, "", $parentPos[0]+$parentPos[2], $y)
    ElseIf $childSide = $CF_BOTTOM Then
        $x = $childPos[0]
        
        If $gParentPos <> "" Then                       
            $x = $x + ($parentPos[0] - $gParentPos[0])
        EndIf
        
        WinMove($child, "", $x, $parentPos[3] + $parentPos[1])
    EndIf
    
    $gParentPos = WinGetPos($parent)
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...