#include<Constants.au3>
#include<EditConstants.au3>
#include<GUIConstantsEx.au3>
#include<SliderConstants.au3>
#include<StaticConstants.au3>
#include<WindowsConstants.au3>
#include <winapi.au3>
Global $leave = 0, $freeze = 0, $schutz = 0, $go = 0, $zf = 1, $stand = 0;$SRCCOPY = 0x00CC0020,

HotKeySet("!a", "freeze")
HotKeySet("!1", "minus")
HotKeySet("!2", "plus")
HotKeySet("!3", "stand")
Opt("GuiOnEventMode", 1)
$mainlupe = GUICreate("Andygo's Bildschirmlupe", 516, 274, -1, -1, BitOR($WS_POPUP,$WS_BORDER,$WS_SIZEBOX+$WS_CLIPCHILDREN), $WS_EX_TOPMOST);$WS_SIZEBOX)
GUISetOnEvent($GUI_EVENT_CLOSE, "leave")
$MyhWnd = WinGetHandle("Andygo's Bildschirmlupe")
$fastx = _WinAPI_GetSystemMetrics(78)
$fasty = _WinAPI_GetSystemMetrics(79)
$hGuiOpt = GUICreate("Lupe Setup", 292, 129, -1, -1, BitOR($WS_SYSMENU,$WS_POPUP,$WS_POPUPWINDOW,$WS_BORDER,$WS_CLIPSIBLINGS), -1, $MyhWnd)
$btnQuit = GUICtrlCreateButton("Lupe beenden", 127, 82, 145, 25, 0)
GUICtrlSetOnEvent(-1, "OptionsEvents")
$btnOK = GUICtrlCreateButton("OK", 20, 80, 87, 29, 0)
GUICtrlSetOnEvent(-1, "OptionsEvents")
$cbInvert = GUICtrlCreateCheckbox("Negative Farben", 20, 35, 100, 18)
$cbschutz = GUICtrlCreateCheckbox("Selbstschutz", 20, 56, 100, 18)
GUICtrlSetOnEvent(-1, "OptionsEvents")
$Label2 = GUICtrlCreateLabel("freeze:   ALT + a"&@CRLF&"fixieren:  ALT + 3"&@CRLF&"Zoom: ALT+2 / ALT+1", 150, 40, 120, 43)
$SliderZoom = GUICtrlCreateSlider(80, 20, 161, 19);, $TBS_AUTOTICKS)
GUICtrlSetLimit(-1, 10, 1)
GUICtrlSetData(-1, $zf)
GUICtrlSetCursor (-1, 13)
GUICtrlSetOnEvent(-1, "OptionsEvents")
$Label1 = GUICtrlCreateLabel("Zoom: x1", 20, 20, 50, 17)
GUISetState(@SW_DISABLE)
GUISetState(@SW_HIDE)
GUISwitch($MyhWnd)
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "SystemEvents")
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "SystemEvents")
GUISetOnEvent($GUI_EVENT_SECONDARYDOWN, "SystemEvents")
func stand()
	if $stand = 0 Then
		$stand = 1
	Else
		$stand = 0
	endif
endfunc
func minus()
	if $zf > 1 then $zf -= 1
	GUICtrlSetData($SliderZoom, $zf)
	GUICtrlSetData($Label1, "Zoom: x" & $zf)
EndFunc
func plus()
	if $zf < 10 then $zf += 1
	GUICtrlSetData($SliderZoom, $zf)
	GUICtrlSetData($Label1, "Zoom: x" & $zf)
EndFunc

Func WinToggle($hWin)
	If BitAND(WinGetState($hWin), 6) Then
		GUISetState(@SW_DISABLE, $hWin)
		GUISetState(@SW_HIDE, $hWin)
	Else
		GUISetState(@SW_ENABLE, $hWin)
		GUISetState(@SW_SHOW, $hWin)
	EndIf
EndFunc
Func SystemEvents()
	Switch @GUI_CtrlId
		Case $GUI_EVENT_CLOSE
			exit
		Case $GUI_EVENT_PRIMARYDOWN
			DllCall("user32.dll","int","SendMessage","hWnd", $MyhWnd,"int",$WM_NCLBUTTONDOWN,"int", $HTCAPTION,"int", 0)
		Case $GUI_EVENT_PRIMARYUP
			;
		Case $GUI_EVENT_SECONDARYDOWN
			If Not BitAnd(WinGetState($hGuiOpt), 2) Then
				$xys = MouseGetPos()
				WinMove($hGuiOpt, "", Min(Max($xys[0]-50, 0), $fastx-320), Min(Max($xys[1]-50, 0), $fasty-180))
				WinToggle($hGuiOpt)
			EndIf
	EndSwitch
