Jump to content

A fun little example using MouseTrap

Recommended Posts


Hey everyone,

Since I haven't yet created anything that isn't specific to my job, and haven't ever posted any examples, I figured I would share this little script I made a while ago for fun between projects.

#include <GUIConstants.au3>
#include <Misc.au3>
HotKeySet("{F9}", "Start")
HotKeySet("{ESC}", "Stop")

Func Start()
    Local $hGUI = GUICreate("Transparent GUI", @DesktopWidth, @DesktopHeight, -1, -1, $WS_POPUP, $WS_EX_TOPMOST)
    Local $i,$j
    WinSetTrans($hGUI, "", 50)
    GUISetState(@SW_SHOW, $hGUI)

    For $i=0 To 1040 Step 1
        _Middle($hGUI, "", @DesktopWidth-$i, @DesktopHeight-$i)
        $winCoord = WinGetPos($hGUI)
        _MouseTrap($winCoord[0],$winCoord[1],($winCoord[0] + $winCoord[2])-5,($winCoord[1] + $winCoord[3])-5)

    For $j=0 to 840 Step 1
        _Middle($hGUI, "", @DesktopWidth-$i-$j, @DesktopHeight-$i)
        $winCoord = WinGetPos($hGUI)
        _MouseTrap($winCoord[0],$winCoord[1],($winCoord[0] + $winCoord[2])-5,($winCoord[1] + $winCoord[3])-5)

    ToolTip("Your mouse has been trapped!", @DesktopWidth/2-80,@DesktopHeight/2+22)

    While 1

EndFunc   ;==>

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE

Func _Middle($win, $txt, $width=-1, $height=-1)
    If $width >= 0 Or $height >= 0 Then
        Local $aWin = WinGetPos($win)
        If $width < 0 Then $width = $aWin[2]
        If $height < 0 Then $height = $aWin[3]
        WinMove($win, $txt, $aWin[0], $aWin[1], $width, $height) ; first winmove
    Local $size = WinGetClientSize($win, $txt)
    Local $y = (@DesktopHeight / 2) - ($size[1] / 2)
    Local $x = (@DesktopWidth / 2) - ($size[0] / 2)
    Return WinMove($win, $txt, $x, $y) ; second winmove
EndFunc   ;==>_Middle

Func Stop()
EndFunc   ;==>Stop

It creates a transparent GUI that slowly shrinks, trapping your mouse inside! I created it after discovering the _MouseTrap() function in the helpfile and the idea floated into my head. It has no real purpose other than entertainment. I figured between all of the serious scripts we use here a little fun is always a nice break :D

F9 to start, ESC to exit at any time.

