#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <GUIConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <GUIConstants.au3>


HotKeySet ( "{SPACE}" ,"save" )
HotKeySet ( "{LEFT}" ,"move_left" )
HotKeySet ( "{RIGHT}" ,"move_right" )
HotKeySet ( "{UP}" ,"move_up" )
HotKeySet ( "{DOWN}" ,"move_down" )


Dim $coord[2], $oldCoord[2], $pict, $hBit, $hGraph, $side = 150, $zoom = 2, $desk = WinGetPos("Program Manager")


$hGUI = GUICreate("Зум и сохранение по рамке", 300, 320,@DesktopWidth - 400,100, -1)
WinSetOnTop ( "Зум и сохранение по рамке", "", 1 )
$otsx = GUICtrlCreateInput ("10",200,0,35,20)
GUICtrlCreateUpdown($otsx)
GUICtrlSetLimit (-1,$side-1,0)
$sinc = GUICtrlCreateButton("",235,0,20,20 )
$otsy = GUICtrlCreateInput ("10",255,0,35,20)
GUICtrlCreateUpdown($otsy)
GUICtrlSetLimit (-1,$side-1,0)
$name = GUICtrlCreateDummy()
$name = "test"
$rash = "bmp"
$help = GUICtrlCreateLabel ("?", 292,3)
GUICtrlSetTip($help, "Для того, чтобы сделать "&@CRLF&"снимок, нажмите пробел. "&@CRLF&"Для большей точности используйте "&@CRLF&"клавиши передвижения на клавиатуре. "&@CRLF&"Максимальный размер скриншота 50 px."&@CRLF&"© Степан")

$dir_obzor = GUICtrlCreateButton("Dir",0,0,20,18)
$dir = GUICtrlCreateInput (@ScriptDir& "\"&$name&"."&$rash,20,0,160,20)
$xuv = GUICtrlCreateButton("x2",180,0,20,20 )
GUISetState()
_GDIPlus_Startup()
While 1
    $msg = GUIGetMsg()
    $coord = MouseGetPos()
    If $coord[0] <> $oldCoord[0] Or $coord[1] <> $oldCoord[1]  Then
        $oldCoord = $coord
        _GDIPlus_GraphicsDispose($hGraph)
        _GDIPlus_ImageDispose($hBit)
        _WinAPI_DeleteObject($pict)
        $pict = _ScreenCapture_Capture("", $coord[0] - $side/$zoom, $coord[1] - $side/$zoom, $coord[0] + $side/$zoom, $coord[1] + $side/$zoom)
        $hBit = _GDIPlus_BitmapCreateFromHBITMAP($pict)
        $hGraph = _GDIPlus_GraphicsCreateFromHWND($hGUI)
        _GDIPlus_GraphicsDrawImageRectRect($hGraph, $hBit, 0, 0, $side, $side, 1, 20, $side*$zoom, $side*$zoom)
    EndIf
    $temp = (GUICtrlRead($otsx)/2)
     $temp2 = GUICtrlRead($otsy)/2
        if $side-$temp*2 and $side-$temp2*2 > -1  Then
            _GDIPlus_GraphicsDrawRect ($hGraph, $side-$temp*$zoom, $side-$temp2*$zoom+20, GUICtrlRead($otsx)*$zoom, GUICtrlRead($otsy)*$zoom)
        EndIf 
		
    If $msg = $GUI_EVENT_CLOSE  Then Exit
    If $msg = $dir_obzor Then
        $op_dir = FileSelectFolder("Обзор","",7,@ScriptDir)
        If @error = 0 Then 
            If StringLen($op_dir) > 3 Then
                GUICtrlSetData($dir,$op_dir&'\'& $name&"."&$rash)          
            Else
                GUICtrlSetData($dir,$op_dir& $name&"."&$rash)
            EndIf
        EndIf
    EndIf
    If $msg = $sinc Then
        $var = GUICtrlRead($sinc)
        If  $var = '' Then
            GUICtrlSetData($sinc,"X")
        EndIf
        If $var = 'X' Then
            GUICtrlSetData($sinc,"")
        EndIf
    EndIf
    $var = GUICtrlRead($sinc)
        If  $var = '' Then
            GUICtrlSetData($otsy,GUICtrlRead($otsy))
        EndIf
        If $var = 'X' Then
            GUICtrlSetData($otsy,GUICtrlRead($otsx))
        EndIf
        If  $msg = $xuv Then
            $xuv_temp =GUICtrlRead($xuv)
           
            If  $xuv_temp = 'x8' Then
                GUICtrlSetData($xuv,'x2')
                $zoom = 2
            EndIf      
            If  $xuv_temp = 'x6' Then
                GUICtrlSetData($xuv,'x8')
                $zoom = 8
            EndIf
            If  $xuv_temp = 'x4' Then
                GUICtrlSetData($xuv,'x6')
                $zoom = 6
            EndIf
            If  $xuv_temp = 'x2' Then
                GUICtrlSetData($xuv,'x4')
                $zoom = 4
            EndIf
        EndIf
    Sleep(50)
WEnd
_GDIPlus_Shutdown()

Func save()
    $otstup = GUICtrlRead($otsx)
    $otstup2 = GUICtrlRead($otsy)
    _ScreenCapture_Capture (GUICtrlRead($dir), $coord[0] - ($otstup/2), $coord[1] - ($otstup2/2), $coord[0] + ($otstup/2)-1, $coord[1] + ($otstup2/2)-1,False)
   
EndFunc

Func move_left()
    $x = MouseGetPos()
    MouseMove($x[0]-1,$x[1])
EndFunc
Func move_right()
    $x = MouseGetPos()
    MouseMove($x[0]+1,$x[1])
EndFunc
Func move_up()
    $x = MouseGetPos()
    MouseMove($x[0],$x[1]-1)
EndFunc

Func move_down()
    $x = MouseGetPos()
    MouseMove($x[0],$x[1]+1)
EndFunc
 