Jump to content

Function called from an include the main script does not continue


molitar
 Share

Recommended Posts

In main script I am using an include that has another function.  The function is called and runs fine but it stops inside the include.  It is a script by Firefox used to do a simple screen capture to clipboard.  The problem is the call works and I get the screen capture in the clipboard but it does not return to the main script to continue.  What am I doing wrong?  I know I could put it into one large script but I am trying to learn how to call functions from an include and keep the processes separate for easier coding and debugging.

main.au3

#include "C:\Users\4477724\Desktop\AutoIT\includes\SimpleCaptureTool.au3"

If HotKeySet("{HOME}", "Example") = 0 Then
    MsgBox($MB_SYSTEMMODAL, "", "Could not set the hotkey !")
    Exit 1
EndIf

ConsoleWrite("Global Var coordinates: " & $iX1 & ", " & $iY1 & ", " & $iX2 & ", " & $iY2)

SimpleCaptureTool

; By FireFox, 2014
; Version : 1.4

#include <WindowsConstants.au3>
#include <Misc.au3>
#include <WinAPI.au3>
#include <ScreenCapture.au3>
#include <ClipBoard.au3>
#include <ColorConstants.au3>
#include <Constants.au3>

; Settings
Global $_fShowMousePos = True, $_fShowRectSize = True, $_fHideDesktop = False, $_fHideTaskBar = False, $giGDIPRef

If HotKeySet("{HOME}", "Example") = 0 Then
    MsgBox($MB_SYSTEMMODAL, "", "Could not set the hotkey !")
    Exit 1
EndIf

HotKeySet("^+{3}", "Example") ;for Windows under a MacBook lacking of a PrintScreen key (yes... me)

While 1
    Sleep(10000) ;10 sec
WEnd

Func Example()
    Global $iX1 = 0, $iY1 = 0, $iX2 = 0, $iY2 = 0
    Local $hGUICapture = 0

    Mark_Rect($hGUICapture, $iX1, $iY1, $iX2, $iY2)

    Local $hBitmap = _ScreenCapture_CaptureWnd("", $hGUICapture, $iX1, $iY1, $iX2, $iY2, False)
    GUIDelete($hGUICapture)

    Local $fOpenCb = _ClipBoard_Open(0)
    If Not $fOpenCb Then
        MsgBox($MB_SYSTEMMODAL, "", "Could not open the clipboard !")
        Return False
    EndIf

    _ClipBoard_Empty()

    Local $hBitmap3 = _WinAPI_CopyImage($hBitmap, 0, 0, 0, BitOR($LR_COPYDELETEORG, $LR_COPYRETURNORG))
    _WinAPI_DeleteObject($hBitmap)

    _ClipBoard_SetDataEx($hBitmap3, $CF_BITMAP)
    _ClipBoard_Close()

    _WinAPI_DeleteObject($hBitmap3)
EndFunc   ;==>Example

