Jump to content

UDF: _RegionSelector.au3


Carlo84
 Share

Recommended Posts

Hey all, i needed something to let the user select a region on the screen, and then record it with GDI just had 2 problems, couldnt find no region selector around and how to get the actual Frame-rate, cause obviously Sleep function is as inaccurate as hell :-p
So i created this lil UDF that does both.
Credits to guinness for his help getting the close option to be gone >[Tread]

Functions:

_RegionselectorCalcFPS


_RegionselectorSetState
_RegionselectorGetPos
_RegionselectorOff
_RegionselectorOn



Example:

#include "_RegionSelector.au3"
_Example()
Func _Example()
    Local $iCapturedFrames = 0, $i, $iPausedTime = 0
    Local $aRegionSelector = _RegionSelectorOn()
    Sleep(500)
    Local $iStartTime = _RegionSelectorSetState($aRegionSelector, 2)

    ;Do some recording.
    For $i = 1 To 50
        $iCapturedFrames += 1
        Sleep(25)
    Next

    ;Pause.
    For $i = 0 To 10
        $iPausedTime += _RegionSelectorSetState($aRegionSelector, 3, 400)
    Next

    ;Record some more.
    For $i = 1 To 50
        $iCapturedFrames += 1
        Sleep(25)
    Next

    ;Stop recording.
    Local $iEndTime = _RegionSelectorSetState($aRegionSelector, 1, $iStartTime)
    _RegionSelectorOff($aRegionSelector, 1)

    MsgBox(64, '_RegionSelector.au3', _
            'Total time passed: ' & Round($iEndTime / 1000, 2) & 'seconds' & @LF & _
            'Frames recorded: ' & $iCapturedFrames & @LF & _
            'Frame delay: ' & _RegionSelectorCalcFPS($iEndTime, $iCapturedFrames, $iPausedTime, 2) & ' ms (for GIFs and such)' & @LF & _
            'FPS: ' & _RegionSelectorCalcFPS($iEndTime, $iCapturedFrames, $iPausedTime, 1, 2) & ' fps')
EndFunc   ;==>_Example



UDF:

#include-once
#Region Includes
#include <GUIMenu.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#EndRegion Includes

#Region _RegionSelectorCalcFPS
; #FUNCTION# ;===============================================================================
;
; Name...........: _RegionSelectorCalcFPS
; Description ...: Sets the state/mode of a Region selector window.
; Syntax.........: _RegionSelectorCalcFPS($iRecTime, $iFrames [, $iPauseTime = 0 [, $iFlag = 1 [, $iDecimals = 0]]] )
;
; Parameters ....: $iRecTime - Total time past between recording start and recording end.
;                  $iFrames - Total number of frames recorded.
;                  $iPauseTime - [optional] Total idle time between recording start and recording end.
;                  $iFlag - [optional] specifies whether to return fps (frames per second) or frame delay (in miliseconds)
;                   | 1 = fps
;                   | 2 = frame delay
;                  $iDecimals - [optional] Number indicating how many places to the right of the decimal are included in the rounding.
;
; Return values .: Success - Returns recorded speed depending on Flag
;                   | when $iFlag is 1: Returns overal fps (frames per second) of recording
;                   | when $iFlag is 2: Returns the overal delay between frames in miliseconds
;
; Author ........: Carlo Westmaas
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........; http://www.autoitscript.com/forum/topic/128399-udf-regionselectorau3/
; Example .......; Yes
;
; ;==========================================================================================
Func _RegionSelectorCalcFPS($iRecTime, $iFrames, $iPauseTime = 0, $iFlag = 1, $iDecimals = 0)
    If $iFlag = 1 Then Return Round(1000 / (($iRecTime - $iPauseTime) / ($iFrames)), $iDecimals)
    Return Round(($iRecTime - $iPauseTime) / $iFrames, $iDecimals)
EndFunc   ;==>_RegionSelectorCalcFPS
#EndRegion _RegionSelectorCalcFPS

