#include <WinAPI.au3>
#include <Constants.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>

; -Author: name22 (www.autoit.de)

Opt("GUIOnEventMode")

Global Const $iFlag = BitOR($SWP_NOSIZE, $SWP_NOZORDER), $tagMSLLHOOKSTRUCT = "INT X; INT Y;DWORD mouseData;DWORD flags;DWORD time;ULONG_PTR dwExtraInfo"
Global $hWnd_Magnifier, $hWnd_Desktop, $hDC_Window, $hDC_Desktop, $iDesktopWidth, $iDesktopHeight
Global $hDll_GDI32 = DllOpen("gdi32.dll"), $hDll_User32 = DllOpen("user32.dll")
Global $WM_EVENT = _WinAPI_RegisterWindowMessage("AutoItEvent")

Global $iGUIWidth = 100
Global $iGUIHeight = 100
Global $iCaptureWidth = 50
Global $iCaptureHeight = 50

$hWnd_Magnifier = GUICreate("AutoItMagnifyingGUI", $iGUIWidth, $iGUIHeight, -1, -1, $WS_POPUP, $WS_EX_TOPMOST)
GUISetState()

$hWnd_Desktop = _WinAPI_GetDesktopWindow()
$hDC_Window = _WinAPI_GetDC($hWnd_Magnifier)
$hDC_Desktop = _WinAPI_GetDC($hWnd_Desktop)
;~ DllCall($hDll_GDI32, "INT", "SetStretchBltMode", "HANDLE", $hDC_Window, "INT", 3)

$iDesktopWidth = _WinAPI_GetWindowWidth($hWnd_Desktop)
$iDesktopHeight = _WinAPI_GetWindowHeight($hWnd_Desktop)

GUIRegisterMsg($WM_EVENT, "_ReDraw")
OnAutoItExitRegister("_Shutdown")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
HotKeySet("{ESC}", "_Exit")

While Sleep(1000)
WEnd

Func _ReDraw($hWnd, $iMsg, $wParam, $lParam)
	Local Static $iX_Max = $iDesktopWidth - $iGUIWidth - $iCaptureWidth / 2, $iY_Max = $iDesktopHeight - $iGUIHeight - $iCaptureHeight / 2
	Local $iX_Mouse = $wParam
	Local $iY_Mouse = $lParam
	Local $iX_MouseTmp = $iX_Mouse
	Local $iY_MouseTmp = $iY_Mouse

	If $iX_MouseTmp < 0 Then $iX_MouseTmp = 0
	If $iX_MouseTmp > $iX_Max Then $iX_MouseTmp = $iX_Max
	If $iY_MouseTmp < 0 Then $iY_MouseTmp = 0
	If $iY_MouseTmp > $iY_Max Then $iY_MouseTmp = $iY_Max
	DllCall($hDll_User32, "BOOL", "SetWindowPos", "HWND", $hWnd_Magnifier, "HWND", 0, "INT", $iX_MouseTmp + $iCaptureWidth / 2 , "INT", $iY_MouseTmp + $iCaptureHeight / 2 , "INT", 0, "INT", 0, "UINT", $iFlag)
	DllCall($hDll_GDI32, "INT", "StretchBlt", "HWND", $hDC_Window, "INT", 0, "INT", 0, "INT", $iGUIWidth, "INT", $iGUIHeight, "HWND", $hDC_Desktop, "INT", $iX_Mouse - $iCaptureWidth / 2, "INT", $iY_Mouse - $iCaptureHeight / 2, "INT", $iCaptureWidth, "INT", $iCaptureHeight, "DWORD", $SRCCOPY)
EndFunc   ;==>_ReDraw

Func _Exit()
	Exit
EndFunc   ;==>_Exit

Func _Shutdown()
	_WinAPI_ReleaseDC($hWnd_Magnifier, $hDC_Window)
	_WinAPI_ReleaseDC($hWnd_Desktop, $hDC_Desktop)
	DllClose($hDll_GDI32)
	DllClose($hDll_User32)
EndFunc


; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_StretchBlt
; Description....: Copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap
;                  to fit the dimensions of the destination rectangle, if necessary.
; Syntax.........: _WinAPI_StretchBlt ( $hDestDC, $iXDest, $iYDest, $iWidthDest, $iHeightDest, $hSrcDC, $iXSrc, $iYSrc, $iWidthSrc, $iHeightSrc, $iRop )
; Parameters.....: $hDestDC     - Handle to the destination device context.
;                  $iXDest      - The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
;                  $iYDest      - The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
;                  $iWidthDest  - The width, in logical units, of the destination rectangle.
;                  $iHeightDest - The height, in logical units, of the destination rectangle.
;                  $hSrcDC      - Handle to the source device context.
;                  $iXSrc       - The x-coordinate, in logical units, of the upper-left corner of the source rectangle.
;                  $iYSrc       - The y-coordinate, in logical units, of the upper-left corner of the source rectangle.
;                  $iWidthSrc   - The width, in logical units, of the source rectangle.
;                  $iHeightSrc  - The height, in logical units, of the source rectangle.
;                  $iRop        - The raster-operation code. These codes define how the color data for the source rectangle is
;                                 to be combined with the color data for the destination rectangle to achieve the final color.
;                                 This parameter must be 0 or one of the following values.
;
;                                 $BLACKNESS
;                                 $CAPTUREBLT
;                                 $DSTINVERT
;                                 $MERGECOPY
;                                 $MERGEPAINT
;                                 $NOMIRRORBITMAP
;                                 $NOTSRCCOPY
;                                 $NOTSRCERASE
;                                 $PATCOPY
;                                 $PATINVERT
;                                 $PATPAINT
;                                 $SRCAND
;                                 $SRCCOPY
;                                 $SRCERASE
;                                 $SRCINVERT
;                                 $SRCPAINT
;                                 $WHITENESS
;
; Return values..: Success      - 1.
;                  Failure      - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: The system stretches or compresses the bitmap according to the stretching mode currently set in the
;                  destination device context.
; Related........:
; Link...........: @@MsdnLink@@ StretchBlt
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_StretchBlt($hDestDC, $iXDest, $iYDest, $iWidthDest, $iHeightDest, $hSrcDC, $iXSrc, $iYSrc, $iWidthSrc, $iHeightSrc, $iRop)

	Local $Ret  = DllCall('gdi32.dll', 'int', 'StretchBlt', 'hwnd', $hDestDC, 'int', $iXDest, 'int', $iYDest, 'int', $iWidthDest, 'int', $iHeightDest, 'hwnd', $hSrcDC, 'int', $iXSrc, 'int', $iYSrc, 'int', $iWidthSrc, 'int', $iHeightSrc, 'dword', $iRop)

	If (@error) Or (Not $Ret[0]) Then
		Return SetError(1, 0, 0)
	EndIf
	Return 1
EndFunc   ;==>_WinAPI_StretchBlt