Func Mark_Rect(ByRef $hGUICapture, ByRef $iX1, ByRef $iY1, ByRef $iX2, ByRef $iY2)
    Local $iX_Pos = 0, $iY_Pos = 0, $iTemp = 0, $iWidth = 0, $iHeight = 0
    Local $hMask_1 = 0

    Local $hWnd = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]")
    Local $aWgp = WinGetPos($hWnd)

    If $_fHideDesktop Then
        ControlHide($hWnd, "", "[CLASS:SysListView32; INSTANCE:1]")
    EndIf

    If $_fHideTaskBar Then
        WinSetState("[CLASS:Shell_TrayWnd]", "", @SW_HIDE)
        WinSetState("[CLASS:Button]", "", @SW_HIDE)
    EndIf

    Local $hBitmap = _ScreenCapture_Capture("", $aWgp[0], $aWgp[1], $aWgp[2], $aWgp[3])

    Local $aSize[2] = [$aWgp[2], $aWgp[3]]
    $aWgp = 0

    $hGUICapture = GUICreate("", $aSize[0], $aSize[1], 0, 0, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_APPWINDOW, $WS_EX_TOPMOST))
    GUISetCursor($IDC_CROSS, 1, $hGUICapture)

    If $giGDIPRef = 0 Then
        _GDIPlus_Startup()
    EndIf

    Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUICapture)

    Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    _WinAPI_DeleteObject($hBitmap)

    WinSetTrans($hGUICapture, "", 0)

    GUISetState(@SW_SHOWNOACTIVATE, $hGUICapture)

    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)

    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)

    If $giGDIPRef > 1 Then
        _GDIPlus_Shutdown()
    EndIf

    WinSetTrans($hGUICapture, "", 255)

    If $_fHideTaskBar Then
        WinSetState("[CLASS:Button]", "", @SW_SHOWNOACTIVATE)
        WinSetState("[CLASS:Shell_TrayWnd]", "", @SW_SHOWNOACTIVATE)
    EndIf

    If $_fHideDesktop Then
        ControlShow($hWnd, "", "[CLASS:SysListView32; INSTANCE:1]")
    EndIf

    Local $hGUIRect = GUICreate("", $aSize[0], $aSize[1], 0, 0, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    GUISetBkColor($COLOR_BLACK)
    GUISetCursor($IDC_CROSS, 1, $hGUIRect)

    _GUICreateInvRect($hGUIRect, $aSize, $hMask_1, 0, 0, 1, 1)

    WinSetTrans($hGUIRect, "", 75)
    GUISetState(@SW_SHOWNOACTIVATE, $hGUIRect)

    Local $hUserDLL = DllOpen("user32.dll")
    Local $aMgp = 0
    Local $fExitLoop = False

    ; Wait until mouse button pressed
    While Not _IsPressed("01", $hUserDLL) And Not $fExitLoop
        If $_fShowMousePos Then
            $aMgp = MouseGetPos()
            ToolTip("x: " & $aMgp[0] & ", y: " & $aMgp[1], _
                    $aMgp[0] + ($aMgp[0] > 100 ? -95 : 10), _
                    $aMgp[1] + ($aMgp[1] > 50 ? -35 : 10))
        EndIf

        Sleep(10)
        If _IsPressed("1B", $hUserDLL) Then $fExitLoop = True
    WEnd

    If $_fShowMousePos Then
        ToolTip("")
    EndIf

    ; Get first mouse position
    $aMgp = MouseGetPos()
    $iX1 = $aMgp[0]
    $iY1 = $aMgp[1]

    ; Draw rectangle while mouse button pressed
    While _IsPressed("01", $hUserDLL) And Not $fExitLoop
        $aMgp = MouseGetPos()

        ; Set in correct order if required
        If $aMgp[0] < $iX1 Then
            $iX_Pos = $aMgp[0]
            $iWidth = $iX1 - $aMgp[0]
        Else
            $iX_Pos = $iX1
            $iWidth = $aMgp[0] - $iX1
        EndIf
        If $aMgp[1] < $iY1 Then
            $iY_Pos = $aMgp[1]
            $iHeight = $iY1 - $aMgp[1]
        Else
            $iY_Pos = $iY1
            $iHeight = $aMgp[1] - $iY1
        EndIf

        _GUICreateInvRect($hGUIRect, $aSize, $hMask_1, $iX_Pos, $iY_Pos, $iWidth, $iHeight)

        If $_fShowRectSize Then
            ToolTip("w: " & Abs($aMgp[0] - $iX1) & ", h: " & Abs($aMgp[1] - $iY1), _
                    $aMgp[0] + ((($aMgp[0] > $aSize[0] - 100 Or ($aMgp[0] - $iX1 < 0 And $aMgp[1] - $iY1 < 0)) And $aMgp[0] > 100) ? -95 : 10), _
                    $aMgp[1] + ((($aMgp[1] > $aSize[1] - 40 Or ($aMgp[0] - $iX1 < 0 And $aMgp[1] - $iY1 < 0)) And $aMgp[1] > 40) ? -35 : 10))
        EndIf

        Sleep(10)
        If _IsPressed("1B", $hUserDLL) Then $fExitLoop = True
    WEnd

    If $_fShowRectSize Then
        ToolTip("")
    EndIf

    _WinAPI_DeleteObject($hMask_1)

    ; Get second mouse position
    $iX2 = $aMgp[0]
    $iY2 = $aMgp[1]

    ; Set in correct order if required
    If $iX2 < $iX1 Then
        $iTemp = $iX1
        $iX1 = $iX2
        $iX2 = $iTemp
    EndIf
    If $iY2 < $iY1 Then
        $iTemp = $iY1
        $iY1 = $iY2
        $iY2 = $iTemp
    EndIf

    GUIDelete($hGUIRect)
    DllClose($hUserDLL)
EndFunc   ;==>Mark_Rect

Func _GUICreateInvRect($hWnd, $aSize, ByRef $hMask_1, $iX, $iY, $iW, $iH)
    Local $hMask_2 = 0, $hMask_3 = 0, $hMask_4 = 0

    $hMask_1 = _WinAPI_CreateRectRgn(0, 0, $aSize[0], $iY)
    $hMask_2 = _WinAPI_CreateRectRgn(0, 0, $iX, $aSize[1])
    $hMask_3 = _WinAPI_CreateRectRgn($iX + $iW, 0, $aSize[0], $aSize[1])
    $hMask_4 = _WinAPI_CreateRectRgn(0, $iY + $iH, $aSize[0], $aSize[1])

    _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_2, 2)
    _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_3, 2)
    _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_4, 2)

    _WinAPI_DeleteObject($hMask_2)
    _WinAPI_DeleteObject($hMask_3)
    _WinAPI_DeleteObject($hMask_4)

    _WinAPI_SetWindowRgn($hWnd, $hMask_1, 1)
