• GPL
    autoit
_LoginBox.au3
Advanced AutoIt Media Player (or AAMP for short) is a media player created using AutoIt V3. It
Code
  1. Opt("GUICoordMode", " 2")
  2. $ret = _GUICreateLogin("Enter Credentials", "Enter the username and password", -1, -1, -1, -1, -1, 10000)
  3.         MsgBox(0, "Error", "Error Returned: " & $ret & @CRLF & "Error Code: " & @error & @CRLF & "Extended: " & @extended)
  4.         MsgBox(0, "Credentails Returned", "Username: " & $ret[0] & @CRLF & "Password:  " & $ret[1])
  5.  
  6. ;===============================================================================
  7. ; Function Name:   _GUICreateLogin()
  8. ; Description:     Create a basic login box with Username, Password, and a prompt.
  9. ; Syntax:
  10. ; Parameter(s):    $hTitle - The title of the Form
  11. ;                  $hPrompt - The text prompt for the user [Optional] (maximum of 2 lines)
  12. ;                  $bBlank - The password or username can be blank. [optional]
  13. ;                            Default = False (Blanks are not allowed)
  14. ;                  $hWidth - Width of the form [optional] - default = 250
  15. ;                  $hHeight - Height of the form [optional] - default = 130
  16. ;                  $hLeft - X position [optional] - default = centered
  17. ;                  $hTop - Y position [optional] - default = centered
  18. ;                  $Timeout - The timeout of the form [optional - default = 0 (no timeout)
  19. ;                  $ShowError - Prompts are displayed to the user with timeout [optional]
  20. ; Requirement(s):  None
  21. ; Return Value(s): Success - Returns an array of 2 elements where
  22. ;                                        [0] = UserName
  23. ;                                        [1] = Password
  24. ;                  Failure - Sets @Error to 1 and
  25. ;                                 @Extended - 1 = Cancel/Exit Button Pressed
  26. ;                                           - 2 = Timed out
  27. ; Author(s):   Brett Francis (exodus.is.me@hotmail.com)
  28. ; Modification(s): GeoSoft
  29. ; Note(s): If $hPrompt is blank then the GUI height will be reduced by 30
  30. ; Example(s):
  31. ;===============================================================================
  32.  
  33. Func _GUICreateLogin($hTitle, $hPrompt = "", $bBlank = False, $hWidth = -1, $hHeight = -1, $hLeft = -1, $hTop = -1, $timeout = 0, $ShowError = 0)
  34.         If Not $hTitle Then $hTitle = "Login"
  35.         $iGCM = Opt("GUICoordMode", 2);; Get the current value of GUICoordMode and set it to 2
  36.         If StringRegExp($bBlank, "(?i)\s|default|-1") Then $bBlank = False
  37.         If StringRegExp($hWidth, "(?i)\s|default|-1") Then $hWidth = 250
  38.         If StringRegExp($hHeight, "(?i)\s|default|-1") Then $hHeight = 130
  39.         If StringRegExp($hLeft, "(?i)\s|default|-1") Then $hLeft = (@DesktopWidth / 2) - ($hWidth / 2)
  40.         If StringRegExp($hTop, "(?i)\s|default|-1") Then $hTop = (@DesktopHeight / 2) - ($hHeight / 2)
  41.         If Not $hPrompt Then $hHeight -= 30;If $hPrompt is blank then resize the GUI.
  42.         Local $retarr[2] = ["", ""], $Time = 0
  43.  
  44.         Local $gui = GUICreate($hTitle, $hWidth, $hHeight, $hLeft, $hTop)
  45.         GUISetCoord(4, 0)
  46.         If $hPrompt Then Local $Lbl_Prompt = GUICtrlCreateLabel($hPrompt, -1, 4, 201, 30)
  47.         Local $Lbl_User = GUICtrlCreateLabel("Username:", -1, 4, 64, 17);44, 64, 17)
  48.         GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
  49.         Local $username = GUICtrlCreateInput('', 8, -1, $hWidth - 81, 21)
  50.         GUICtrlCreateLabel("Password:", -($hWidth - 8), 4, 65, 17)
  51.         GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
  52.         Local $password = GUICtrlCreateInput('', 8, -1, $hWidth - 81, 21, 32);, $ES_PASSWORD)
  53.         GUISetCoord(($hWidth / 2) - 85, $hHeight - 34)
  54.         Local $Btn_OK = GUICtrlCreateButton("&OK", -1, -1, 75, 25)
  55.         Local $Btn_Cancel = GUICtrlCreateButton("&Cancel", 20, -1, 75, 25)
  56.         GUICtrlSetState($Btn_OK, 512)
  57.         GUISetState()
  58.         If $timeout Then $Time = TimerInit()
  59.         While 1
  60.                 $Msg = GUIGetMsg()
  61.                 Local $sUser = GUICtrlRead($username)
  62.                 Local $sPass = GUICtrlRead($password)
  63.                 If ($Time And TimerDiff($Time) >= $timeout) And ($sUser = "" And $sPass = "") Then
  64.                         $Status = 2
  65.                         ExitLoop
  66.                 EndIf
  67.                 Select
  68.                         Case $Msg = -3
  69.                                 $Status = 0
  70.                                 ExitLoop
  71.                         Case $Msg = $Btn_OK
  72.                                 If $bBlank And ($sUser = "" Or $sPass = "") Then
  73.                                         $Status = 1
  74.                                         ExitLoop
  75.                                 Else
  76.                                         If $sUser <> "" And $sPass <> "" Then
  77.                                                 $Status = 1
  78.                                                 ExitLoop
  79.                                         Else
  80.                                                 Select
  81.                                                         Case $sUser = "" And $sPass = ""
  82.                                                                 If $ShowError = 1 Then MsgBox(16, "Error!", "Username and Password cannot be blank!")
  83.                                                         Case $sPass = ""
  84.                                                                 If $ShowError = 1 Then MsgBox(16, "Error!", "Password cannot be blank!")
  85.                                                                 GUICtrlSetState($password, 256)
  86.                                                         Case $sUser = ""
  87.                                                                 If $ShowError = 1 Then MsgBox(16, "Error!", "Username cannot be blank!")
  88.                                                                 GUICtrlSetState($username, 256)
  89.                                                 EndSelect
  90.                                         EndIf
  91.                                 EndIf
  92.                         Case $Msg = $Btn_Cancel
  93.                                 $Status = 0
  94.                                 ExitLoop
  95.                         Case $timeout And TimerDiff($Time) >= $timeout; And $timeout
  96.                                 If $bBlank And ($sUser = "" Or $sPass = "") Then
  97.                                         $Status = 2
  98.                                         ExitLoop
  99.                                 Else
  100.                                         ;$time = TimerInit()
  101.                                         Select
  102.                                                 Case $sUser = "" And $sPass = ""
  103.                                                         If $timeout Then $Time = TimerInit()
  104.                                                         If $ShowError = 1 Then MsgBox(16, "Error!", "Username and Password cannot be blank!")
  105.                                                 Case $sPass = ""
  106.                                                         If $timeout Then $Time = TimerInit()
  107.                                                         If $ShowError = 1 Then
  108.                                                                 MsgBox(16, "Error!", "Password cannot be blank!")
  109.                                                                 GUICtrlSetState($password, 256)
  110.                                                         EndIf
  111.                                                 Case $sUser = ""
  112.                                                         $Time = TimerInit()
  113.                                                         If $ShowError = 1 Then
  114.                                                                 MsgBox(16, "Error!", "Username cannot be blank!")
  115.                                                                 GUICtrlSetState($username, 256)
  116.                                                         EndIf
  117.                                                         ;Case ($Timeout AND TimerDiff($time) >= $timeout) AND ($sUser = "" OR $sPass = "")
  118.                                                         ;$status = 2
  119.                                                         ;ExitLoop
  120.                                                 Case Else
  121.                                                         If $sUser <> "" And $sPass <> "" Then
  122.                                                                 $Status = 3
  123.                                                                 ;If $Timeout AND TimerDiff($time) >= $timeout Then $Status = 2
  124.                                                                 ExitLoop
  125.                                                         EndIf
  126.                                         EndSelect
  127.                                 EndIf
  128.                 EndSelect
  129.         WEnd
  130.         Local $eMsg = ""
  131.         Switch $Status
  132.                 Case 0, 2
  133.                         $err = 1;0
  134.                         $ext = 1;Cancel/Exit Button Pressed
  135.                         $eMsg = "Cancel/Exit Button Pressed"
  136.                         If $Status = 2 Then $ext = 2;Timed Out
  137.                         If $ext = 2 Then $eMsg = "Timed Out"
  138.                 Case Else;1, 3
  139.                         $retarr[0] = $sUser
  140.                         $retarr[1] = $sPass
  141.                         $err = 0;1
  142.                         $ext = 0
  143.                         If $Status = 3 Then $ext = 1;Username Fields Not Blank, Timeout reached
  144.         EndSwitch
  145.         GUIDelete($gui)
  146.         Opt("GUICoordMode", $iGCM);Reset the GUICoordMode to what it started as.
  147.         If $eMsg Then Return SetError($err, $ext, $eMsg)
  148.         Return $retarr
  149. EndFunc   ;==>_GUICreateLogin
  150.