Santa_ryan

APB AFK Bot

Apr 28th, 2013
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 27.96 KB | None | 0 0
  1. #RequireAdmin
  2. #include <file.au3>
  3. #include <Constants.au3>
  4. #include <WinAPI.au3>
  5. _DeclareMsgBoxVars()
  6. #region ;ScriptSettings
  7. #region ;UserSetSettings
  8. Global $Answer, $Character, $District, $Faction, $BorderWidth = _WinAPI_GetSystemMetrics(7), $BorderHeight = _WinAPI_GetSystemMetrics(8) + _WinAPI_GetSystemMetrics(4), $WindowedMode, $WinPos, $Size, $login
  9.  
  10. _MsgBox_SetButtonText(3, "Steam", "G1", "None")
  11. Global $Answer = _MsgBox(3, "Login System", '"Would you like to use the automatic login system? If yes choose the method you will be using. (Steam options requires steam  to be running as well as the account already tied)', 0)
  12.  
  13. If $Answer = 6 Or $Answer = 7 Then
  14.     If $Answer = 7 Then
  15.         Global $login = _GUICreateLogin("G1 Login", "", False)
  16.         If @error Then Exit
  17.     EndIf
  18.  
  19.     Do
  20.         _MsgBox_SetButtonText(4, "Criminal", "Enforcer")
  21.         $Faction = _MsgBox(4, "Faction Selection", "Choose the faction of the character to be botted", 0)
  22.     Until $Faction = 6 Or $Faction = 7
  23.  
  24.     $Character = InputBox("Character Selection", "Which character slot should be botted?" & @LF & @LF & "Must match faction selected previously", "1")
  25.  
  26.     Do
  27.         _MsgBox_SetButtonText(4, "Financial", "Waterfront")
  28.         $District = _MsgBox(4, "District Select", "Choose the district to spawn in.", 0)
  29.     Until $District = 6 Or $District = 7
  30. EndIf
  31.  
  32. Global $PauseKey = InputBox("Pause key", "What key should pause the bot?", "`")
  33.  
  34. Global $PhrasesFile = "Phrases.txt"
  35. #endregion ;UserSetSettings
  36.  
  37. #region ;InternalSettings
  38. Global $Sleep[3][2] = [[5, 15],[0, 10],[5, 15]]
  39. ;Controls the sleep timers, with the timers being seperated by rows and actual sleep time is between the first number and the second number, in seconds
  40.  
  41. Global $RandomAddition[2] = [1, 3]
  42. ;Controls the adder that adds to the "ForceRandomPhrase" variable. It adds a random number between the two values to the $ForceRandomPhrase var.
  43.  
  44. Global $TriggerNumber = 6
  45. ;When the adder makes the "ForceRandomPhrase" variable greater than this number, a random phrase is typed in game.
  46. #endregion ;InternalSettings
  47. #endregion ;ScriptSettings
  48.  
  49. #region ;Internal Vars
  50. ;Login Page
  51. Global $SteamLogin[2] = [0.5, 0.4759259259259259]
  52. Global $LoginEmail[2] = [0.5213541666666667, 0.5759259259259259]
  53. Global $LoginPass[2] = [0.5213541666666667, 0.6305555555555556]
  54. Global $LoginButton[2] = [0.5744791666666667, 0.6759259259259259]
  55.  
  56. ;Character Page
  57. Global $CharacterSlots[2] = [0.0963541666666667, 0.25]
  58. Global $CharacterSpacing = 0.0407407407407407
  59. Global $SelectCharacter[2] = [0.9395833333333333, 0.9768518518518519]
  60.  
  61. ;District Page
  62. Global $FinanDistrict[2] = [0.3416666666666667, 0.4592592592592593]
  63. Global $WFDistrict[2] = [0.4979166666666667, 0.4592592592592593]
  64. Global $JoinDistrict[2] = [0.9411458333333333, 0.9768518518518519]
  65.  
  66. ;Criminals
  67. Global $DoubleB[2] = [0.5182291666666667, 0.5027777777777778]
  68. Global $PaganBloodrose[2] = [0.4270833333333333, 0.5842592592592593]
  69.  
  70. ;Enforcers
  71. Global $TyDurrent[2] = [0.5765625, 0.4046296296296296]
  72. Global $SimonTran[2] = [0.1958333333333333, 0.8111111111111111]
  73.  
  74. Global $PremiumMsg[2] = [0.398958333333333, 0.699074074074074]
  75.  
  76. Global $Paused
  77. Global $Phrases[1]
  78. Global $Hwnd
  79. #endregion ;Internal Vars
  80.  
  81. Global $ForceRandomPhrase
  82. #region ;Main
  83. HotKeySet($PauseKey, "_TogglePause")
  84. _GetSize()
  85. AdlibRegister("_GetSize", 15000)
  86. If $Answer = 6 Or $Answer = 7 Then _LoginSystem($Answer)
  87.  
  88. If FileExists($PhrasesFile) Then _FileReadToArray($PhrasesFile, $Phrases)
  89.  
  90. While 1
  91.     While WinActive($Hwnd)
  92.         Sleep(Number(Random($Sleep[0][0], $Sleep[0][1], 1) * 1000))
  93.         Send("k")
  94.         Send("b")
  95.         Sleep(Number(Random($Sleep[1][0], $Sleep[1][1], 1) * 1000))
  96.         Send("{Enter}")
  97.         Sleep(2000)
  98.         Send("/AbandonMission")
  99.         Sleep(2000)
  100.         Send("{Enter}")
  101.         Sleep(Number(Random($Sleep[2][0], $Sleep[2][1], 1) * 1000))
  102.         Switch Number(Random(1, 4, 1))
  103.             Case 1
  104.                 Send("w down")
  105.                 Sleep(5000)
  106.             Case 2
  107.                 Send("a down")
  108.             Case 3
  109.                 Send("s down")
  110.             Case 4
  111.                 Send("d down")
  112.         EndSwitch
  113.         If FileExists($PhrasesFile) Then _RandomPhrase()
  114.     WEnd
  115. WEnd
  116. #endregion ;Main
  117. #region ;Functions
  118. Func _ProcessGetWindow($aPId)
  119.     $aPId = ProcessExists($aPId)
  120.     If $aPId = 0 Then
  121.         Return SetError(1, 0, False)
  122.     Else
  123.         Local $aWinList = WinList()
  124.         For $i = 1 To $aWinList[0][0]
  125.             If WinGetProcess($aWinList[$i][1], "") = $aPId And $aWinList[$i][0] <> "" And $aWinList[$i][0] <> "DIEmWin" Then Return WinGetHandle($aWinList[$i][1])
  126.         Next
  127.         Return SetError(2, 0, False)
  128.     EndIf
  129. EndFunc   ;==>_ProcessGetWindow
  130. Func _GetSize()
  131.     $Hwnd = _ProcessGetWindow("APB.exe")
  132.     If Not $Paused Then
  133.         Local $aTimeout = TimerInit()
  134.         Do
  135.             WinActivate($Hwnd)
  136.             $Size = WinGetClientSize($Hwnd) ; 0=w, 1=h
  137.             If TimerDiff($aTimeout) > 10000 Then
  138.                 MsgBox(0, "Failure", "Bot could not get size of APB")
  139.                 Exit
  140.             EndIf
  141.         Until IsArray($Size) And $Size[0] > 640 And $Size[1] > 480
  142.         Sleep(5000)
  143.         $WindowedMode = _WindowedMode()
  144.         $WinPos = WinGetPos($Hwnd)
  145.     EndIf
  146. EndFunc   ;==>_GetSize
  147. Func _LoginSystem($A)
  148.     WinActivate($Hwnd)
  149.     Sleep(10000)
  150.     If $A = 6 Then
  151.         _Click($SteamLogin)
  152.     ElseIf $A = 7 Then
  153.         _Click($LoginEmail)
  154.         Send("^a" & $login[0])
  155.         _Click($LoginPass)
  156.         Send("^a" & $login[1])
  157.         _Click($LoginButton)
  158.     EndIf
  159.     Sleep(15000)
  160.     _CharacterSelect($CharacterSlots, $CharacterSpacing, $Character)
  161.     Sleep(10000)
  162.     _Click($SelectCharacter)
  163.     Sleep(10000)
  164.     If $District = 6 Then
  165.         _Click($FinanDistrict)
  166.     ElseIf $District = 7 Then
  167.         _Click($WFDistrict)
  168.     Else
  169.         MsgBox(0,"Error", "Invalid District Detected")
  170.         Exit
  171.     EndIf
  172.     _Click($JoinDistrict)
  173.     If $District = 6 Then
  174.         _Click($FinanDistrict)
  175.         _Click($JoinDistrict)
  176.         Sleep(60000)
  177.         If $Faction = 6 Then
  178.             _Click($DoubleB)
  179.         ElseIf $Faction = 7 Then
  180.             _Click($TyDurrent)
  181.         Else
  182.             MsgBox(0,"Error", "Invalid Faction Detected")
  183.             Exit
  184.         EndIf
  185.     ElseIf $District = 7 Then
  186.         _Click($WFDistrict)
  187.         _Click($JoinDistrict)
  188.         Sleep(60000)
  189.         If $Faction = 6 Then
  190.             _Click($PaganBloodrose)
  191.         ElseIf $Faction = 7 Then
  192.             _Click($SimonTran)
  193.         Else
  194.             MsgBox(0,"Error", "Invalid Faction Detected")
  195.             Exit
  196.         EndIf
  197.     Else
  198.         MsgBox(0,"Error", "Invalid District Detected")
  199.         Exit
  200.     EndIf
  201. EndFunc   ;==>_LoginSystem
  202.  
  203. ;_Click($LoginButton)
  204. Func _Click(ByRef $ClickLocation, $Clicks = 1)
  205.     MouseMove(($WindowedMode * ($BorderWidth + $WinPos[0])) + $ClickLocation[0] * $Size[0], ($WindowedMode * ($BorderHeight + $WinPos[1])) + ($ClickLocation[1] * $Size[1]), 10)
  206.     MouseClick("Left", ($WindowedMode * ($BorderWidth + $WinPos[0])) + $ClickLocation[0] * $Size[0], ($WindowedMode * ($BorderHeight + $WinPos[1])) + ($ClickLocation[1] * $Size[1]), $Clicks, 0)
  207. EndFunc   ;==>_Click
  208.  
  209. ;_CharacterSelect($CharacterSlots, $CharacterSpacing, 1)
  210. Func _CharacterSelect(ByRef $ClickLocation, $aS, $Character)
  211.     MouseMove(($WindowedMode * ($BorderWidth + $WinPos[0])) + $ClickLocation[0] * $Size[0], ($WindowedMode * ($BorderHeight + $WinPos[1])) + ($ClickLocation[1] * $Size[1]), 10)
  212.     MouseClick("Left", ($WindowedMode * ($BorderWidth + $WinPos[0])) + $ClickLocation[0] * $Size[0], ($WindowedMode * ($BorderHeight + $WinPos[1])) + (($ClickLocation[1] + ($aS * ($Character - 1))) * $Size[1]), 1, 0)
  213. EndFunc   ;==>_CharacterSelect
  214.  
  215. Func _RandomPhrase()
  216.     $ForceRandomPhrase += Number(Random($RandomAddition[0], $RandomAddition[1]))
  217.     If $ForceRandomPhrase > Number($TriggerNumber) Then
  218.         Send("{Enter}")
  219.         Sleep(2000)
  220.         Send("/d " & $Phrases[Random(1, $Phrases[0], 1)])
  221.         Sleep(2000)
  222.         Send("{Enter}")
  223.         $ForceRandomPhrase = 0
  224.     EndIf
  225. EndFunc   ;==>_RandomPhrase
  226.  
  227. Func _TogglePause()
  228.     $Paused = Not $Paused
  229.     While $Paused
  230.         Sleep(100)
  231.         ToolTip('AFK Bot is paused', 0, 0)
  232.     WEnd
  233.     ToolTip("")
  234. EndFunc   ;==>_TogglePause
  235.  
  236.  
  237. Func _WindowedMode()
  238.     If ProcessExists("APB.exe") And IniRead(StringReplace(_ProcessGetPath("APB.exe"), "\binaries\apb.exe", "") & "\APBGame\Config\APBMachineOptions.ini", "SystemSettings", "Fullscreen", "TRUE") = "False" Then Return SetExtended(1, True)
  239. EndFunc   ;==>_WindowedMode
  240. Func _ProcessGetPath($vProcess)
  241.     Local $i_PID, $hKernel32, $hPsapi, $aProcessHandle, $tDLLStruct, $iError, $sProcessPath
  242.     $i_PID = ProcessExists($vProcess)
  243.     If Not $i_PID Then Return SetError(1, 0, "");process doesn't exist?
  244.     $hKernel32 = DllOpen("Kernel32.dll")
  245.     $iError = @error
  246.     If $iError Then
  247.         DllClose($hKernel32)
  248.         Return SetError(2, $iError, ""); dllopen kernell32.dll failed
  249.     EndIf
  250.     $aProcessHandle = DllCall($hKernel32, "int", "OpenProcess", "int", 0x0400 + 0x0010, "int", 0, "int", $i_PID)
  251.     $iError = @error
  252.     If $iError Or $aProcessHandle[0] = 0 Then
  253.         DllClose($hKernel32)
  254.         Return SetError(2, $iError, "");openprocess failed
  255.     EndIf
  256.     $hPsapi = DllOpen("PSAPI.dll")
  257.     $iError = @error
  258.     If $iError Then
  259.         DllClose($hKernel32)
  260.         DllClose($hPsapi)
  261.         Return SetError(3, $iError, ""); dllopen psapi.dll failed
  262.     EndIf
  263.     $tDLLStruct = DllStructCreate("char[1000]")
  264.     DllCall($hPsapi, "long", "GetModuleFileNameEx", "int", $aProcessHandle[0], "int", 0, "ptr", DllStructGetPtr($tDLLStruct), "long", DllStructGetSize($tDLLStruct))
  265.     $iError = @error
  266.     DllCall($hKernel32, "int", "CloseHandle", "int", $aProcessHandle[0])
  267.     DllClose($hKernel32)
  268.     DllClose($hPsapi)
  269.     If $iError Then
  270.         $tDLLStruct = 0
  271.         Return SetError(4, $iError, "");getmodulefilenamex failed
  272.     EndIf
  273.     $sProcessPath = DllStructGetData($tDLLStruct, 1)
  274.     $tDLLStruct = 0
  275.     ;format the output
  276.     If StringLen($sProcessPath) < 2 Then Return SetError(5, 0, "");is empty or non readable
  277.     If StringLeft($sProcessPath, 4) = "\??\" Then $sProcessPath = StringReplace($sProcessPath, "\??\", "")
  278.     If StringLeft($sProcessPath, 20) = "\SystemRoot\System32" Then $sProcessPath = StringReplace($sProcessPath, "\SystemRoot\System32", @SystemDir)
  279.     Return SetError(0, 0, $sProcessPath)
  280. EndFunc   ;==>_ProcessGetPath
  281. Func _WinAPI_ShellQueryUserNotificationState()
  282.  
  283.     Local $Ret = DllCall('shell32.dll', 'uint', 'SHQueryUserNotificationState', 'uint*', 0)
  284.  
  285.     If @error Then
  286.         Return SetError(1, 0, 0)
  287.     Else
  288.         If $Ret[0] Then
  289.             Return SetError(1, $Ret[0], 0)
  290.         EndIf
  291.     EndIf
  292.     Return $Ret[1]
  293. EndFunc   ;==>_WinAPI_ShellQueryUserNotificationState
  294. #endregion ;Functions
  295.  
  296. Func _DeclareMsgBoxVars()
  297.     Global Const $__MsgBox_HCBT_MOVESIZE = 0
  298.     Global Const $__MsgBox_HCBT_MINMAX = 1
  299.     Global Const $__MsgBox_HCBT_QS = 2
  300.     Global Const $__MsgBox_HCBT_CREATEWND = 3
  301.     Global Const $__MsgBox_HCBT_DESTROYWND = 4
  302.     Global Const $__MsgBox_HCBT_ACTIVATE = 5
  303.     Global Const $__MsgBox_HCBT_CLICKSKIPPED = 6
  304.     Global Const $__MsgBox_HCBT_KEYSKIPPED = 7
  305.     Global Const $__MsgBox_HCBT_SYSCOMMAND = 8
  306.     Global Const $__MsgBox_HCBT_SETFOCUS = 9
  307.  
  308.     Global Enum $__iMsgBox_IDOK = 1, _
  309.             $__iMsgBox_IDCANCEL, _
  310.             $__iMsgBox_IDABORT, _
  311.             $__iMsgBox_IDRETRY, _
  312.             $__iMsgBox_IDIGNORE, _
  313.             $__iMsgBox_IDYES, _
  314.             $__iMsgBox_IDNO, _
  315.             $__iMsgBox_IDCLOSE, _
  316.             $__iMsgBox_IDHELP, _
  317.             $__iMsgBox_IDTRYAGAIN, _
  318.             $__iMsgBox_IDCONTINUE, _
  319.             $__iMsgBox_IDLASTITEM
  320.  
  321.     Global $__aMsgBoxTextDefault[$__iMsgBox_IDLASTITEM] = [$__iMsgBox_IDLASTITEM - 1, _
  322.             "OK", "Cancel", "&Abort", "&Retry", "&Ignore", "Yes", "No", "", "Help", _
  323.             "&Continue", "&Try Again"]
  324.  
  325.     Global $__iMsgBox_Flag = -1
  326.     Global $__fMsgBox_Move = False
  327.     Global $__hMsgBox_Parent = 0
  328.     Global $__iMsgBox_XPos = -1
  329.     Global $__iMsgBox_YPos = -1
  330.  
  331.     Global $__aMsgBoxText = $__aMsgBoxTextDefault
  332.     Global $__hMsgBoxHook = 0
  333. EndFunc   ;==>_DeclareMsgBoxVars
  334.  
  335. ;===================================================================================================
  336. ;
  337. ; Function Name....:    _CBT_MsgBoxProc()
  338. ; Description......:    Message Box Callback Function
  339. ; Parameter(s).....:
  340. ;                       $i_code:  N/A
  341. ;                       $w_param:  N/A
  342. ;                       $l_param:  N/A
  343. ; Return Value(s)..:
  344. ;                       Success...: N/A
  345. ;                       Failure...: N/A
  346. ;                       Error.....: N/A
  347. ;                       Extended..: N/A
  348. ; Requirement(s)...:
  349. ; Author(s)........:    SmOke_N (Ron Nielsen)
  350. ; Modified.........:
  351. ; Comment(s).......:    Tested with AutoIt 3.3.6.1
  352. ; Example(s).......:    N/A - Internal Use Only
  353. ;
  354. ;===================================================================================================
  355.  
  356. Func _CBT_MsgBoxProc($i_code, $w_param, $l_param)
  357.     #forceref $l_param
  358.  
  359.     Local $h_msgbox = HWnd($w_param)
  360.     Local $t_rect_msg = 0, $t_rect_hwnd
  361.     Local $i_hwnd_left, $i_hwnd_top, $i_hwnd_right, $i_hwnd_bottom
  362.     Local $i_msg_left, $i_msg_top, $i_msg_right, $i_msg_bottom
  363.     #forceref $h_msgbox, $t_rect_msg, $t_rect_hwnd
  364.     #forceref $i_hwnd_left, $i_hwnd_top, $i_hwnd_right, $i_hwnd_bottom
  365.     #forceref $i_msg_left, $i_msg_top, $i_msg_right, $i_msg_bottom
  366.  
  367.     Switch $i_code
  368.         Case $__MsgBox_HCBT_ACTIVATE
  369.  
  370.             If $__fMsgBox_Move Then
  371.  
  372.                 If $__iMsgBox_XPos = -1 Or $__iMsgBox_YPos = -1 Then
  373.                     If $__hMsgBox_Parent Then
  374.                         $t_rect_hwnd = _WinAPI_GetWindowRect($__hMsgBox_Parent)
  375.                     Else
  376.                         $t_rect_hwnd = _WinAPI_GetWindowRect(_WinAPI_GetDesktopWindow())
  377.                     EndIf
  378.                     $i_hwnd_left = DllStructGetData($t_rect_hwnd, "left")
  379.                     $i_hwnd_top = DllStructGetData($t_rect_hwnd, "top")
  380.                     $i_hwnd_right = DllStructGetData($t_rect_hwnd, "right")
  381.                     $i_hwnd_bottom = DllStructGetData($t_rect_hwnd, "bottom")
  382.  
  383.                     $t_rect_msg = _WinAPI_GetWindowRect($h_msgbox)
  384.                     $i_msg_left = DllStructGetData($t_rect_msg, "left")
  385.                     $i_msg_top = DllStructGetData($t_rect_msg, "top")
  386.                     $i_msg_right = DllStructGetData($t_rect_msg, "right")
  387.                     $i_msg_bottom = DllStructGetData($t_rect_msg, "bottom")
  388.                 EndIf
  389.  
  390.                 If $__iMsgBox_XPos = -1 Then
  391.                     $__iMsgBox_XPos = ($i_hwnd_left + ($i_hwnd_right - $i_hwnd_left) / 2) - _
  392.                             (($i_msg_right - $i_msg_left) / 2)
  393.                 EndIf
  394.  
  395.                 If $__iMsgBox_YPos = -1 Then
  396.                     $__iMsgBox_YPos = ($i_hwnd_top + ($i_hwnd_bottom - $i_hwnd_top) / 2) - _
  397.                             (($i_msg_bottom - $i_msg_top) / 2)
  398.                 EndIf
  399.  
  400.                 _WinAPI_SetWindowPos($h_msgbox, 0, $__iMsgBox_XPos, $__iMsgBox_YPos, _
  401.                          - 1, -1, BitOR($SWP_NOSIZE, $SWP_NOZORDER, $SWP_NOACTIVATE))
  402.  
  403.             EndIf
  404.  
  405.             If $__iMsgBox_Flag <> -1 Then
  406.                 For $iid = $__iMsgBox_IDOK To $__iMsgBox_IDLASTITEM - 1
  407.  
  408.                     If $iid = $__iMsgBox_IDCLOSE Then ContinueLoop
  409.  
  410.                     If _WinAPI_GetDlgItem($h_msgbox, $iid) Then
  411.                         DllCall("User32.dll", "int", "SetDlgItemText", "hwnd", $h_msgbox, _
  412.                                 "int", $iid, "str", $__aMsgBoxText[$iid])
  413.                     EndIf
  414.                 Next
  415.             EndIf
  416.  
  417.             _WinAPI_UnhookWindowsHookEx($__hMsgBoxHook)
  418.             $__hMsgBoxHook = 0
  419.  
  420.             Return 0
  421.     EndSwitch
  422.  
  423.     Return _WinAPI_CallNextHookEx($__hMsgBoxHook, $i_code, $w_param, $l_param)
  424. EndFunc   ;==>_CBT_MsgBoxProc
  425.  
  426. ;===================================================================================================
  427. ;
  428. ; Function Name....:    _MsgBox()
  429. ; Description......:    Message box
  430. ; Parameter(s).....:
  431. ;                       $i_flag:  Button(s) flag
  432. ;                       $s_title:  Text of title
  433. ;                       $s_text:  Text of body
  434. ;                       $i_timeout:  ( Optional -1 or Default ) Timeout of message box
  435. ;                       $h_wnd:  ( Optional -1 or Default ) Parent Window
  436. ; Return Value(s)..:
  437. ;                       Success...: Button code that was selected
  438. ;                       Failure...: N/A
  439. ;                       Error.....: N/A
  440. ;                       Extended..: N/A
  441. ; Requirement(s)...:
  442. ; Author(s)........:    SmOke_N (Ron Nielsen)
  443. ; Modified.........:
  444. ; Comment(s).......:    Tested with AutoIt 3.3.6.1
  445. ; Example(s).......:    _MsgBox(64 + 4 + 262144, "Title", "Body Of Text")
  446. ;
  447. ;===================================================================================================
  448.  
  449. Func _MsgBox($i_flag, $s_title, $s_text, $i_timeout = 0, $h_wnd = 0)
  450.  
  451.     If Not $__fMsgBox_Move Then
  452.  
  453.         ; If everything is default we don't need the flag
  454.         If $__iMsgBox_Flag = -1 Then Return MsgBox($i_flag, $s_title, $s_text, $i_timeout, $h_wnd)
  455.  
  456.         ; If flags are different than just set off normal msgbox rather than work through possible conflicts
  457.         If Not (BitAND($i_flag, $__iMsgBox_Flag) = $__iMsgBox_Flag) Then _MsgBox_SetButtonText(-1)
  458.     EndIf
  459.  
  460.     Local $h_cb = DllCallbackRegister("_CBT_MsgBoxProc", "lresult", "int;wparam;lparam")
  461.     Local $p_cb = DllCallbackGetPtr($h_cb)
  462.  
  463.     $__hMsgBoxHook = _WinAPI_SetWindowsHookEx($WH_CBT, $p_cb, 0, _WinAPI_GetCurrentThreadId())
  464.  
  465.     Local $i_ret = MsgBox($i_flag, $s_title, $s_text, $i_timeout, $h_wnd)
  466.  
  467.     If $__hMsgBoxHook Then _WinAPI_UnhookWindowsHookEx($__hMsgBoxHook)
  468.     DllCallbackFree($h_cb)
  469.     $__hMsgBoxHook = 0
  470.  
  471.     ; Reset Globals
  472.     _MsgBox_SetButtonText(-1)
  473.     _MsgBox_SetWindowPos()
  474.  
  475.     Return $i_ret
  476. EndFunc   ;==>_MsgBox
  477.  
  478. ;===================================================================================================
  479. ;
  480. ; Function Name....:    _MsgBox_SetWindowPos()
  481. ; Description......:    Move the window to a specific position or center to a window
  482. ; Parameter(s).....:
  483. ;                       $i_pos_x: ( Optional -1 or Default ) X position of messagebox
  484. ;                                   ( reverts to default (center) if $h_parent param is used )
  485. ;                       $i_pos_y: ( Optional -1 or Default ) Y position of messagebox
  486. ;                                   ( reverts to default (center) if $h_parent param is used )
  487. ;                       $h_parent: ( Optional -1 or Default ) Window to center messagbox on
  488. ; Return Value(s)..:
  489. ;                       Success...: N/A
  490. ;                       Failure...: N/A
  491. ;                       Error.....: N/A
  492. ;                       Extended..: N/A
  493. ; Requirement(s)...:
  494. ; Author(s)........:    SmOke_N (Ron Nielsen)
  495. ; Modified.........:
  496. ; Comment(s).......:    This must be called before _MsgBox(); Tested with AutoIt 3.3.6.1
  497. ; Example(s).......:    _MsgBox_SetWindowPos(100, 200)
  498. ;
  499. ;===================================================================================================
  500.  
  501. Func _MsgBox_SetWindowPos($i_pos_x = -1, $i_pos_y = -1, $h_parent = -1)
  502.  
  503.     If $h_parent = Default Or $h_parent = -1 Then $h_parent = 0
  504.     If $i_pos_x = Default Or $h_parent Then $i_pos_x = -1
  505.     If $i_pos_y = Default Or $h_parent Then $i_pos_y = -1
  506.  
  507.     $__iMsgBox_XPos = $i_pos_x
  508.     $__iMsgBox_YPos = $i_pos_y
  509.     $__hMsgBox_Parent = $h_parent
  510.  
  511.     If $i_pos_x = -1 And $i_pos_y = -1 And $h_parent = 0 Then
  512.         $__fMsgBox_Move = False
  513.         Return True
  514.     EndIf
  515.  
  516.     $__fMsgBox_Move = True
  517.     Return True
  518. EndFunc   ;==>_MsgBox_SetWindowPos
  519.  
  520.  
  521. ;===================================================================================================
  522. ;
  523. ; Function Name....:    _MsgBox_SetButtonText()
  524. ; Description......:    Change the text of buttons
  525. ; Parameter(s).....:
  526. ;                       $i_flag:  Flag for button(s) ( See helpfile )
  527. ;                       $s_button1:  ( Optional -1 or Default ) Text for first button
  528. ;                       $s_button2:  ( Optional -1 or Default ) Text for second button
  529. ;                       $s_button3:  ( Optional -1 or Default ) Text for third button
  530. ; Return Value(s)..:
  531. ;                       Success...: N/A
  532. ;                       Failure...: N/A
  533. ;                       Error.....: N/A
  534. ;                       Extended..: N/A
  535. ; Requirement(s)...:
  536. ; Author(s)........:    SmOke_N (Ron Nielsen)
  537. ; Modified.........:
  538. ; Comment(s).......:    This must be called before _MsgBox(); Tested with AutoIt 3.3.6.1
  539. ; Example(s).......:    _MsgBox_SetButtonText(4, "Button 1", "Button 2")
  540. ;
  541. ;===================================================================================================
  542.  
  543. Func _MsgBox_SetButtonText($i_flag, $s_button1 = -1, $s_button2 = -1, $s_button3 = -1)
  544.  
  545.     If ((IsInt($i_flag) And ($i_flag < 0 Or $i_flag > 6)) Or _
  546.             Not StringIsInt($i_flag) Or $i_flag = Default) Then
  547.         $__aMsgBoxText = $__aMsgBoxTextDefault
  548.         $__iMsgBox_Flag = -1
  549.         Return True
  550.     EndIf
  551.  
  552.     $__iMsgBox_Flag = Int($i_flag)
  553.  
  554.     Switch $__iMsgBox_Flag
  555.         Case 0
  556.             If (IsInt($s_button1) And $s_button1 = -1) Or $s_button1 = Default Then $s_button1 = "OK"
  557.             $__aMsgBoxText[$__iMsgBox_IDOK] = $s_button1
  558.         Case 1
  559.             If (IsInt($s_button1) And $s_button1 = -1) Or $s_button1 = Default Then $s_button1 = "OK"
  560.             If (IsInt($s_button2) And $s_button2 = -1) Or $s_button2 = Default Then $s_button2 = "Cancel"
  561.             $__aMsgBoxText[$__iMsgBox_IDOK] = $s_button1
  562.             $__aMsgBoxText[$__iMsgBox_IDCANCEL] = $s_button2
  563.         Case 2
  564.             If (IsInt($s_button1) And $s_button1 = -1) Or $s_button1 = Default Then $s_button1 = "&Abort"
  565.             If (IsInt($s_button2) And $s_button2 = -1) Or $s_button2 = Default Then $s_button2 = "&Retry"
  566.             If (IsInt($s_button3) And $s_button3 = -1) Or $s_button3 = Default Then $s_button3 = "&Ignore"
  567.             $__aMsgBoxText[$__iMsgBox_IDABORT] = $s_button1
  568.             $__aMsgBoxText[$__iMsgBox_IDRETRY] = $s_button2
  569.             $__aMsgBoxText[$__iMsgBox_IDIGNORE] = $s_button3
  570.         Case 3
  571.             If (IsInt($s_button1) And $s_button1 = -1) Or $s_button1 = Default Then $s_button1 = "Yes"
  572.             If (IsInt($s_button2) And $s_button2 = -1) Or $s_button2 = Default Then $s_button2 = "No"
  573.             If (IsInt($s_button3) And $s_button3 = -1) Or $s_button3 = Default Then $s_button3 = "Cancel"
  574.             $__aMsgBoxText[$__iMsgBox_IDYES] = $s_button1
  575.             $__aMsgBoxText[$__iMsgBox_IDNO] = $s_button2
  576.             $__aMsgBoxText[$__iMsgBox_IDCANCEL] = $s_button3
  577.         Case 4
  578.             If (IsInt($s_button1) And $s_button1 = -1) Or $s_button1 = Default Then $s_button1 = "Yes"
  579.             If (IsInt($s_button2) And $s_button2 = -1) Or $s_button2 = Default Then $s_button2 = "No"
  580.             $__aMsgBoxText[$__iMsgBox_IDYES] = $s_button1
  581.             $__aMsgBoxText[$__iMsgBox_IDNO] = $s_button2
  582.         Case 5
  583.             If (IsInt($s_button1) And $s_button1 = -1) Or $s_button1 = Default Then $s_button1 = "&Retry"
  584.             If (IsInt($s_button2) And $s_button2 = -1) Or $s_button2 = Default Then $s_button2 = "Cancel"
  585.             $__aMsgBoxText[$__iMsgBox_IDRETRY] = $s_button1
  586.             $__aMsgBoxText[$__iMsgBox_IDCANCEL] = $s_button2
  587.         Case 6
  588.             If (IsInt($s_button1) And $s_button1 = -1) Or $s_button1 = Default Then $s_button1 = "Cancel"
  589.             If (IsInt($s_button2) And $s_button2 = -1) Or $s_button2 = Default Then $s_button2 = "&Try Again"
  590.             If (IsInt($s_button3) And $s_button3 = -1) Or $s_button3 = Default Then $s_button3 = "&Continue"
  591.             $__aMsgBoxText[$__iMsgBox_IDCANCEL] = $s_button1
  592.             $__aMsgBoxText[$__iMsgBox_IDTRYAGAIN] = $s_button2
  593.             $__aMsgBoxText[$__iMsgBox_IDCONTINUE] = $s_button3
  594.     EndSwitch
  595.  
  596.     Return True
  597. EndFunc   ;==>_MsgBox_SetButtonText
  598.  
  599. ;===============================================================================
  600. ; Function Name:   _GUICreateLogin()
  601. ; Description:     Create a basic login box with Username, Password, and a prompt.
  602. ; Syntax:
  603. ; Parameter(s):    $hTitle - The title of the Form
  604. ;                  $hPrompt - The text prompt for the user [Optional] (maximum of 2 lines)
  605. ;                  $bBlank - The password or username can be blank. [optional]
  606. ;                            Default = False (Blanks are not allowed)
  607. ;                  $hWidth - Width of the form [optional] - default = 250
  608. ;                  $hHeight - Height of the form [optional] - default = 130
  609. ;                  $hLeft - X position [optional] - default = centered
  610. ;                  $hTop - Y position [optional] - default = centered
  611. ;                  $Timeout - The timeout of the form [optional - default = 0 (no timeout)
  612. ;                  $ShowError - Prompts are displayed to the user with timeout [optional]
  613. ; Requirement(s):  None
  614. ; Return Value(s): Success - Returns an array of 2 elements where
  615. ;                                        [0] = UserName
  616. ;                                        [1] = Password
  617. ;                  Failure - Sets @Error to 1 and
  618. ;                                 @Extended - 1 = Cancel/Exit Button Pressed
  619. ;                                           - 2 = Timed out
  620. ; Author(s):   Brett Francis ([email protected])
  621. ; Modification(s): GeoSoft
  622. ; Note(s): If $hPrompt is blank then the GUI height will be reduced by 30
  623. ; Example(s):
  624. ;===============================================================================
  625.  
  626. Func _GUICreateLogin($hTitle, $hPrompt = "", $bBlank = False, $hWidth = -1, $hHeight = -1, $hLeft = -1, $hTop = -1, $timeout = 0, $ShowError = 0)
  627.     If Not $hTitle Then $hTitle = "Login"
  628.     Local $Msg, $Status, $err, $ext, $iGCM = Opt("GUICoordMode", 2);; Get the current value of GUICoordMode and set it to 2
  629.     If StringRegExp($bBlank, "(?i)\s|default|-1") Then $bBlank = False
  630.     If StringRegExp($hWidth, "(?i)\s|default|-1") Then $hWidth = 250
  631.     If StringRegExp($hHeight, "(?i)\s|default|-1") Then $hHeight = 130
  632.     If StringRegExp($hLeft, "(?i)\s|default|-1") Then $hLeft = (@DesktopWidth / 2) - ($hWidth / 2)
  633.     If StringRegExp($hTop, "(?i)\s|default|-1") Then $hTop = (@DesktopHeight / 2) - ($hHeight / 2)
  634.     If Not $hPrompt Then $hHeight -= 30;If $hPrompt is blank then resize the GUI.
  635.     Local $retarr[2] = ["", ""], $Time = 0
  636.  
  637.     Local $gui = GUICreate($hTitle, $hWidth, $hHeight, $hLeft, $hTop)
  638.     GUISetCoord(4, 0)
  639.     If $hPrompt Then Local $Lbl_Prompt = GUICtrlCreateLabel($hPrompt, -1, 4, 201, 30)
  640.     Local $Lbl_User = GUICtrlCreateLabel("Username:", -1, 4, 64, 17);44, 64, 17)
  641.     GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
  642.     Local $username = GUICtrlCreateInput('', 8, -1, $hWidth - 81, 21)
  643.     GUICtrlCreateLabel("Password:", -($hWidth - 8), 4, 65, 17)
  644.     GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
  645.     Local $password = GUICtrlCreateInput('', 8, -1, $hWidth - 81, 21, 32);, $ES_PASSWORD)
  646.     GUISetCoord(($hWidth / 2) - 85, $hHeight - 34)
  647.     Local $Btn_OK = GUICtrlCreateButton("&OK", -1, -1, 75, 25)
  648.     Local $Btn_Cancel = GUICtrlCreateButton("&Cancel", 20, -1, 75, 25)
  649.     GUICtrlSetState($Btn_OK, 512)
  650.     GUISetState()
  651.     If $timeout Then $Time = TimerInit()
  652.     While 1
  653.         $Msg = GUIGetMsg()
  654.         Local $sUser = GUICtrlRead($username)
  655.         Local $sPass = GUICtrlRead($password)
  656.         If ($Time And TimerDiff($Time) >= $timeout) And ($sUser = "" And $sPass = "") Then
  657.             $Status = 2
  658.             ExitLoop
  659.         EndIf
  660.         Select
  661.             Case $Msg = -3
  662.                 $Status = 0
  663.                 ExitLoop
  664.             Case $Msg = $Btn_OK
  665.                 If $bBlank And ($sUser = "" Or $sPass = "") Then
  666.                     $Status = 1
  667.                     ExitLoop
  668.                 Else
  669.                     If $sUser <> "" And $sPass <> "" Then
  670.                         $Status = 1
  671.                         ExitLoop
  672.                     Else
  673.                         Select
  674.                             Case $sUser = "" And $sPass = ""
  675.                                 If $ShowError = 1 Then MsgBox(16, "Error!", "Username and Password cannot be blank!")
  676.                             Case $sPass = ""
  677.                                 If $ShowError = 1 Then MsgBox(16, "Error!", "Password cannot be blank!")
  678.                                 GUICtrlSetState($password, 256)
  679.                             Case $sUser = ""
  680.                                 If $ShowError = 1 Then MsgBox(16, "Error!", "Username cannot be blank!")
  681.                                 GUICtrlSetState($username, 256)
  682.                         EndSelect
  683.                     EndIf
  684.                 EndIf
  685.             Case $Msg = $Btn_Cancel
  686.                 $Status = 0
  687.                 ExitLoop
  688.             Case $timeout And TimerDiff($Time) >= $timeout; And $timeout
  689.                 If $bBlank And ($sUser = "" Or $sPass = "") Then
  690.                     $Status = 2
  691.                     ExitLoop
  692.                 Else
  693.                     ;$time = TimerInit()
  694.                     Select
  695.                         Case $sUser = "" And $sPass = ""
  696.                             If $timeout Then $Time = TimerInit()
  697.                             If $ShowError = 1 Then MsgBox(16, "Error!", "Username and Password cannot be blank!")
  698.                         Case $sPass = ""
  699.                             If $timeout Then $Time = TimerInit()
  700.                             If $ShowError = 1 Then
  701.                                 MsgBox(16, "Error!", "Password cannot be blank!")
  702.                                 GUICtrlSetState($password, 256)
  703.                             EndIf
  704.                         Case $sUser = ""
  705.                             $Time = TimerInit()
  706.                             If $ShowError = 1 Then
  707.                                 MsgBox(16, "Error!", "Username cannot be blank!")
  708.                                 GUICtrlSetState($username, 256)
  709.                             EndIf
  710.                             ;Case ($Timeout AND TimerDiff($time) >= $timeout) AND ($sUser = "" OR $sPass = "")
  711.                             ;$status = 2
  712.                             ;ExitLoop
  713.                         Case Else
  714.                             If $sUser <> "" And $sPass <> "" Then
  715.                                 $Status = 3
  716.                                 ;If $Timeout AND TimerDiff($time) >= $timeout Then $Status = 2
  717.                                 ExitLoop
  718.                             EndIf
  719.                     EndSelect
  720.                 EndIf
  721.         EndSelect
  722.     WEnd
  723.     Local $eMsg = ""
  724.     Switch $Status
  725.         Case 0, 2
  726.             $err = 1;0
  727.             $ext = 1;Cancel/Exit Button Pressed
  728.             $eMsg = "Cancel/Exit Button Pressed"
  729.             If $Status = 2 Then $ext = 2;Timed Out
  730.             If $ext = 2 Then $eMsg = "Timed Out"
  731.         Case Else;1, 3
  732.             $retarr[0] = $sUser
  733.             $retarr[1] = $sPass
  734.             $err = 0;1
  735.             $ext = 0
  736.             If $Status = 3 Then $ext = 1;Username Fields Not Blank, Timeout reached
  737.     EndSwitch
  738.     GUIDelete($gui)
  739.     Opt("GUICoordMode", $iGCM);Reset the GUICoordMode to what it started as.
  740.     If $eMsg Then Return SetError($err, $ext, $eMsg)
  741.     Return $retarr
  742. EndFunc   ;==>_GUICreateLogin
Add Comment
Please, Sign In to add comment