Sign in to follow this  
Followers 0
Achilles

_WinTrap() - Somebody help me make this?

12 posts in this topic

I'm really hoping for someone who knows a bunch about dll's to do something like _MouseTrap() but if that fails I have this code which is working near to fine except that once it is being stopped on say the left side the user can drag it too far up or down and vice versa for any direction... Here's the code:

#include <GUIConstants.au3>

Guicreate('Testing', 240, 240) 
$label = GuiCtrlCreateLabel('Drag me!', 20, 20, 200, 200, $SS_SUNKEN)

$window = 'Testing'

$moving = False ;If true then the window will drag when the label is clicked...

GuiSetState()

While 1 

    Switch GuiGetMsg() 
        Case $GUI_EVENT_CLOSE
            Exit
    
        Case $label 
            If $moving = True then 
                AdLibDisable()
            Else 
                Opt("MouseCoordMode", 0) 
                $mousePos = MouseGetPos()
                Opt("MouseCoordMode", 1) 
                AdLibEnable('_WinTrap', 20)
            EndIf 
            $moving = Not $moving
    EndSwitch
    
WEnd

Func _WinTrap() 
    $pos = MouseGetPos()
    $winPos = WinGetPos($window)

    If $pos[0] - $mousePos[0] < 0 then 
        WinMove($window, '', 0, $pos[1] - $mousePos[1])
    ElseIf ($winPos[2] - $mousePos[0]) + $pos[0] >= @DesktopWidth then 
        WinMove($window, '', @DesktopWidth - $winPos[2], $pos[1] - $mousePos[1])
    ElseIf $pos[1] - $mousePos[1] < 0 then 
        WinMove($window, '', $pos[0] - $mousePos[0], 0) 
    ElseIf ($winPos[3] - $mousePos[1]) + $pos[1] >= @DesktopHeight then 
        WinMove($window, '', $pos[0] - $mousePos[0], @DesktopHeight - $winPos[3])
    Else    
        WinMove($window, '', $pos[0] - $mousepos[0], $pos[1] - $mousePos[1])
    EndIf 
EndFunc

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites



This should fix it. The problem was that once your bound-checking picked up that you were out of bounds on the left or right, it stopped checking for out of bound errors on the top and bottom. I simply added some bound-checking inside the cases where there were out of bound errors.

#include <GUIConstants.au3>

Guicreate('Testing', 240, 240) 
$label = GuiCtrlCreateLabel('Drag me!', 20, 20, 200, 200, $SS_SUNKEN)

$window = 'Testing'

$moving = False ;If true then the window will drag when the label is clicked...

GuiSetState()

While 1 

    Switch GuiGetMsg() 
        Case $GUI_EVENT_CLOSE
            Exit
    
        Case $label 
            If $moving = True then 
                AdLibDisable()
            Else 
                Opt("MouseCoordMode", 0) 
                $mousePos = MouseGetPos()
                Opt("MouseCoordMode", 1) 
                AdLibEnable('_WinTrap', 20)
            EndIf 
            $moving = Not $moving
    EndSwitch
    
WEnd

Func _WinTrap() 
    $pos = MouseGetPos()
    $winPos = WinGetPos($window)

    If $pos[0] - $mousePos[0] < 0  then 
        If $pos[1] - $mousePos[1] < 0 Then
           WinMove($window, '', 0, 0)
        ElseIf $winPos[3] - $mousePos[1] + $pos[1] > @DesktopHeight Then
           WinMove($window, '', 0, @DesktopHeight - $winPos[3])
        Else
           WinMove($window, '', 0, $pos[1] - $mousePos[1])
        EndIf
    ElseIf ($winPos[2] - $mousePos[0]) + $pos[0] > @DesktopWidth then
        If $pos[1] - $mousePos[1] < 0 Then
           WinMove($window, '', @DesktopWidth - $winPos[2], 0)
        ElseIf $winPos[3] - $mousePos[1] + $pos[1] > @DesktopHeight Then
           WinMove($window, '', @DesktopWidth - $winPos[2], @DesktopHeight - $winPos[3])
        Else 
           WinMove($window, '', @DesktopWidth - $winPos[2], $pos[1] - $mousePos[1])
        EndIf
    ElseIf $pos[1] - $mousePos[1] < 0 then 
        If $pos[0] - $mousePos[0] < 0 Then
           WinMove($window, '', 0, 0)
        ElseIf $winPos[2] - $mousePos[0] + $pos[0] > @DesktopWidth Then
           WinMove($window, '', @DesktopWidth - $winPos[2], 0)
        Else
           WinMove($window, '', $pos[0] - $mousePos[0], 0)
        EndIf
    ElseIf ($winPos[3] - $mousePos[1]) + $pos[1] > @DesktopHeight then 
        If $pos[0] - $mousePos[0] < 0 Then
           WinMove($window, '', 0, @DesktopHeight - $winPos[3])
        ElseIf $winPos[2] - $mousePos[0] + $pos[0] > @DesktopWidth Then
           WinMove($window, '', @DesktopWidth - $winPos[2], @DesktopHeight - $winPos[3])
        Else
           WinMove($window, '', $pos[0] - $mousePos[0], @DesktopHeight - $winPos[3])
        EndIf
    Else    
        WinMove($window, '', $pos[0] - $mousepos[0], $pos[1] - $mousePos[1])
    EndIf 
