Jump to content
Sign in to follow this  
Zedna

_MouseTrap() in Misc.au3

Recommended Posts

Zedna

missing return values and other optimizations

original code:

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 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

corrected and optimized code:

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)
        Return 0
    EndIf

    If @NumParams == 2 Then
        $i_right = $i_left + 1
        $i_bottom = $i_top + 1
    EndIf
    
    Local $r = DllStructCreate("int;int;int;int")
    If @error Then Return -1
    DllStructSetData($r, 1, $i_left)
    DllStructSetData($r, 2, $i_top)
    DllStructSetData($r, 3, $i_right)
    DllStructSetData($r, 4, $i_bottom)
    DllCall("user32.dll", "int", "ClipCursor", "ptr", DllStructGetPtr($r))
    DllStructDelete($r)
    Return 0
EndFunc;==>_MouseTrap

EDIT: version 3.1.1.67

Edited by Zedna

Share this post


Link to post
Share on other sites
Zedna

:whistle: Don't understand the Return addition since that is implicit nor the optimization ?

<{POST_SNAPBACK}>

Now looking at HelpFile - You are right.

I'm sorry, I didn't know about that implicit return value 0 for user defined functions.

So there are only my optimizations:

variables with constants that are used only once...

EDIT: --> Local $Rect = "int;int;int;int", $left = 1, $top = 2, $right = 3, $bottom = 4

Edited by Zedna

Share this post


Link to post
Share on other sites
GaryFrost

if your worried about returns this would be more appropriate.

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 Then
            $i_right = $i_left + 1
            $i_bottom = $i_top + 1
        EndIf
        Local $Rect = DllStructCreate("int;int;int;int")
        If @error Then Return 0
        DllStructSetData($Rect, 1, $i_left)
        DllStructSetData($Rect, 2, $i_top)
        DllStructSetData($Rect, 3, $i_right)
        DllStructSetData($Rect, 4, $i_bottom)
        Local $ret = DllCall("user32.dll", "int", "ClipCursor", "ptr", DllStructGetPtr($Rect))
        DllStructDelete($Rect)
        Return $ret[0]
    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
w0uter

@numparams = 0 always returns 0.

so i think this would be better.

Func _MouseTrap($i_left = 0, $i_top = 0, $i_right = 0, $i_bottom = 0)
    If @NumParams == 0 Then
        $av_Ret = DllCall("user32.dll", "int", "ClipCursor", "int", 0)
    Else
        If @NumParams == 2 Then
            $i_right = $i_left + 1
            $i_bottom = $i_top + 1
        EndIf
        Local $v_Rect = DllStructCreate("int;int;int;int")
        If @error Then Return 0
        DllStructSetData($v_Rect, 1, $i_left)
        DllStructSetData($v_Rect, 2, $i_top)
        DllStructSetData($v_Rect, 3, $i_right)
        DllStructSetData($v_Rect, 4, $i_bottom)
        Local $av_Ret = DllCall("user32.dll", "int", "ClipCursor", "ptr", DllStructGetPtr($v_Rect))
        DllStructDelete($v_Rect)
    EndIf
    Return $av_Ret[0]
EndFunc  ;==>_MouseTrap

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Share this post


Link to post
Share on other sites
GaryFrost

Func _MouseTrap($i_left = 0, $i_top = 0, $i_right = 0, $i_bottom = 0)
    Local $av_ret
    If @NumParams == 0 Then
        $av_ret = DllCall("user32.dll", "int", "ClipCursor", "int", 0)
    Else
        If @NumParams == 2 Then
            $i_right = $i_left + 1
            $i_bottom = $i_top + 1
        EndIf
        Local $Rect = DllStructCreate("int;int;int;int")
        If @error Then Return 0
        DllStructSetData($Rect, 1, $i_left)
        DllStructSetData($Rect, 2, $i_top)
        DllStructSetData($Rect, 3, $i_right)
        DllStructSetData($Rect, 4, $i_bottom)
        $av_ret = DllCall("user32.dll", "int", "ClipCursor", "ptr", DllStructGetPtr($Rect))
        DllStructDelete($Rect)
    EndIf
    Return $av_ret[0]
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
jpm

as it is not a bug I move it to developper forum so you can continue to post :whistle:

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  

×

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.