Sign in to follow this  
Followers 0
GaryFrost

Restrict cursor to area on the screen

25 posts in this topic

#1 ·  Posted (edited)

Requires beta version

#include <GuiConstants.au3>

Opt("MustDeclareVars",1)

Dim $GUI, $coords[4], $msg

$GUI = GUICreate("Mouse Trap Example", 392, 323)

GUISetState()

While 1
    $coords = WinGetPos($GUI)
    _MouseTrap($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3])
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case Else
    ;;;
    EndSelect
WEnd
_MouseTrap()
Exit

UDF:

;===============================================================================
;
; Description:        _MouseTrap
; Parameter(s):    $i_left - Left coord
;                        $i_top - Top coord
;                        $i_right - Right coord
;                        $i_bottom - Bottom coord
; User CallTip:    _MouseTrap([$i_left = 0[, $i_top = 0[, $i_right = 0[, $i_bottom = 0]]]]) Confine the Mouse Cursor to specified coords. (required: <Misc.au3>)
; Author(s):        Gary Frost (custompcs@charter.net)
; Note(s):            Use _MouseTrap() with no params to release the Mouse Cursor
;
;===============================================================================
Func _MouseTrap($i_left = 0, $i_top = 0, $i_right = 0, $i_bottom = 0)
    If @NumParams == 0 Then
        DllCall("user32.dll", "int", "ClipCursor", "int", 0)
    Else
        If @NumParams == 2 And ($i_left > 0 Or $i_top > 0) Then
            $i_right = $i_left + 1
            $i_bottom = $i_top + 1
        EndIf
        Local $Rect = "int;int;int;int", $left = 1, $top = 2, $right = 3, $bottom = 4, $r
        $r = DllStructCreate($Rect)
        If @error Then Return -1
        DllStructSetData($r, $left, $i_left)
        DllStructSetData($r, $top, $i_top)
        DllStructSetData($r, $right, $i_right)
        DllStructSetData($r, $bottom, $i_bottom)
        DllCall("user32.dll", "int", "ClipCursor", "ptr", DllStructGetPtr($r))
        DllStructDelete($r)
    EndIf
EndFunc  ;==>_MouseTrap
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites



Looking at possibly adding this into UDFs, if there is enough people interested in this function being added.

Gary


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Cool! Just Tried it with params of 0,0,0,0 and It locked the cursor in the corner of the screen. Useful for locking the cursor for security reasons.


HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

Updated the UDF, using @Numparams, so if you want 0,0,0,0 as the coords only need to call the function with 1 param of 0

Gary


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

How about having the right and bottom fields default to the left and top ones? Then you could call _CursorClip(0, 0) or _CursorClip(200, 200) and 'stick' the cursor to that location.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

You might want to mention that it's necessary to _CursorClip() before setting a different region.

Also, on my system this works better (no movement):

If @NumParams = 2 Then
    $i_right = $i_left + 1
    $i_bottom = $i_top + 1
EndIf

On my system _CursorClip(0, 0) hides the cursor. That's a handy feature that should probably be documented!

Edited by LxP

Share this post


Link to post
Share on other sites

You might want to mention that it's necessary to _CursorClip() before setting a different region.

Also, on my system this works better (no movement):

If @NumParams = 2 Then
    $i_right = $i_left + 1
    $i_bottom = $i_top + 2
EndIf

On my system _CursorClip(0, 0) hides the cursor. That's a handy feature that should probably be documented!

<{POST_SNAPBACK}>

Ok, changed the if statement added 1 to each only if left or top are not zero

no need to call _ClipCursor() before setting diff region at leas not on my system here's an example

#include <GuiConstants.au3>

HotKeySet("n", "_NewCoords")

Opt("MustDeclareVars",1)

Dim $GUI, $coords[4], $msg

$GUI = GUICreate("Clip Cursor Example", 392, 323)

GUISetState()
_CursorClip(0,0)
While 1
;~   $coords = WinGetPos($GUI)
;~   _CursorClip($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3])
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case Else
   ;;;
    EndSelect
WEnd
_CursorClip()
Exit


Func _NewCoords()
_CursorClip(50,50, 100, 100)
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Oops, must have been calling it improperly -- it's working as expected now (still hides the cursor at (0, 0) though).

Your UDF's blurb mentions that it needs <Misc.au3> -- why is this?

Share this post


Link to post
Share on other sites

That's where I have it on my system right now, if I get enough ppl interested in it, that's probably where it'll end up.

Gary


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Oops, must have been calling it improperly -- it's working as expected now (still hides the cursor at (0, 0) though).

Your UDF's blurb mentions that it needs <Misc.au3> -- why is this?

<{POST_SNAPBACK}>

For when it gets added to AutoIt as a UDF :whistle:

EDIT: Gafrost beat me to it.. :dance: lol

Edited by layer

FootbaG

Share this post


Link to post
Share on other sites

This is ideal for me. Computer-illiterate staff needs to click OK on an error dialog, but instead they just bury it under some other window.

J


If I am too verbose, just say so. You don't need to run on and on.

Share this post


Link to post
Share on other sites

great, well renaming the function _MouseTrap, lil more descriptive of what it does, not sure if it's the same thing that Larry's does in the dll, even if it is the all the functions in dll should be able to be done directly thru the beta version now.

Gary


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

I like _MouseTrap hahah... I think most of Larrys Dll funcs are now native AutoIt commands with Dll*** functions except for _MouseTrap... Sooo, I don't see why this couldn't be added.


FootbaG

Share this post


Link to post
Share on other sites

hmm.. Dude, really nice udf.

maybe u could also create a function which tells me if someone mouseovers a controll / window?

Share this post


Link to post
Share on other sites

Ok, noob Question:

Ok, what button would I press to unlock it? If you dont have that feature I think I could figure out how to add it... with alot of free-time...

Also, what's a UDF? :whistle:

Share this post


Link to post
Share on other sites

in the example in the 1st post by hitting the "Esc" key

UDF: User Defined Function


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Oooh, ok, thanks alot, I'm gonna use this so my brother doesn't go looking through everything while I'm away :whistle:

Cheers

Edited by GoronSlayer99

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