EndFunc

Share this post


Link to post
Share on other sites

I cleaned up my previous version of the program. Now I've added bound-checking for the four cases where both coordinates are out of bounds.

#include <GUIConstants.au3>

Guicreate('Testing', 240, 240) 
$label = GuiCtrlCreateLabel('Drag me!', 20, 20, 200, 200, $SS_SUNKEN)

$window = 'Testing'

$moving = False ;If true then the window will drag when the label is clicked...

GuiSetState()

While 1 

    Switch GuiGetMsg() 
        Case $GUI_EVENT_CLOSE
            Exit
    
        Case $label 
            If $moving = True then 
                AdLibDisable()
            Else 
                Opt("MouseCoordMode", 0) 
                $mousePos = MouseGetPos()
                Opt("MouseCoordMode", 1) 
                AdLibEnable('_WinTrap', 20)
            EndIf 
            $moving = Not $moving
    EndSwitch
    
WEnd

Func _WinTrap() 
    $pos = MouseGetPos()
    $winPos = WinGetPos($window)
    If $pos[0] - $mousePos[0] < 0  AND $pos[1] - $mousePos[1] < 0 then 
        ;Move to top-left corner
        WinMove($window, '', 0, 0)
    ElseIf $pos[0] - $mousePos[0] < 0 AND $winPos[3] - $mousePos[1] + $pos[1] > @DesktopHeight then
        ;Move to bottom-left corner
        WinMove($window, '', 0, @DesktopHeight - $winPos[3])
    ElseIf $winPos[2] - $mousePos[0] + $pos[0] > @DesktopWidth AND $pos[1] - $mousePos[1] < 0 then
        ;Move to top-right corner
        WinMove($window, '', @DesktopWidth - $winPos[2], 0)
    ElseIf $winPos[2] - $mousePos[0] + $pos[0] > @DesktopWidth AND $winPos[3] - $mousePos[1] + $pos[1] > @DesktopHeight then
        ;Move to bottom-right corner
        WinMove($window, '', @DesktopWidth - $winPos[2], @DesktopHeight - $winPos[3])
    ElseIf $pos[0] - $mousePos[0] < 0 then 
        WinMove($window, '', 0, $pos[1] - $mousePos[1])
    ElseIf ($winPos[2] - $mousePos[0]) + $pos[0] > @DesktopWidth then 
        WinMove($window, '', @DesktopWidth - $winPos[2], $pos[1] - $mousePos[1])
    ElseIf $pos[1] - $mousePos[1] < 0 then 
        WinMove($window, '', $pos[0] - $mousePos[0], 0)
    ElseIf ($winPos[3] - $mousePos[1]) + $pos[1] > @DesktopHeight then 
        WinMove($window, '', $pos[0] - $mousePos[0], @DesktopHeight - $winPos[3])
    Else    
        WinMove($window, '', $pos[0] - $mousepos[0], $pos[1] - $mousePos[1])
    EndIf 
EndFunc

Share this post


Link to post
Share on other sites

I cleaned up my previous version of the program. Now I've added bound-checking for the four cases where both coordinates are out of bounds.

#include <GUIConstants.au3>

Guicreate('Testing', 240, 240) 
$label = GuiCtrlCreateLabel('Drag me!', 20, 20, 200, 200, $SS_SUNKEN)

$window = 'Testing'

$moving = False ;If true then the window will drag when the label is clicked...

GuiSetState()

While 1 

    Switch GuiGetMsg() 
        Case $GUI_EVENT_CLOSE
            Exit
    
        Case $label 
            If $moving = True then 
                AdLibDisable()
            Else 
                Opt("MouseCoordMode", 0) 
                $mousePos = MouseGetPos()
                Opt("MouseCoordMode", 1) 
                AdLibEnable('_WinTrap', 20)
            EndIf 
            $moving = Not $moving
    EndSwitch
    
