#AutoIt3Wrapper_Res_HiDpi=y #include #include #include #include ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Crop ; Description ...: A function that allows you to freely and visually select an area of the screen ; Syntax ........: _Crop([$i_X = Default[, $i_Y = Default[, $i_W = Default[, $i_H = Default[, $iMinW = -1[, $iMaxW = -1[, ; $iMinH = -1[, $iMaxH = -1]]]]]]]]) ; ; - To resize the area click the left mouse button and drag on any of the moving colored edges of the tool. ; You can also LeftClick within the selected area and drag to move the whole selection. ; ; - To terminate the selection operation RightClick within the selected area. ; ; You can alse terminate the selection operation by hitting the ESC key; ; in that case the @extended macro will be setted to true ; ; Parameters ....: All parameters are optional: ; $i_X - [optional] an integer value. Default is MouseGetPos(0) - 50. ; The upper left X coordinate where you want the cropping tool to appear ; ; $i_Y - [optional] an integer value. Default is MouseGetPos(1) - 50. ; The upper left Y coordinate where you want the cropping tool to appear ; ; $i_W - [optional] an integer value. Default is 100. ; The initial width of the selection area ; ; $i_H - [optional] an integer value. Default is 100. ; The initial height of the selection area ; ; $iMinW - [optional] an integer value. Default is 1 (minimum allowed is 1). ; The minimum selectable width ; ; $iMaxW - [optional] an integer value. Default is -1 (no limit). ; The maximum selectable width ; ; $iMinH - [optional] an integer value. Default is 1 (minimum allowed is 1). ; The minimum selectable height ; ; $iMaxH - [optional] an integer value. Default is -1 (no limit). ; The maximum selectable height ; ; Return values .: - A 4 element 1D array where: ; element [0] the upperLeft X coordinate of the selected area ; element [1] the upperLeft Y coordinate of the selected area ; element [2] the width of the selected area ; element [3] the height of the selected area ; ; @extended is set to True if you exit from the _crop() function by pressing the ESC key ; ; Author ........: Chimp (Gianni Addiego) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== ; Func _Crop($i_X = MouseGetPos(0) - 50, $i_Y = MouseGetPos(1) - 50, $i_W = 100, $i_H = 100, $iMinW = -1, $iMaxW = -1, $iMinH = -1, $iMaxH = -1) Func _Crop($i_X = Default, $i_Y = Default, $i_W = Default, $i_H = Default, $iMinW = 1, $iMaxW = -1, $iMinH = 1, $iMaxH = -1) _GDIPlus_Startup() If ($i_X = '') Or ($i_X = Default) Then $i_X = MouseGetPos(0) - 50 If ($i_Y = '') Or ($i_Y = Default) Then $i_Y = MouseGetPos(1) - 50 If ($i_W = '') Or ($i_W = Default) Then $i_W = 100 If ($i_H = '') Or ($i_H = Default) Then $i_H = 100 If ($iMinW < 1) Or ($iMinW = Default) Then $iMinW = 1 If ($iMaxW < 1) Or ($iMaxW = Default) Then $iMaxW = 65535 If ($iMinH < 1) Or ($iMinH = Default) Then $iMinH = 1 If ($iMaxH < 1) Or ($iMaxH = Default) Then $iMaxH = 65535 ; used local variables Local $hGUI2, $hGUI3, $aWinPos2, $aPrevPos2[4], $aClientSize Local $iX, $iY, $iW, $iH Local Enum $x, $y Local $iOuterTolerance, $iBorder, $iInnerTolerance Local $hGraphic, $hPen, $AlphaKey = 0xFF000000, $RectColor, $RectBgColor Local $hDLL = DllOpen("user32.dll") Local $fOffset, $iDelay, $iTimerID = TimerInit() Local $iCursorID, $iBorderID, $aMousePointer, $iSide, $iTopBot, $iInner, $iRightEdge, $iBottomEdge, $iMouseOffsetX, $iMouseOffsetY Local $bEscape ; ================================================================================== ; Set the appearance and behaviour of the tool ; ================================================================================== $iBorder = 2 ; the width of the colored border (minimum 1) ; since with a very thin border it is difficult to click on it to drag it ; it is possible to set an external courtesy area authorized to click on the border ; even if the mouse is not exactly on it (it can also be set to 0 to disable it) $iOuterTolerance = 4 ; 'courtesy' external area allowed to click on the border ; same as above but for the inside of the edge $iInnerTolerance = 3 ; 'courtesy' internal area allowed to click on the border $RectColor = 0xFFFF0000 ; The color of the colored moving edge $RectBgColor = 0xFFFFFF00 ; The Background color of the colored moving edge $iDelay = 50 ; this value determines the rotation speed of the moving colored border. The lower it is, the faster it is ; here the pattern of the moving edge can be customized ; Thanks to @UEZ for this snippet. See Here for reference: https://www.autoitscript.com/forum/topic/185769-moving-dash-rectangle/?do=findComment&comment=1343856 Local $iCount = 4, $tArray = DllStructCreate("float;float;float;float") DllStructSetData($tArray, 1, 2) ; dash length DllStructSetData($tArray, 2, 4) ; space length DllStructSetData($tArray, 3, 2) ; dash length DllStructSetData($tArray, 4, 4) ; space length ; Important!: also set the below variable as the sum of all the above dash/space values ; ========== Local $iDotsDashesTotLen = 2 + 4 + 2 + 4 ; <--- adapt this variable to the above values ; ================================================================================== ; Create a transparent window where to draw the moving colored edge effect using the $iBorder variable ; X and Y coordinates refers to where will be placed the client crop area (not the draggable outer colored border) $hGUI2 = GUICreate('', $i_W + $iBorder * 2, $i_H + $iBorder * 2, $i_X - $iBorder, $i_Y - $iBorder, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST)) ; make sure $AlphaKey color is different from both $RectColor and $RectBgColor While ($AlphaKey = $RectColor) Or ($AlphaKey = $RectBgColor) $AlphaKey += 1 WEnd GUISetBkColor($AlphaKey, $hGUI2) _WinAPI_SetLayeredWindowAttributes($hGUI2, $AlphaKey, 0, $LWA_COLORKEY) ; Create a transparent window wider than the main one to allow the custom mouse cursor to be displayed even beyond the colored border (Tolerance) ; also, since this window is not "passthrough", it prevents mouse clicks from passing underneath. $hGUI3 = GUICreate('', $i_W + $iBorder * 2 + $iOuterTolerance * 2, $i_H + $iBorder * 2 + $iOuterTolerance * 2, $i_X - $iBorder - $iOuterTolerance, $i_Y - $iBorder - $iOuterTolerance, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST)) WinSetTrans($hGUI3, "", 1) WinSetOnTop($hGUI3, "", $WINDOWS_ONTOP) GUISetState(@SW_SHOW, $hGUI2) GUISetState(@SW_SHOW, $hGUI3) $hPen = _GDIPlus_PenCreate($RectColor, $iBorder * 2) ; why * 2 ?? _GDIPlus_PenSetColor($hPen, $RectColor) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI2) Do $iSide = 0 $iTopBot = 0 $iInner = 0 $bEscape = False $aMousePointer = GUIGetCursorInfo($hGUI2) ; zero based $aWinPos2 = WinGetPos($hGUI2) $aClientSize = WinGetClientSize($hGUI2) ; ----------------------- ; check pointer position ; -------------------------------------------------------------------------------------------------------------------------------------------------------- If _ ; Pointer is on the left border (($aMousePointer[$x] >= -$iOuterTolerance) And ($aMousePointer[$x] < ($iBorder + $iInnerTolerance))) And _ (($aMousePointer[$y] >= -$iOuterTolerance) And ($aMousePointer[$y] < ($aClientSize[1] + $iOuterTolerance))) Then $iSide = 1 If _ ; Pointer is on the right border (($aMousePointer[$x] >= ($aClientSize[0] - $iBorder - $iInnerTolerance)) And ($aMousePointer[$x] < ($aClientSize[0] + $iOuterTolerance))) And _ (($aMousePointer[$y] >= -$iOuterTolerance) And ($aMousePointer[$y] < ($aClientSize[1] + $iOuterTolerance))) Then $iSide = 2 If _ ; Pointer is on the top border (($aMousePointer[$y] >= -$iOuterTolerance) And ($aMousePointer[$y] < ($iBorder + $iInnerTolerance))) And _ (($aMousePointer[$x] >= -$iOuterTolerance) And ($aMousePointer[$x] < ($aClientSize[0] + $iOuterTolerance))) Then $iTopBot = 3 If _ ; Pointer is on the bottom border (($aMousePointer[$y] >= ($aClientSize[1] - $iBorder - $iInnerTolerance)) And ($aMousePointer[$y] < $aClientSize[1] + $iOuterTolerance)) And _ (($aMousePointer[$x] >= -$iOuterTolerance) And ($aMousePointer[$x] < ($aClientSize[0] + $iOuterTolerance))) Then $iTopBot = 6 If _ ; Pointer is within the client area (($aMousePointer[$x] >= ($iBorder + $iInnerTolerance)) And $aMousePointer[$x] < ($aClientSize[0] - $iBorder - $iInnerTolerance)) And _ (($aMousePointer[$y] >= ($iBorder + $iInnerTolerance)) And $aMousePointer[$y] < ($aClientSize[1] - $iBorder - $iInnerTolerance)) Then $iInner = 9 ; -------------------------------------------------------------------------------------------------------------------------------------------------------- $iBorderID = $iSide + $iTopBot + $iInner ; ; SetCursor ; --------- $iCursorID = 0 Switch $iBorderID Case 0 $iCursorID = 2 ; ARROW Case 1, 2 ; 1 left ; 2 right $iCursorID = 13 ; SIZEWE Case 3, 6 ; 3 top ; 6 bottom $iCursorID = 11 ; SIZENS Case 5, 7 ; 5 TopRight ; 7 BottomLeft $iCursorID = 10 ; SIZENESW Case 4, 8 ; 4 TopLeft ; 8 BottomRight $iCursorID = 12 ; SIZENWSE Case 9 ; 9 Inside $iCursorID = 0 ; HAND - or SIZEALL (9) EndSwitch GUISetCursor($iCursorID, 1) ; ------------------------------------------------------- ; If LeftClick on the tool then Drag or redimm selector ; ------------------------------------------------------- If _IsPressed("1", $hDLL) And $iBorderID And (WinActive($hGUI2) Or WinActive($hGUI3)) Then ; Left clicked $aWinPos2 = WinGetPos($hGUI2) Switch $iBorderID Case 0 Case 1 ; Pointer is on the left border $iMouseOffsetX = $aWinPos2[0] - MouseGetPos(0) $iRightEdge = $aWinPos2[0] + WinGetClientSize($hGUI2)[0] Case 2 ; Pointer is on the right border $iMouseOffsetX = $aWinPos2[0] + $aWinPos2[2] - MouseGetPos(0) Case 3 ; Pointer is on the top border $iMouseOffsetY = $aWinPos2[1] - MouseGetPos(1) $iBottomEdge = $aWinPos2[1] + $aWinPos2[3] Case 4 ; Pointer is on the top-left corner $iMouseOffsetY = $aWinPos2[1] - MouseGetPos(1) $iMouseOffsetX = $aWinPos2[0] - MouseGetPos(0) $iRightEdge = $aWinPos2[0] + WinGetClientSize($hGUI2)[0] $iBottomEdge = $aWinPos2[1] + $aWinPos2[3] Case 5 ; Pointer is on the top-right corner $iMouseOffsetX = $aWinPos2[0] + $aWinPos2[2] - MouseGetPos(0) $iMouseOffsetY = $aWinPos2[1] - MouseGetPos(1) $iBottomEdge = $aWinPos2[1] + $aWinPos2[3] Case 6 ; Pointer is on the bottom border $iMouseOffsetY = $aWinPos2[1] + $aWinPos2[3] - MouseGetPos(1) Case 7 ; Pointer is on the bottom-left $iMouseOffsetY = $aWinPos2[1] + $aWinPos2[3] - MouseGetPos(1) $iMouseOffsetX = $aWinPos2[0] - MouseGetPos(0) $iRightEdge = $aWinPos2[0] + WinGetClientSize($hGUI2)[0] Case 8 ; Pointer is on the bottom-right $iMouseOffsetY = $aWinPos2[1] + $aWinPos2[3] - MouseGetPos(1) $iMouseOffsetX = $aWinPos2[0] + $aWinPos2[2] - MouseGetPos(0) Case 9 $iMouseOffsetY = $aWinPos2[1] - MouseGetPos(1) $iMouseOffsetX = $aWinPos2[0] - MouseGetPos(0) EndSwitch Do $iX = Default $iY = Default $iW = Default $iH = Default Switch $iBorderID Case 1 ; Pointer is on the left border $iX = MouseGetPos(0) + $iMouseOffsetX $iW = $iRightEdge - MouseGetPos(0) + -$iMouseOffsetX Case 2 ; Pointer is on the right border $iW = (MouseGetPos(0) - $aWinPos2[0]) + $iMouseOffsetX Case 3 ; Pointer is on the top border $iY = MouseGetPos(1) + $iMouseOffsetY $iH = $iBottomEdge - MouseGetPos(1) + -$iMouseOffsetY Case 4 ; Pointer is on the top-left corner $iX = MouseGetPos(0) + $iMouseOffsetX $iY = MouseGetPos(1) + $iMouseOffsetY $iW = $iRightEdge - MouseGetPos(0) + -$iMouseOffsetX $iH = $iBottomEdge - MouseGetPos(1) + -$iMouseOffsetY Case 5 ; Pointer is on the top-right corner $iY = MouseGetPos(1) + $iMouseOffsetY $iW = (MouseGetPos(0) - $aWinPos2[0]) + $iMouseOffsetX $iH = $iBottomEdge - MouseGetPos(1) + -$iMouseOffsetY Case 6 ; Pointer is on the bottom border $iH = (MouseGetPos(1) - $aWinPos2[1]) + $iMouseOffsetY Case 7 ; Pointer is on the bottom-left corner $iX = MouseGetPos(0) + $iMouseOffsetX $iW = $iRightEdge - MouseGetPos(0) + -$iMouseOffsetX $iH = (MouseGetPos(1) - $aWinPos2[1]) + $iMouseOffsetY Case 8 ; Pointer is on the bottom-right corner $iW = (MouseGetPos(0) - $aWinPos2[0]) + $iMouseOffsetX $iH = (MouseGetPos(1) - $aWinPos2[1]) + $iMouseOffsetY Case 9 ; Pointer is inside the client area $iX = MouseGetPos(0) + $iMouseOffsetX $iY = MouseGetPos(1) + $iMouseOffsetY EndSwitch If $iW > ($iMaxW + $iBorder * 2) Then $iW = $iMaxW + $iBorder * 2 $iX = Default EndIf If $iW < ($iMinW + $iBorder * 2) Then $iW = $iMinW + $iBorder * 2 $iX = Default EndIf If $iH > ($iMaxH + $iBorder * 2) Then $iH = $iMaxH + $iBorder * 2 $iY = Default EndIf If $iH < ($iMinH + $iBorder * 2) Then $iH = $iMinH + $iBorder * 2 $iY = Default EndIf WinMove($hGUI2, '', $iX, $iY, $iW, $iH) GUISetCursor($iCursorID, 1) $aWinPos2 = WinGetPos($hGUI2) $aClientSize = WinGetClientSize($hGUI2) ; If GUI2 moved or resized then redraw the colored border and reposition also GUI3 If $aPrevPos2[0] <> $aWinPos2[0] Or $aPrevPos2[1] <> $aWinPos2[1] Or $aPrevPos2[2] <> $aWinPos2[2] Or $aPrevPos2[3] <> $aWinPos2[3] Then ; store new position/size $aPrevPos2[0] = $aWinPos2[0] $aPrevPos2[1] = $aWinPos2[1] $aPrevPos2[2] = $aWinPos2[2] $aPrevPos2[3] = $aWinPos2[3] _GDIPlus_GraphicsDispose($hGraphic) WinMove($hGUI3, '', $aWinPos2[0] - $iOuterTolerance, $aWinPos2[1] - $iOuterTolerance, $aWinPos2[2] + $iOuterTolerance * 2, $aWinPos2[3] + $iOuterTolerance * 2) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI2) _GDIPlus_GraphicsClear($hGraphic, $AlphaKey) ; erase rect _GDIPlus_PenSetDashStyle($hPen, $GDIP_DASHSTYLESOLID) _GDIPlus_PenSetColor($hPen, $RectBgColor) _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $aWinPos2[2], $aWinPos2[3], $hPen) ; _GDIPlus_PenSetDashStyle($hPen, $GDIP_DASHSTYLEDOT) DllCall($__g_hGDIPDll, "int", "GdipSetPenDashArray", "handle", $hPen, "struct*", $tArray, "long", $iCount) _GDIPlus_PenSetColor($hPen, $RectColor) _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $aWinPos2[2], $aWinPos2[3], $hPen) ; draw again rect EndIf If TimerDiff($iTimerID) > $iDelay Then $fOffset = Mod($fOffset + .5, $iDotsDashesTotLen) ; this is the key for the animated dotted line ;-) by UEZ ; https://www.autoitscript.com/forum/topic/185769-moving-dash-rectangle/?do=findComment&comment=1343851 DllCall($__g_hGDIPDll, "int", "GdipSetPenDashOffset", "handle", $hPen, "float", $fOffset) _GDIPlus_GraphicsClear($hGraphic, $AlphaKey) ; erase rect _GDIPlus_PenSetDashStyle($hPen, $GDIP_DASHSTYLESOLID) _GDIPlus_PenSetColor($hPen, $RectBgColor) _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $aWinPos2[2], $aWinPos2[3], $hPen) ; _GDIPlus_PenSetDashStyle($hPen, $GDIP_DASHSTYLEDOT) DllCall($__g_hGDIPDll, "int", "GdipSetPenDashArray", "handle", $hPen, "struct*", $tArray, "long", $iCount) _GDIPlus_PenSetColor($hPen, $RectColor) _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $aWinPos2[2], $aWinPos2[3], $hPen) ; draw again rect $iTimerID = TimerInit() EndIf Until Not _IsPressed("1", $hDLL) ; Right click released EndIf If TimerDiff($iTimerID) > $iDelay Then $fOffset = Mod($fOffset + .5, $iDotsDashesTotLen) DllCall($__g_hGDIPDll, "int", "GdipSetPenDashOffset", "handle", $hPen, "float", $fOffset) ; this is the key for the animated dotted line ;-) by UEZ _GDIPlus_GraphicsClear($hGraphic, $AlphaKey) ; erase rect _GDIPlus_PenSetDashStyle($hPen, $GDIP_DASHSTYLESOLID) _GDIPlus_PenSetColor($hPen, $RectBgColor) _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $aWinPos2[2], $aWinPos2[3], $hPen) ; _GDIPlus_PenSetDashStyle($hPen, $GDIP_DASHSTYLEDOT) DllCall($__g_hGDIPDll, "int", "GdipSetPenDashArray", "handle", $hPen, "struct*", $tArray, "long", $iCount) _GDIPlus_PenSetColor($hPen, $RectColor) _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $aWinPos2[2], $aWinPos2[3], $hPen) ; draw again rect $iTimerID = TimerInit() EndIf If _IsPressed('1B', $hDLL) Then $bEscape = True ; 1B ESC key Until ((_IsPressed('2', $hDLL) Or $bEscape) And (WinActive($hGUI2) Or WinActive($hGUI3))) ; RightClick means I'm done with selection Do; neutralize the RightClick persistency Until Not _IsPressed('2', $hDLL) Local $aGotRect[4] = [ _ $aWinPos2[0] + $iBorder, _ ; ....... UpperLeft X of window's client area $aWinPos2[1] + $iBorder, _ ; ....... UpperLeft Y of window's client area $aWinPos2[2] - $iBorder * 2, _ ; ... Width of window's client area $aWinPos2[3] - $iBorder * 2 _ ; .... Height of window's client area ] _GDIPlus_PenDispose($hPen) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_Shutdown() DllClose($hDLL) $tArray = 0 GUIDelete($hGUI3) GUIDelete($hGUI2) Return SetError(0, $bEscape, $aGotRect) EndFunc ;==>_Crop