EndFunc   ;==>_GUICreateInvRect

 

 

Link to comment
Share on other sites

OK I commented them out and than added a global variable to the SimpleCaptureToool.au3 so I know when I have captured the screenshot.

include "C:\Users\4477724\Desktop\AutoIT\includes\SimpleCaptureTool.au3"

Global $capture = 0

While $capture = 0
    If HotKeySet("{HOME}", "Example") = 0 Then
        MsgBox($MB_SYSTEMMODAL, "", "Could not set the hotkey !")
        Exit 1
    EndIf

    If $capture = 1 Then
        ConsoleWrite("Global Var coordinates: " & $iX1 & ", " & $iY1 & ", " & $iX2 & ", " & $iY2);verify I got the coordinates
        $capture = 0
    EndIf
WEnd

Sometimes when I capture it will not let me capture again.  Sometimes I get 1, 2 or 3 captures than the process terminates.

Console Output:

Global Var coordinates: 2107, 231, 2394, 363
+>02:02:10 AutoIt3.exe ended.rc:0
+>02:02:10 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 8.337

As you can see it successfully captured one time the second time it finished the entire process exiting it.

Link to comment
Share on other sites

I feel like that we're missing something more that you're using, since I have no issues with running it and capturing many times.

Also I would make some changes to your While loop for the main:

#include "SimpleCaptureTool.au3"

Global $capture = 0

; We only need to set the hotkey once, unless it's unset somewhere
If HotKeySet("{HOME}", "Example") = 0 Then
    MsgBox($MB_SYSTEMMODAL, "", "Could not set the hotkey !")
    Exit 1
EndIf

While $capture = 0 ; You may want to change this to just While True, if you want an infinite loop
    Sleep(10) ; Small sleep to avoid 100% cpu core usage
    If $capture = 1 Then
        ConsoleWrite("Global Var coordinates: " & $iX1 & ", " & $iY1 & ", " & $iX2 & ", " & $iY2 & @CRLF) ;verify I got the coordinates
        $capture = 0
    EndIf
WEnd

 

Edited by mistersquirrle

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

11 hours ago, mistersquirrle said:

I feel like that we're missing something more that you're using, since I have no issues with running it and capturing many times.

Also I would make some changes to your While loop for the main:

#include "SimpleCaptureTool.au3"

Global $capture = 0

; We only need to set the hotkey once, unless it's unset somewhere
If HotKeySet("{HOME}", "Example") = 0 Then
    MsgBox($MB_SYSTEMMODAL, "", "Could not set the hotkey !")
    Exit 1
EndIf

While $capture = 0 ; You may want to change this to just While True, if you want an infinite loop
    Sleep(10) ; Small sleep to avoid 100% cpu core usage
    If $capture = 1 Then
        ConsoleWrite("Global Var coordinates: " & $iX1 & ", " & $iY1 & ", " & $iX2 & ", " & $iY2 & @CRLF) ;verify I got the coordinates
        $capture = 0
    EndIf
WEnd

 

Thanks I see what you mean with the sleep for CPU Usage that maybe what is causing it because I'm on a work laptop and Sophos already uses a lot of CPU resources. Also the initial while loop did change a bit but not by much now that I put it into a form with a button control.  I appreciate the tip.

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
 Share

  • Recently Browsing   0 members

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