WEnd

Func _WinTrap() 
    $pos = MouseGetPos()
    $winPos = WinGetPos($window)
    If $pos[0] - $mousePos[0] < 0  AND $pos[1] - $mousePos[1] < 0 then 
        ;Move to top-left corner
        WinMove($window, '', 0, 0)
    ElseIf $pos[0] - $mousePos[0] < 0 AND $winPos[3] - $mousePos[1] + $pos[1] > @DesktopHeight then
        ;Move to bottom-left corner
        WinMove($window, '', 0, @DesktopHeight - $winPos[3])
    ElseIf $winPos[2] - $mousePos[0] + $pos[0] > @DesktopWidth AND $pos[1] - $mousePos[1] < 0 then
        ;Move to top-right corner
        WinMove($window, '', @DesktopWidth - $winPos[2], 0)
    ElseIf $winPos[2] - $mousePos[0] + $pos[0] > @DesktopWidth AND $winPos[3] - $mousePos[1] + $pos[1] > @DesktopHeight then
        ;Move to bottom-right corner
        WinMove($window, '', @DesktopWidth - $winPos[2], @DesktopHeight - $winPos[3])
    ElseIf $pos[0] - $mousePos[0] < 0 then 
        WinMove($window, '', 0, $pos[1] - $mousePos[1])
    ElseIf ($winPos[2] - $mousePos[0]) + $pos[0] > @DesktopWidth then 
        WinMove($window, '', @DesktopWidth - $winPos[2], $pos[1] - $mousePos[1])
    ElseIf $pos[1] - $mousePos[1] < 0 then 
        WinMove($window, '', $pos[0] - $mousePos[0], 0)
    ElseIf ($winPos[3] - $mousePos[1]) + $pos[1] > @DesktopHeight then 
        WinMove($window, '', $pos[0] - $mousePos[0], @DesktopHeight - $winPos[3])
    Else    
        WinMove($window, '', $pos[0] - $mousepos[0], $pos[1] - $mousePos[1])
    EndIf 
EndFunc
Ahh, I see what I was missing... Thanks a lot for that!

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

I'm glad I could help. Overnight, I thought of a much cleaner version that deals with the x and y coordinates seperately. Here's my final code.

#include <GUIConstants.au3>

Guicreate('Testing', 240, 240) 
$label = GuiCtrlCreateLabel('Drag me!', 20, 20, 200, 200, $SS_SUNKEN)

$window = 'Testing'

$moving = False ;If true then the window will drag when the label is clicked...

GuiSetState()

While 1 

    Switch GuiGetMsg() 
        Case $GUI_EVENT_CLOSE
            Exit
    
        Case $label 
            If $moving = True then 
                AdLibDisable()
            Else 
                Opt("MouseCoordMode", 0) 
                $mousePos = MouseGetPos()
                Opt("MouseCoordMode", 1) 
                AdLibEnable('_WinTrap', 20)
            EndIf 
            $moving = Not $moving
    EndSwitch
    
WEnd

Func _WinTrap() 
    $pos = MouseGetPos()
    $winPos = WinGetPos($window)

    If $pos[0] - $mousePos[0] < 0 Then
        $x = 0
    ElseIf $winPos[2] - $mousePos[0] + $pos[0] > @DesktopWidth Then
        $x = @DesktopWidth - $winPos[2]
    Else
        $x = $pos[0] - $mousepos[0]
    EndIf

    If $pos[1] - $mousePos[1] < 0 Then
        $y = 0
    ElseIf $winPos[2] - $mousePos[1] + $pos[1] > @DesktopHeight Then
        $y = @DesktopHeight - $winPos[3]
    Else
        $y = $pos[1] - $mousepos[1]
    EndIf

    WinMove($window, '', $x, $y)

EndFunc

Share this post


Link to post
Share on other sites

Probably would have found what you were looking for here, and instead of making it always perform WinMove() even if the mouse is not moving... you can see how to do it easily with only moving if the last position recorded has changed.

http://www.autoitscript.com/forum/index.ph...st&p=168660


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Probably would have found what you were looking for here, and instead of making it always perform WinMove() even if the mouse is not moving... you can see how to do it easily with only moving if the last position recorded has changed.

http://www.autoitscript.com/forum/index.ph...st&p=168660

That is a good idea... would save CPU work.

What is the link suppose to help for? The magnifier doesn't really relate to my script in any way I see, unless your talking about checking whether the mouse is in a different position.


My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

That is a good idea... would save CPU work.

What is the link suppose to help for? The magnifier doesn't really relate to my script in any way I see, unless your talking about checking whether the mouse is in a different position.