If this is against the rules feel free to remove it, I didn't see any place this would fall under.


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

  • Similar Content

    • UEZ
      By UEZ
      A simple firework for New Year's Eve with sound fx build 2016-01-08.
    • Ascend4nt
      By Ascend4nt

      GUI Fun!

      Not enough posts here are about just plain fun stuff to do with AutoIt, at least not lately.  So I figure, why not dig up one of my old UDFs, clean it up a bit, and throw it very emphatically at you lot of misfits!

      One way of mucking about with AutoIt and Windows that can be entertaining is creating shaped GUIs. Sure, GUIs with regions are nothing new here or in general on the Windows platform. But have you ever just wanted to stare at colorful shapes flying across your screen, for no apparent reason?  Well, my friend, you've come to the right place.

      The first UDF I've put up pretty simple.  Boxes, circles, triangles, diamonds, and stars are what I bring you, in technicolor wonder.  I will probably add a few more examples when I find the time..

      However, its my hope that more people add to this thread - and maybe provide links to other topics or posts where some nifty GUI fun is to be had!
      GUI related Threads & UDFs of Interest
      >GUI design concepts (started by Guinness) >Perforator - Perforated GUIs (and really anything GUI or GDI+-related by UEZ) >PNG as GUI >Multiple PNG images as GUI elements >FreeText - Text shape GUIs >Visible Controls on a Transparent Window >Layered Window without Image >AnyGUI - Extend the GUI of any Window >_UskinLibrary - Skinning w/DLL >XSkin - More GUI Skinning >GUIExtender, >GUIFrame, >Toast and other nice UDFs by Melba23 >GIF Animation >GDI+ Animated Waiting / Loading Screens My Examples
      I  should point out other GUI manipulation UDF's I've uploaded in the past, as they too can provide some amusement.
      >GUIBox - Rubber-band GUIs >Full-screen Crosshairs >BarSweep (very simple example) >BoxSelection (another simple example)  
      Okay, on to the new stuff!

      _GUIShapes is a UDF with functions to create GUI's shapes like Circles, Boxes, Triangles, Stars, and Diamonds.  These GUI's have no interactive elements or controls and are click-through-able, meaning that a click on the GUI will pass through to whatever window is underneath.
      _LineTraverser is a UDF with functions to create a well, line traverser. Given a start and end point, it will allow you to step through a given line without needing to do any extra work on your behalf.  This uses Bresenham's line algorithm to calculate the individual steps. I know this one isn't related to GUI, but its what I used for my second 'GUI fun' example below.
      Pacman Line-Traversing GUI (>static or >animated). That's right, I managed to animate a GUI! Check it!  (now included in the ZIP)
      Examples that follow will be bundled with the ZIP.  Have fun!

      GUIShapesExample - whacky shapes flying all over the screen!!!
      #include "_GUIShapes.au3" ;~ #include <WinAPI.au3>    ; already included in <_GUIShapes.au3> ; ======================================================================================================== ; <GUIShapesExample.au3> ; ; Example use of <_GUIShapes.au3> UDF ; ; This example creates a bunch of random GUI shapes with random attributes, and moves ; everything around - randomly. ; ; Author: Ascend4nt ; ======================================================================================================== ; ---------------------- MAIN CODE ------------------------------- Local $iShapeGUIs = 20, $aShapeGUIs[$iShapeGUIs], $iTimer, $iRand, $aRet Local $iRandX, $iRandY, $iRandColor, $iRandLength Local $iTriangles = 0, $iCircles = 0, $iStars = 0, $iDiamonds = 0, $iBoxes = 0 For $i = 0 To $iShapeGUIs - 1     ; Everything random!     $iRandX = Random(0, @DesktopWidth - 20, 1)     $iRandY = Random(0, @DesktopHeight - 20, 1)     $iRandLength = Random(12, 300, 1)     $iRandColor = Random(0x111111, 0xFFFFFF, 1)     ; Choose a GUI shape at random, with semi-random attributes     Switch Random(0, 5, 1)         Case 0             $aShapeGUIs[$i] = _TriangleGUICreate($iRandX, $iRandY, $iRandLength, BitAND($iTriangles, 1) * Random(1, 10, 1), Default, Mod($i, 4), $iRandColor)             $iTriangles += 1         Case 1             $aShapeGUIs[$i] = _CircleGUICreate($iRandX, $iRandY, $iRandLength, BitAND($iCircles, 1) * Random(1, 10, 1), Default, $iRandColor)             $iCircles += 1         Case 2             $aShapeGUIs[$i] = _StarGUICreate($iRandX, $iRandY, $iRandLength, BitAND($iStars, 1) * Random(1, 10, 1), $iRandColor)             $iStars += 1         Case 3             $aShapeGUIs[$i] = _DiamondGUICreate($iRandX, $iRandY, $iRandLength, Default, BitAND($iDiamonds, 1) * Random(1, 10, 1), $iRandColor)             $iDiamonds += 1         Case Else ; 4             $aShapeGUIs[$i] = _BoxGUICreate($iRandX, $iRandY, $iRandLength, BitAND($iBoxes, 1) * Random(1, 10, 1), Default, $iRandColor)             $iBoxes += 1     EndSwitch     ; Show the randomly created GUI     GUISetState(@SW_SHOWNOACTIVATE, $aShapeGUIs[$i]) ; And set a random transparency too     WinSetTrans($aShapeGUIs[$i], '', Random(50, 255, 1)) Next ConsoleWrite("GUI Totals: Triangles:" & $iTriangles & ", Circles:" & $iCircles & ", Stars:" & $iStars & ", Diamonds:" & $iDiamonds & ", Boxes:" & $iBoxes & @CRLF) ; Timer for moving shapes $iTimer = TimerInit() While 1     ; Exit on 'ESC' keypress (BitAND() test for down-state)     If BitAND(_WinAPI_GetAsyncKeyState(0x1B), 0x8000) Then ExitLoop     Sleep(10)     ; Move a random GUI every 30+ms     If TimerDiff($iTimer) >= 30 Then         $iRand = Random(0, $iShapeGUIs - 1, 1)         ; Set GUI above other windows         WinSetOnTop($aShapeGUIs[$iRand], "", 1)         ; Move to a random position         WinMove($aShapeGUIs[$iRand], "", Random(0, @DesktopWidth - 20, 1), Random(0, @DesktopHeight - 20, 1), Default, Default, 2)         ; Reset timer         $iTimer = TimerInit()     EndIf WEnd _
      LineTraverserExample - Where's the ball? Huh, where is it, Fido?! Ooh, there it is! Fetch the ball! Gooood circle..
      #include "_GUIShapes.au3" #include "_LineTraverser.au3" ;~ #include <WinAPI.au3> ; ======================================================================================================== ; <LineTraverserExample.au3> ; ; Simple Example of using the <_LineTraverser.au3> and <_GuiShapes.au3> UDF's ; ;    A little red-ball will display, and a hollow ball will move towards it. ;    A line will be drawn to show how the path from the hollow ball to the red-ball target should work, ;    and then the hollow ball moves to it in $iStep increments. ; ; ; Author: Ascend4nt ; ======================================================================================================== Global Const $iStep = 2 Local $hHollowCircle, $hDestCircle, $iExt Local $iXTarget, $iYTarget, $aLineTraverser ; Create the 2 circle GUIs $hHollowCircle = _CircleGUICreate(Random(0, @DesktopWidth - 20, 1), Random(0, @DesktopHeight - 20, 1), 81, 10, Default, Random(0x111111, 0xFFFFFF, 1)) $hDestCircle = _CircleGUICreate(1, 1, 17, 0, 17, 0xFF0000) ; Set initial target point $iXTarget = Random(0, @DesktopWidth - 20, 1) $iYTarget = Random(0, @DesktopHeight - 20, 1) ; Source/Target are same to start off with $aLineTraverser = _LineTraverserCreate($iXTarget, $iYTarget, $iXTarget, $iYTarget) ; Move windows to start positions WinMove($hHollowCircle, '', $aLineTraverser[0], $aLineTraverser[1]) ; + Center of hollow circle, - half of target circle WinMove($hDestCircle, '', $iXTarget + 40 - 8, $iYTarget + 40 - 8) ; Transparency on 'seeker' circle WinSetTrans($hHollowCircle, '', 150) ; Show both GUIs, and put on top of all windows WinSetState($hDestCircle, '', @SW_SHOWNOACTIVATE) WinSetState($hHollowCircle, '', @SW_SHOWNOACTIVATE) WinSetOnTop($hHollowCircle, '', 1) WinSetOnTop($hDestCircle, '', 1) While 1     ; Exit on 'ESC' keypress (in down state)     If BitAND(_WinAPI_GetAsyncKeyState(0x1B), 0x8000) Then ExitLoop     ; < 10 ms sleep with an API call     DllCall("kernel32.dll",'none','Sleep','dword',3)     If Not _LineTraverserStep($aLineTraverser, $iStep) Then         $iExt = @extended         ; Was there movement? Then moooove         If $iExt Then             WinMove($hHollowCircle, '', $aLineTraverser[0], $aLineTraverser[1])         EndIf         $aPos = WinGetPos($hHollowCircle)         ; Debug check.  Should never be hit, so long as Window is moved to each step (including any last steps - see @extended)         If $iXTarget <> $aPos[0] Or $iYTarget <> $aPos[1] Then             ConsoleWrite("Mismatch: TargetX:" & $iXTarget & ", TraverserX:" & $aLineTraverser[0] & ", Current X:" & $aPos[0] & _                     ", TargetY:" & $iYTarget & ", TraverserY:" & $aLineTraverser[1] & ", Current Y:" & $aPos[1] & ", @extended:" & $iExt & @CRLF)                 EndIf         ; A little extra sleep to make it clear we've reached our destination.         DllCall("kernel32.dll",'none','Sleep','dword',6)         ; Now we'll set a new destination (with a visible line)         Dim $iXTarget = Random(0, @DesktopWidth - 20, 1), $iYTarget = Random(0, @DesktopHeight - 20, 1)         ; Create a new Line-Traverser (no need to explicitly destroy the last one, it was just an array of numbers)         $aLineTraverser = _LineTraverserCreate($aLineTraverser[0], $aLineTraverser[1], $iXTarget, $iYTarget)         ; + Center of hollow circle, - center of target circle         WinMove($hDestCircle, '', $iXTarget + 40 - 8, $iYTarget + 40 - 8) ;~     Draw the line on-screen to give a visual indicator of the path that the hollow circle should take ;~     (note that the line will be overwritten by ANY screen activity, but that's fine for the example)         ; Get DC to screen         $hDC = _WinAPI_GetDC(0)         ; Create pen and select it into DC         $hPen = _WinAPI_CreatePen(0, 3, 0xFFFFFF)         $hPenOld = _WinAPI_SelectObject($hDC, $hPen)         ; Note we add 40 (for the center of the hollow circle GUI)         _WinAPI_DrawLine($hDC, $aPos[0] + 40 - 1, $aPos[1] + 40 - 1, $iXTarget + 40 - 1, $iYTarget + 40 - 1)         ; Select the old pen back         _WinAPI_SelectObject($hDC, $hPenOld)         ; Clean up pen and then release DC         _WinAPI_DeleteObject($hPen)         _WinAPI_ReleaseDC(0, $hDC)         ; What fun drawing with GDI is  =|     Else         WinMove($hHollowCircle, '', $aLineTraverser[0], $aLineTraverser[1])     EndIf WEnd GUIShapesFun.zip ~prev downloads: 60

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.