#Region _RegionSelectorSetState
; #FUNCTION# ;===============================================================================
;
; Name...........: _RegionSelectorSetState
; Description ...: Sets the state/mode of a Region selector window.
; Syntax.........: _RegionSelectorSetState($aSelector [, $iState = 1 [, $iTime = 400]] )
;
; Parameters ....: $aSelector - A array with info about the Selector window as returned by _RegionSelectorOn()
;                  $iState - the state/mode to change the window to.
;                   | 1 = (Default) Normal mode, Border is green and window can be dragged around.
;                   | 2 = Recording mode, Border is red and user can interact with the region below.
;                   | 3 = Paused mode, border blinks.
;                  $iTime - Depending on $iState
;                   | when $iState is 1: A timestamp as returned by $iState 1 or TimerInit()
;                   | when $iState is 3: Time to pause script
;
; Return values .: Success - Depending on $iState.
;                   | when $iState is 1: Returns the time difference (in milliseconds) from a previous call to $iState 2 or TimerInit().
;                   | when $iState is 2: Returns a timestamp number (in milliseconds).
;                   | when $iState is 3: Time diffrence between Pause start and pause end.
;
; Author ........: Carlo Westmaas
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........; http://www.autoitscript.com/forum/topic/128399-udf-regionselectorau3/
; Example .......; Yes
;
; ;==========================================================================================
Func _RegionSelectorSetState($aSelector, $iState = 1, $iTime = 400)
    If Not IsArray($aSelector) Then Return SetError(1, 0, 0)
    If Not IsHWnd($aSelector[0]) Then Return SetError(2, 0, 0)
    Local $iTop = BitAND(_WinAPI_GetWindowLong($aSelector[0], $GWL_EXSTYLE), $WS_EX_TOPMOST)
    Select
        Case $iState = 1 ; Normal
            $iTime = TimerDiff($iTime)
            _WinAPI_SetWindowLong($aSelector[0], $GWL_EXSTYLE, $WS_EX_LAYERED)
            If $iTop Then WinSetOnTop($aSelector[0], '', 1)
            GUISetBkColor(0x41A317, $aSelector[0])
            WinSetTitle($aSelector[0], '', 'Selector')
            Return $iTime
        Case $iState = 2 ; Recording
            _WinAPI_SetWindowLong($aSelector[0], $GWL_EXSTYLE, BitOR($WS_EX_LAYERED, $WS_EX_TRANSPARENT))
            If $iTop Then WinSetOnTop($aSelector[0], '', 1)
            GUISetBkColor(0xF62817, $aSelector[0])
            WinSetTitle($aSelector[0], '', 'Recording')
            Return TimerInit()
        Case $iState = 3 ; Paused
            Local $timer = TimerInit()
            WinSetTitle($aSelector[0], '', 'Paused')
            GUISetBkColor(0xF76541, $aSelector[0])
            Sleep($iTime / 2)
            GUISetBkColor(0xF62817, $aSelector[0])
            Sleep($iTime / 2)
            WinSetTitle($aSelector[0], '', 'Recording')
            Return TimerDiff($timer)
    EndSelect
EndFunc   ;==>_RegionSelectorSetState
#EndRegion _RegionSelectorSetState

#Region _RegionSelectorGetPos
; #FUNCTION# ;===============================================================================
;
; Name...........: _RegionSelectorGetPos
; Description ...: Retrieves the position and size of the Selector window or region.
; Syntax.........: _RegionSelectorGetPos($aSelector [, $iFlag = 0] )
;
; Parameters ....: $aSelector - A array with info about the Selector window as returned by _RegionSelectorOn()
;                  $iFlag - specifies whether to return Window position or Region position.
;                   | 0 = (Default) return Region position.
;                   | 1 = return Window position
;
; Return values .: Success - Returns a 4-element array containing the following information:
;                  | $array[0] = X position
;                  | $array[1] = Y position
;                  | $array[2] = Width
;                  | $array[3] = Height
;
; Author ........: Carlo Westmaas
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........; http://www.autoitscript.com/forum/topic/128399-udf-regionselectorau3/
; Example .......; No
;
; ;==========================================================================================
Func _RegionSelectorGetPos($aSelector, $iFlag = 0)
    If Not IsArray($aSelector) And UBound($aSelector) < 2 Then Return SetError(1, 0, 0)
    If Not IsHWnd($aSelector[0]) Then Return SetError(2, 0, 0)
    Local $iState = BitAND(WinGetState($aSelector[0]), 16)
    Select
        Case $iFlag = 0
            If Not IsHWnd($aSelector[1]) Then Return SetError(2, 0, 0)
            If @error Then Return SetError(3, 0, 0)
            If $iState Then WinSetState($aSelector[0], '', @SW_RESTORE)
            Local $aReturn = WinGetPos($aSelector[1])
            If $iState Then WinSetState($aSelector[0], '', @SW_MINIMIZE)
            Return $aReturn
        Case $iFlag = 1
            If $iState Then WinSetState($aSelector[0], '', @SW_RESTORE)
            Local $aReturn = WinGetPos($aSelector[0])
            If $iState Then WinSetState($aSelector[0], '', @SW_MINIMIZE)
    EndSelect
EndFunc   ;==>_RegionSelectorGetPos
#EndRegion _RegionSelectorGetPos