In this case I posted the link to the code, but for what the code did specifically. If you can see past the minute stuff, you'll see your answer quickly (as you already have I will assume by your post).

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I'm glad I could help. Overnight, I thought of a much cleaner version that deals with the x and y coordinates seperately. Here's my final code.

;Was:
    ElseIf $winPos[2] - $mousePos[1] + $pos[1] > @DesktopHeight Then
;Should be:
    ElseIf $winPos[3] - $mousePos[1] + $pos[1] > @DesktopHeight Then
Thanks again Tobias7, this works good except one small part... for the getting the Y coords you have a $winPos[2] where there should be a $winPos[3]. Edited by Piano_Man

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

Thanks again Tobias7, this works good except one small part... for the getting the Y coords you have a $winPos[2] where there should be a $winPos[3].

Whoops! Sorry about that.

Share this post


Link to post
Share on other sites

I made slight changes also.

Do not know if it was your intent or not, but...

1) Stop drag upon release, and

2) Only update when the mouse moves; no 'AdLibEnable'

#include <GUIConstants.au3>

Guicreate('Testing', 240, 240) 
$label = GuiCtrlCreateLabel('Drag me!', 20, 20, 200, 200, $SS_SUNKEN)

$window = 'Testing'

$moving = $label

GuiSetState()

While 1 

    Switch GuiGetMsg() 
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $GUI_EVENT_PRIMARYUP
            If $moving Then $moving = $label
            
        Case $label
            If $moving <> $GUI_EVENT_MOUSEMOVE Then
                $moving =  $GUI_EVENT_MOUSEMOVE 
                Opt("MouseCoordMode", 0) 
                $mousePos = MouseGetPos()
                Opt("MouseCoordMode", 1)
            EndIf
            
        Case $moving
            _WinTrap()

   EndSwitch
    
WEnd

Func _WinTrap() 
    $pos = MouseGetPos()
    $winPos = WinGetPos($window)

    If $pos[0] - $mousePos[0] < 0 Then
        $x = 0
    ElseIf $winPos[2] - $mousePos[0] + $pos[0] > @DesktopWidth Then
        $x = @DesktopWidth - $winPos[2]
    Else
        $x = $pos[0] - $mousepos[0]
    EndIf

    If $pos[1] - $mousePos[1] < 0 Then
        $y = 0
    ElseIf $winPos[3] - $mousePos[1] + $pos[1] > @DesktopHeight Then
        $y = @DesktopHeight - $winPos[3]
    Else
        $y = $pos[1] - $mousepos[1]
    EndIf

    WinMove($window, '', $x, $y)

EndFunc

gsb


"Did you ever stop to think? ...and forget to restart!"

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I made slight changes also.

Do not know if it was your intent or not, but...

1) Stop drag upon release, and

2) Only update when the mouse moves; no 'AdLibEnable'

#include <GUIConstants.au3>

Guicreate('Testing', 240, 240) 
$label = GuiCtrlCreateLabel('Drag me!', 20, 20, 200, 200, $SS_SUNKEN)

$window = 'Testing'

$moving = $label

GuiSetState()

While 1 

    Switch GuiGetMsg() 
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $GUI_EVENT_PRIMARYUP
            If $moving Then $moving = $label
            
        Case $label
            If $moving <> $GUI_EVENT_MOUSEMOVE Then
                $moving =  $GUI_EVENT_MOUSEMOVE 
                Opt("MouseCoordMode", 0) 
                $mousePos = MouseGetPos()
                Opt("MouseCoordMode", 1)
            EndIf
            
        Case $moving
            _WinTrap()

   EndSwitch
    
WEnd

Func _WinTrap() 
    $pos = MouseGetPos()
    $winPos = WinGetPos($window)

    If $pos[0] - $mousePos[0] < 0 Then
        $x = 0
    ElseIf $winPos[2] - $mousePos[0] + $pos[0] > @DesktopWidth Then
        $x = @DesktopWidth - $winPos[2]
    Else
        $x = $pos[0] - $mousepos[0]
    EndIf

    If $pos[1] - $mousePos[1] < 0 Then
        $y = 0
    ElseIf $winPos[3] - $mousePos[1] + $pos[1] > @DesktopHeight Then
        $y = @DesktopHeight - $winPos[3]
    Else
        $y = $pos[1] - $mousepos[1]
    EndIf

    WinMove($window, '', $x, $y)

EndFunc

gsb

That's great.. I'll implement this in the code I'm using the _WinSnap thing in.. Thanks! :) Edited by Piano_Man

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
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
Sign in to follow this  
Followers 0