Sign in to follow this  
Followers 0
Skrip

UDF: _MouseTrapWin

5 posts in this topic

#1 ·  Posted (edited)

UDF and example script below. It's commented, so it it all explained in the script.

#include <Misc.au3>; Required for the _MouseTrap() Func
HotKeySet("h", "_exit"); So you can easily exit

MsgBox(0, "", "This pauses the script for 3 seconds, so you can view the window before the mouse is captured.", 3)

While 1
    _MouseTrapWin("Untitled - Notepad", 1, 0, 1); Trap the mouse to a notepad window, capture it, do NOT allow the selection of the title bar, activate the window and keep it active
WEnd

Func _MouseTrapWin($mWin, $mEnable = 1, $mTit = 1, $mAct = 0); $mWin = window to snap to - $mEnable = capture the mouse or release it 1/0 - $mTit = allow access to the title bar 1/0 - mAct = Activate the window?; This must be called constantly!!
    If WinExists($mWin) Then
        If $mAct = 1 Then
            WinActivate($mWin)
        EndIf
        If $mEnable = 1 Then
            $mPos = WinGetPos($mWin)
            If $mTit = 0 Then
                _MouseTrap($mPos[0]+4, $mPos[1]+30, $mPos[0] + $mPos[2]-4, $mPos[1] + $mPos[3]-4); Capture mouse to window minus the title bar
;~              ConsoleWrite($mPos[0] & "|" & $mPos[1] & "|" & $mPos[0] + $mPos[2] & "|" & $mPos[1] + $mPos[3] & @CRLF); Output the Coords of the window
            Else
                _MouseTrap($mPos[0]+4, $mPos[1]+30, $mPos[0] + $mPos[2]-4, $mPos[1] + $mPos[3]-4); Capture mouse to window
;~              ConsoleWrite($mPos[0] & "|" & $mPos[1] & "|" & $mPos[0] + $mPos[2] & "|" & $mPos[1] + $mPos[3] & @CRLF); Output the Coords of the window
            EndIf
        EndIf
    Else
        _MouseTrap(); Exit the Capture
        SetError(1); Window does not exist
        ConsoleWriteError("Error " & @error & " - Window does not exist." & @CRLF)
    EndIf
EndFunc

Func _exit()
    _MouseTrap(); Exit the Capture
    Exit
EndFunc

Needed for a little project..thought I would share.

Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites



Great stuff; haven't tried it yet, but i see potential for internet cafe application.

Share this post


Link to post
Share on other sites

Thank you. I made sure to make it so you can/cannot select the title bar so you can/cannot move the window around aswell.


[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I've created something similar long time ago.

#include <Misc.au3>
#include <WinAPI.au3>
Opt('MustDeclareVars', 1)


; #FUNCTION# ====================================================================================================================
; Name...........: _WinTrap
; Description ...: Confine the Mouse Cursor to specified window
; Syntax.........: _WinTrap($hWnd = "", $iFrame = 0, $Console = False)
; Parameters ....: $hWnd    - Handle to the window
;                  $iFrame  - Frame size in pixels
;                  $Console - Write to console
; Return values .: Success - True
;                  Failure - False
;                            @ERROR  - 0 = No Error
;                                    - 1 = Invalid hWnd
;                                    - 2 = hWnd does not exists or minimized/maximized

; Author ........: Me
; Related .......: _MouseTrap
; ===============================================================================================================================
Func _WinTrap($hWnd = "", $iFrame = 0, $Console = False)
    Local Const $SM_CYCAPTION = 4
    Local $tRect, $W_Left, $W_Top, $W_Right, $W_Bottom, $C_Width, $C_Height, $iTitleHeight
    
    ;   Check hWnd
    If Not IsHWnd($hWnd) Then
        $hWnd = WinGetHandle($hWnd)
        If @error Then Return SetError(1, 0, False)
    EndIf
    
    ;   Check WinState
    If Not BitAND(WinGetState($hWnd), 1) Or Not BitAND(WinGetState($hWnd), 2) Or Not BitAND(WinGetState($hWnd), 4) Or BitAND(WinGetState($hWnd), 16) Or BitAND(WinGetState($hWnd), 32) Then Return SetError(2, 0, False)
    If $Console Then ConsoleWrite("State: " & WinGetState($hWnd) & @LF)
    
    ;   GetWindowRect
    $tRect = _WinAPI_GetWindowRect($hWnd)
    
    $W_Left = DllStructGetData($tRect, "Left")
    $W_Top = DllStructGetData($tRect, "Top")
    $W_Right = DllStructGetData($tRect, "Right")
    $W_Bottom = DllStructGetData($tRect, "Bottom")
    
    If $Console Then
        ConsoleWrite("Left: " & $W_Left & @LF)
        ConsoleWrite("Top: " & $W_Top & @LF)
        ConsoleWrite("Right: " & $W_Right & @LF)
        ConsoleWrite("Bottom: " & $W_Bottom & @LF)
    EndIf
    
    ;   GetClientRect
    $tRect = _WinAPI_GetClientRect($hWnd)
    
    $C_Width = DllStructGetData($tRect, "Right")
    $C_Height = DllStructGetData($tRect, "Bottom")
    
    If $Console Then
        ConsoleWrite("Width: " & $C_Width & @LF)
        ConsoleWrite("Height: " & $C_Height & @LF)
    EndIf
    
    ;   Set frame size
    $iFrame = ($W_Right - $W_Left - $C_Width)/2 + $iFrame
    $iTitleHeight = _WinAPI_GetSystemMetrics($SM_CYCAPTION)
    
    If $Console Then
        ConsoleWrite("TitleBar: " & $iTitleHeight & @LF)
        ConsoleWrite("Frame: " & $iFrame & @LF & @LF)
    EndIf
    
    ;   MouseTrap
    Return _MouseTrap($W_Left + $iFrame, $W_Top + $iFrame + $iTitleHeight, $W_Right - $iFrame, $W_Bottom - $iFrame)
    
EndFunc

As you can see, I've used _WinAPI_GetSystemMetrics($SM_CYCAPTION) to get the height of the caption (It depends on the current theme), and included an iFrame variable, with you can increase, or decrease the size of the trap area.

Edited by Izebize

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