EndFunc
Func OptionsEvents()
	Switch @GUI_CtrlId
		Case $btnOK
			WinToggle(@GUI_WinHandle)
		Case $btnQuit
			exit
		Case $cbInvert
			$go = BitXOR($go, 1)
		Case $cbschutz
			$schutz = BitXOR($schutz, 1)
		Case $SliderZoom
			$zf = GUICtrlRead($SliderZoom)
			GUICtrlSetData($Label1, "Zoom: x" & $zf)
	EndSwitch
EndFunc
$xy = MouseGetPos()
While Not $leave
    Sleep(10)
    if $freeze = 0 then MAG()
WEnd

func freeze()
	if $freeze = 0 then
		$freeze = 1
	Else
		$freeze = 0
	EndIf
Endfunc
Func Min($n1, $n2)
	If $n1 < $n2 Then Return $n1
	Return $n2
EndFunc
Func Max($n1, $n2)
	If $n1 > $n2 Then Return $n1
	Return $n2
EndFunc

Func MAG()
    $MyHDC = DLLCall("user32.dll","int","GetDC","hwnd",$MyhWnd)
    If @error Then Return
    $DeskHDC = DLLCall("user32.dll","int","GetDC","hwnd",0)
    If Not @error Then
	    $test = WinGetClientSize ( $MyhWnd)
		$test2 = WinGetPos($MyhWnd)
		if $stand = 0 Then $xy = MouseGetPos()
        If Not @error Then
            $srcW = $test[0] / $zf
	        $srcH = $test[1] / $zf
			$l = Min(Max($xy[0] - $test[0] / (2 * $zf), 0), $fastx-$srcW)
		    $t = Min(Max($xy[1] - $test[1] / (2 * $zf), 0), $fasty-$srcH)
			if $schutz = 1 then
			    if $xy[0] < $test2[0]-($srcW/2) or $xy[0] > $test2[0]+$test[0]+($srcW/2) or $xy[1] < $test2[1]-($srcH/2) or $xy[1] > $test2[1]+$test[1]+($srcH/2) then;bedingung lupe verboten
			        if $go = 0 then DLLCall("gdi32.dll","int","StretchBlt","int",$MyHDC[0],"int",0,"int",0,"int",$test[0],"int",$test[1],"int",$DeskHDC[0],"int",$l,"int",$t,"int",int($test[0]/$zf),"int",int($test[1]/$zf),"long",0x00CC0020);$SRCCOPY)
                    if $go = 1 then DLLCall("gdi32.dll","int","StretchBlt","int",$MyHDC[0],"int",0,"int",0,"int",$test[0],"int",$test[1],"int",$DeskHDC[0],"int",$l,"int",$t,"int",int($test[0]/$zf),"int",int($test[1]/$zf),"long",$NOTSRCCOPY);$SRCCOPY)
				endif
			Else
			    if $go = 0 then DLLCall("gdi32.dll","int","StretchBlt","int",$MyHDC[0],"int",0,"int",0,"int",$test[0],"int",$test[1],"int",$DeskHDC[0],"int",$l,"int",$t,"int",int($test[0]/$zf),"int",int($test[1]/$zf),"long",0x00CC0020);$SRCCOPY)
				if $go = 1 then DLLCall("gdi32.dll","int","StretchBlt","int",$MyHDC[0],"int",0,"int",0,"int",$test[0],"int",$test[1],"int",$DeskHDC[0],"int",$l,"int",$t,"int",int($test[0]/$zf),"int",int($test[1]/$zf),"long",$NOTSRCCOPY);$SRCCOPY)
			endif
		EndIf
        DLLCall("user32.dll","int","ReleaseDC","int",$DeskHDC[0],"hwnd",0)
    EndIf
    DLLCall("user32.dll","int","ReleaseDC","int",$MyHDC[0],"hwnd",$MyhWnd)
EndFunc

Func leave()
    $leave = 1
EndFunc