#Region _RegionSelectorOff
; #FUNCTION# ;===============================================================================
;
; Name...........: _RegionSelectorOff
; Description ...: Turns Selector window off.
; Syntax.........: _RegionSelectorOff($aSelector [, $iGuiCloseOnESC = 0] )
;
; Parameters ....: $aSelector - A array with info about the Selector window as returned by _RegionSelectorOn()
;                  $iGuiCloseOnESC - [optional] if set to 1 the GuiCloseOnESC option will be restored as how it was before the selector was created.
;
; Return values .: Success - Returns a 4-element array containing the following information:
;                  | $array[0] = X position of window
;                  | $array[1] = Y position of window
;                  | $array[2] = Width of window
;                  | $array[3] = Height of window
;
; Author ........: Carlo Westmaas
; Modified.......:
; Remarks .......: note the positions are of the window not the region.
; Related .......: _RegionSelectorOn
; Link ..........; http://www.autoitscript.com/forum/topic/128399-udf-regionselectorau3/
; Example .......; Yes
;
; ;==========================================================================================
Func _RegionSelectorOff($aSelector, $iGuiCloseOnESC = 0)
    If Not IsArray($aSelector) And UBound($aSelector) < 3 Then Return SetError(1, 0, 0)
    If Not IsHWnd($aSelector[0]) Then Return SetError(2, 0, 0)
    If BitAND(WinGetState($aSelector[0]), 16) Then WinSetState($aSelector[0], '', @SW_RESTORE)
    Local $aReturn = WinGetPos($aSelector[0])
    GUIDelete($aSelector[0])
    If $iGuiCloseOnESC Then Opt('GUICloseOnESC', $aSelector[2])
    Return ($aReturn)
EndFunc   ;==>_RegionSelectorOff
#EndRegion _RegionSelectorOff

#Region _RegionSelectorOn
; #FUNCTION# ;===============================================================================
;
; Name...........: _RegionSelectorOn
; Description ...: Creates a region selector window.
; Syntax.........: _RegionSelectorOn([$iWidth = 640 [, $iHeight = 480 [, $iLeft = -1 [, $iTop = -1 [, $iBorder = 5 [, $iFlag = 1]]]]]] )
;
; Parameters ....: $iHeight - [optional] The width of the window.
;                  $iLeft - [optional] The left side of the dialog box. By default (-1), the window is centered. If defined, $iTop must also be defined.
;                  $iTop - [optional] The top of the dialog box. Default (-1) is centered
;                  $iBorder - [optional] Thickness of the selector's borders in pixels. default is 5
;                  $iFlag - [optional] Determines whether the window should have the "TOPMOST" flag set.
;                   | 0 = no on top flag
;                   | 1 = [default] set on top flag
;
; Return values .: Success - Returns a 3-element array containing the following information:
;                   | $array[0] = A windows handle of the created GUI.
;                   | $array[1] = A window handle of the Region control.
;                   | $array[2] = The old value of Opt("GUICloseOnESC", 0)
;
; Author ........: Carlo Westmaas
; Modified.......:
; Remarks .......: the Opt setting for GUICloseOnESC is changed in this function to prevent the window from receiving a $GUI_EVENT_CLOSE message.
; Related .......: _RegionSelectorOff
; Link ..........; http://www.autoitscript.com/forum/topic/128399-udf-regionselectorau3/
; Example .......; Yes
;
; ;==========================================================================================
Func _RegionSelectorOn($iWidth = 640, $iHeight = 480, $iLeft = -1, $iTop = -1, $iBorder = 5, $iFlag = 1)
    Local $aReturn[3]
    $aReturn[0] = GUICreate('Selector', $iWidth, $iHeight, $iLeft, $iTop, BitOR($WS_SIZEBOX, $WS_MINIMIZEBOX, $WS_POPUP), $WS_EX_LAYERED)
    Local $aSize = WinGetClientSize($aReturn[0])
    $aReturn[1] = GUICtrlCreateLabel('', $iBorder, $iBorder, $aSize[0] - ($iBorder * 2), $aSize[1] - ($iBorder * 2), -1, 0x00100000)
    $aReturn[1] = ControlGetHandle($aReturn[0], '', $aReturn[1])
    GUICtrlSetResizing(-1, 102)
    GUICtrlSetBkColor(-1, 0xF60D16)
    GUICtrlSetCursor(-1, 9)
    GUISetBkColor(0x41A317, $aReturn[0])
    WinSetOnTop($aReturn[0], '', $iFlag)
    _WinAPI_SetLayeredWindowAttributes($aReturn[0], 0xF60D16, 255)
    _GUICtrlMenu_EnableMenuItem(_GUICtrlMenu_GetSystemMenu($aReturn[0]), $SC_CLOSE, 1, False)
    $aReturn[2] = Opt('GUICloseOnESC', 0)
    GUISetState()
    Return $aReturn
EndFunc   ;==>_RegionSelectorOn
#EndRegion _RegionSelectorOn


I have been getting very good results with this, hope it will proof usefull to someone else. :-)

Edited by Carlo84
Link to comment
Share on other sites

Look interesting ! Posted Image

How do you display the "record" result ?

Thanks, i'm not sure i understand your question.

in the example function replace "$iCapturedFrames += 1" with your screencapturing function.

For my gif-recorder im just using GDI and count the output files with DirGetSize

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...