Jump to content

Autoit Help - MouseTrapping a GUI


Recommended Posts

Have some Autoit Code I seriously need addressing

With the help of others I've created this:

#include <Misc.au3>

Opt('GUIOnEventMode', 1)

#Region Pref GUI
$PGUI = GUICreate('Login', 170, 110, -1, -1, BitOR(0x00040000, 0x00000080))
GUISetOnEvent(-3, '_PrefClose')

GUICtrlCreateGroup('Enter Password', 5, 10, 155, 40)
$password = GUICtrlCreateInput("", 10, 25, 145, 20, 0x0020) ; $ES_PASSWORD style <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

GUICtrlCreateButton('Apply', 55, 55, 50, 20)
GUICtrlSetOnEvent(-1, '_PrefClose')
GUISetState(@SW_HIDE, $PGUI)
#EndRegion Pref GUI

; Always run the application
_PrefOpen()

Func _MouseTrapCurrent($hWin)
If WinExists($hWin) Then
$Pos = WinGetPos($hWin)
If Not @error Then
_MouseTrap($Pos[0], $Pos[1], $Pos[0] + $Pos[2], $Pos[1] + $Pos[3])
EndIf
Else
_MouseTrap()
Endif
EndFunc
While 1
_MouseTrapCurrent('Login')
WEnd

Func _PrefOpen()
GUISetState(@SW_SHOW, $PGUI)
EndFunc ;==>_PrefOpen

Func _PrefClose()
Exit GUISetState(@SW_HIDE, $PGUI)
EndFunc ;==>_PrefClose
#EndRegion Pref Func

While 1
Sleep(10)
WEnd

Which basically freezes the mouse into the password box perfectly as it should.

However.. I cannot get it to work within this:

Func _PasswordOpen()
$PASSGUI = GUICreate('Login', 170, 110, -1, -1, BitOR(0x00040000, 0x00000080))
GUISetOnEvent(-3, '_PasswordClose')

GUICtrlCreateGroup('Enter Password', 5, 10, 155, 40)
Global $password = GUICtrlCreateInput("", 10, 25, 145, 20, 0x0020) ; $ES_PASSWORD style <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

GUICtrlCreateButton('OK', 55, 55, 50, 20)
GUICtrlSetOnEvent(-1, '_PasswordCheck')

GUISetState(@SW_HIDE, $PASSGUI)
GUISetState(@SW_SHOW, $PASSGUI)
EndFunc ;==>_PasswordOpen

Func _MouseTrapCurrent($hWin)
If WinExists($hWin) Then
$Pos = WinGetPos($hWin)
If Not @error Then
_MouseTrap($Pos[0], $Pos[1], $Pos[0] + $Pos[2], $Pos[1] + $Pos[3])
EndIf
Else
_MouseTrap()
Endif
EndFunc
While 1
_MouseTrapCurrent('Login')
WEnd

Switch _PasswordCheck()
Case 1
MsgBox(0, "Result", "Login Correct")
Case 0
MsgBox(0, "Result", "Login Cancelled ")
case -1
MsgBox(0, "Result", "Failed 5 times lock out user")
EndSwitch

Func _PasswordCheck()
Local $iXTimes = 5, $iCount = 1
While 1
_PasswordOpen()
Local $bPasswordHash=(FileReadLine($sav, 5))
Local $bMasterPasswordHash=(FileReadLine($sav, 6))

$sPassWord = GUICtrlRead($password)
If _Crypt_HashData($sPassword,$CALG_MD5)=$bPasswordHash or _Crypt_HashData($sPassword,$CALG_MD5)=$bMasterPasswordHash Then Return $sPassword
Select
Case $sPassword = "Correct"
Return 1
Case $iCount = $iXTimes
BlockInput(1) ; Freeze user's mouse/keyboard -- likely will omit this but here for testing the lock down.
MsgBox(16, "Failed Password Login attempt limit reached", "Please contact your local Associate or Front Store Manager for Assistance..")
Return -1
Case Else
MsgBox(16, "Access Denied", "Password Incorrect!" & @CR & "You have " & $iXTimes - $iCount & " trys left")
$iCount += 1
EndSelect
WEnd
EndFunc ;==>_PasswordCheck

The Login GUI box is essentially conflicting with the PasswordCheck function by not working together AND also NOT freezing the mouse in the Login box as in the first code example. I'm also there just need some help merging both code bits together. The key was using a GUI rather than an InputBox which should then properly hold the mouse in place. This all has to work within the greater project.

If you want I can post the entire code. Its meant for a screensaver we're working on which simulates Windows Authentication via AD (which is turned off in the environment I work in).

Thanks MSFN team!

Link to comment
Share on other sites


#Include <Misc.au3>
#include <GUIConstantsEx.au3>

Opt ("GUIOnEventMode", 1)

global $passgui

_PasswordOpen()


Func _PasswordCheck()
Local $iXTimes = 5, $iCount = 1
While 1

Local $bPasswordHash=(FileReadLine($sav, 5))
Local $bMasterPasswordHash=(FileReadLine($sav, 6))

$sPassWord = GUICtrlRead($password)
If _Crypt_HashData($sPassword,$CALG_MD5)=$bPasswordHash or _Crypt_HashData($sPassword,$CALG_MD5)=$bMasterPasswordHash Then Return $sPassword
Select
Case $sPassword = "Correct"
Return 1
Case $iCount = $iXTimes
BlockInput(1)
MsgBox(16, "Failed Password Login attempt limit reached", "Please contact your local Associate or Front Store Manager for Assistance..")
Return -1
Case Else
MsgBox(16, "Access Denied", "Password Incorrect!" & @CR & "You have " & $iXTimes - $iCount & " trys left")
$iCount += 1
EndSelect
WEnd
EndFunc ;==>_PasswordCheck


Func _MouseTrapCurrent($hWin)
If WinExists($hWin) Then
$Pos = WinGetPos($hWin)
If Not @error Then
_MouseTrap($Pos[0], $Pos[1], $Pos[0] + $Pos[2], $Pos[1] + $Pos[3])
EndIf
Else
_MouseTrap()
Endif
EndFunc



Func _PasswordOpen()

Local $msg

$PASSGUI = GUICreate('Login', 170, 110, -1, -1, BitOR(0x00040000, 0x00000080))


GUISetOnEvent(-3, '_PasswordClose')

GUICtrlCreateGroup('Enter Password', 5, 10, 155, 40)
Global $password = GUICtrlCreateInput("", 10, 25, 145, 20, 0x0020)


$ok1 = GUICtrlCreateButton('OK', 55, 55, 50, 20)
GUICtrlSetOnEvent($ok1, '_PasswordCheck')

GUISetState(@SW_SHOW, $PASSGUI)

While 1
_MouseTrapCurrent('Login')
WEnd

While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()

EndFunc


Func _PasswordClose()
MsgBox(0, "cancel Pressed" , "cancel pressed. GUI ID=" & @gui_ctrlid & "winhandle=" & @gui_winhandle )
guidelete()
exit
endfunc

***I did notice that you pop a standard message box for the "Access Denied" msgbox. The users mouse will still be constrained to the GUI and they wont be able to click ok on that.***

Edited by iamtheky
Link to comment
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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...