Ticket #1040: _ScreenCapture_Capture.au3

File _ScreenCapture_Capture.au3, 4.4 KB (added by rover, 15 years ago)

_ScreenCapture_Capture() UDF with added error handling

Line 
1; #FUNCTION# ====================================================================================================================
2; Name...........: _ScreenCapture_Capture
3; Description ...: Captures a region of the screen
4; Syntax.........: _ScreenCapture_Capture([$sFileName = ""[, $iLeft = 0[, $iTop = 0[, $iRight = -1[, $iBottom = -1[, $fCursor = True]]]]]])
5; Parameters ....: $sFileName   - Full path and extension of the image file
6;                  $iLeft       - X coordinate of the upper left corner of the rectangle
7;                  $iTop        - Y coordinate of the upper left corner of the rectangle
8;                  $iRight      - X coordinate of the lower right corner of the rectangle.  If this is  -1,  the  current  screen
9;                  +width will be used.
10;                  $iBottom     - Y coordinate of the lower right corner of the rectangle.  If this is  -1,  the  current  screen
11;                  +height will be used.
12;                  $fCursor     - If True the cursor will be captured with the image
13; Return values .: Success - HBITMAP handle or NONE (See remarks). @error set to 0
14;                  Failure - Returns 0 and @error set to error location.
15;                  @error  - -1, -2, 1-7 capture failure location
16; Author ........: Paul Campbell (PaulIA)
17; Modified.......: rover
18; Remarks .......: If FileName is not blank this function will capture the screen and save it to file. If FileName is blank, this
19;                  function will capture the screen and return a HBITMAP handle to the bitmap image.  In this case, after you are
20;                  finished with the bitmap you must call _WinAPI_DeleteObject to delete the bitmap handle.
21;+
22;                  Requires GDI+: GDI+ requires a redistributable for applications  that
23;                  run on the Microsoft Windows NT 4.0 SP6, Windows 2000, Windows 98, and Windows Me operating systems.
24; Related .......: _WinAPI_DeleteObject, _ScreenCapture_SaveImage
25; Link ..........:
26; Example .......: Yes
27; ===============================================================================================================================
28Func _ScreenCapture_Capture($sFileName = "", $iLeft = 0, $iTop = 0, $iRight = -1, $iBottom = -1, $fCursor = True)
29        Local $iH, $iW, $hWnd, $hDDC, $hCDC, $hBMP, $aCursor, $aIcon, $hIcon, $iSCError = 0
30
31        If $iRight = -1 Then $iRight = _WinAPI_GetSystemMetrics($__SCREENCAPTURECONSTANT_SM_CXSCREEN)
32        If $iBottom = -1 Then $iBottom = _WinAPI_GetSystemMetrics($__SCREENCAPTURECONSTANT_SM_CYSCREEN)
33        If $iRight < $iLeft Then Return SetError(-1, 0, 0)
34        If $iBottom < $iTop Then Return SetError(-2, 0, 0)
35
36        $iW = $iRight - $iLeft
37        $iH = $iBottom - $iTop
38        $hWnd = _WinAPI_GetDesktopWindow()
39        If @error Then Return SetError(1, 0, 0)
40        $hDDC = _WinAPI_GetDC($hWnd)
41        If @error Then Return SetError(2, 0, 0)
42        $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
43        If @error Then
44                _WinAPI_ReleaseDC($hWnd, $hDDC)
45                Return SetError(3, 0, 0)
46        EndIf
47        $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iW, $iH)
48        If @error Or $hBMP = 0 Then
49                _WinAPI_ReleaseDC($hWnd, $hDDC)
50                _WinAPI_DeleteDC($hCDC)
51                Return SetError(4, 0, 0)
52        EndIf
53        _WinAPI_SelectObject($hCDC, $hBMP)
54        If @error Then
55                _WinAPI_ReleaseDC($hWnd, $hDDC)
56                _WinAPI_DeleteDC($hCDC)
57                _WinAPI_DeleteObject($hBMP)
58                Return SetError(5, 0, 0)
59        EndIf
60        _WinAPI_BitBlt($hCDC, 0, 0, $iW, $iH, $hDDC, $iLeft, $iTop, $__SCREENCAPTURECONSTANT_SRCCOPY)
61        If @error Then
62                _WinAPI_ReleaseDC($hWnd, $hDDC)
63                _WinAPI_DeleteDC($hCDC)
64                _WinAPI_DeleteObject($hBMP)
65                Return SetError(6, 0, 0)
66        EndIf
67
68        If $fCursor Then
69                $aCursor = _WinAPI_GetCursorInfo()
70                If Not @error And $aCursor[1] = True Then
71                        $hIcon = _WinAPI_CopyIcon($aCursor[2])
72                        If Not @error And $hIcon <> 0 Then
73                                $aIcon = _WinAPI_GetIconInfo($hIcon)
74                                If Not @error And $aIcon[0] = True Then
75                                        _WinAPI_DrawIcon($hCDC, $aCursor[3] - $aIcon[2] - $iLeft, $aCursor[4] - $aIcon[3] - $iTop, $hIcon)
76                                        If $aIcon[4] <> 0 Then _WinAPI_DeleteObject($aIcon[4])
77                                        If $aIcon[5] <> 0 Then _WinAPI_DeleteObject($aIcon[5])
78                                EndIf
79                                _WinAPI_DestroyIcon($hIcon)
80                        EndIf
81                EndIf
82        EndIf
83
84        _WinAPI_ReleaseDC($hWnd, $hDDC)
85        _WinAPI_DeleteDC($hCDC)
86        If $sFileName = "" Then Return SetError(0, 0, $hBMP)
87
88        _ScreenCapture_SaveImage($sFileName, $hBMP)
89        If @error Then $iSCError = 7
90        _WinAPI_DeleteObject($hBMP)
91        SetError($iSCError, 0)
92EndFunc   ;==>_ScreenCapture_Capture