Sign in to follow this  
Followers 0
ChrisL

Setting Boundries for Dragging Gui

2 posts in this topic

#1 ·  Posted (edited)

I'm trying to make a Zoom and Crop function which uses a Gui hole to select the crop area.

The Graphics\GuiDrag.pngs (50 pixel image) are used to drag the gui hole corners around but you can drag them outside of the image.

I need to prevent the GuiDrag (Corners) from going outside of the image cell and I can't for the life of me get my head around what I need to do.

The Image Cell will eventually vary in size so the solution needs to be dynamic.

Thanks

Chris

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GDIPlus.au3>


Opt("GuiOnEventmode",1)

_GDIPlus_Startup()
$FileName = "DSCF0094.jpg";

$FrameW = 800
$FrameH = 600

Local $aCrop[4]
$X = 0
$Y = 1
$W = 2
$H = 3

OnAutoItExitRegister("_Cleanup")

$hMain = GUICreate("Zoom & Crop",1000,700)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Exit" )

$Label = GuiCtrlCreateLabel("",20,20,$FrameW,$FrameH,BitOr($GUI_SS_DEFAULT_LABEL,$SS_ETCHEDFRAME))
$hLabel = GUICtrlGetHandle($Label)

;$ButtonZoomIn = GuiCtrlCreateButton("Zoom In",900,20,100,30)
;GuiCtrlSetOnEvent(-1,"_Zoom")
;$ButtonZoomOut = GuiCtrlCreateButton("Zoom Out",900,60,100,30)
;GuiCtrlSetOnEvent(-1,"_Zoom")

;$ButtonCrop = GuiCtrlCreateButton("Crop",900,100,100,30)
;GUICtrlSetOnEvent(-1,"_SaveCrop")
GuiSetState()

$ChildGrey = GUICreate("",$FrameW,$FrameH,20,20,$WS_POPUP,BitOR($WS_EX_LAYERED,$WS_EX_MDICHILD),$hMain)
GUISetState()
GUISetBkColor(0xFFFFFF) ; Make it White
WinSetOnTop($ChildGrey, "", 1) ; Put it on top
WinSetTrans($ChildGrey, "", 180)

_GuiHole($ChildGrey, 180, 240, 400, 300)

$DragTL = _GuiCtrlCreatePNG("Graphics\GuiDrag.png",180-25, 240-25, $ChildGrey)
$DragTR =  _GuiCtrlCreatePNG("Graphics\GuiDrag.png",180 + 400-25, 240-25, $ChildGrey)
$DragBL = _GuiCtrlCreatePNG("Graphics\GuiDrag.png",180-25, 240 + 300-25, $ChildGrey)
$DragBR = _GuiCtrlCreatePNG("Graphics\GuiDrag.png",180 + 400-25, 240 + 300-25, $ChildGrey)

$hImage = _GDIPlus_ImageLoadFromFile($FileName)
$ImageWidth = _GDIPlus_ImageGetWidth($hImage)
$ImageHeight = _GDIPlus_ImageGetHeight($hImage)
$hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hLabel)
_GetImageSectionScaleToFill($ImageWidth,$ImageHeight,$FrameW,$FrameH,$aCrop)

_GDIPlus_GraphicsDrawImageRectRect($hGraphic,$hImage,$aCrop[$X],$aCrop[$Y],$aCrop[$W],$aCrop[$H],0,0,$FrameW,$FrameH)

$ZoomStepW = $ImageWidth  /20
$ZoomStepH = $ImageHeight  /20

GUIRegisterMsg($WM_WINDOWPOSCHANGING, "WM_WINDOWPOSCHANGING")
GUIRegisterMsg($WM_NCHITTEST, "WM_NCHITTEST")

While 1
    Sleep(100)
WEnd


; ====================================================================================================

; Handle the WM_NCHITTEST for the layered window so it can be dragged by clicking anywhere on the image.
; ====================================================================================================

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)

    Switch $hWnd
        Case $DragTL, $DragTR, $DragBL, $DragBR
        If $iMsg = $WM_NCHITTEST Then Return $HTCAPTION
    EndSwitch


EndFunc   ;==>WM_NCHITTEST

Func WM_WINDOWPOSCHANGING($hWnd, $Msg, $wParam, $lParam)

    Local $stWinPos = DllStructCreate("uint;uint;int;int;int;int;uint", $lParam)
    Local $iLeft = DllStructGetData($stWinPos, 3)
    Local $iTop = DllStructGetData($stWinPos, 4)
    Local $iWidth = DllStructGetData($stWinPos, 5)
    Local $iHeight = DllStructGetData($stWinPos, 6)

    Switch $hWnd
        Case $DragTL
            ConsoleWrite("Drag TL" & @crlf)
            $aDragBL = WinGetPos($DragBL)
            $aDragTR = WinGetPos($DragTR)
            WinMove($DragBL,"",$iLeft,$aDragBL[1])
            WinMove($DragTR,"",$aDragTR[0],$iTop)
        Case $DragTR
            ConsoleWrite("Drag TR" & @crlf)
            $aDragTL = WinGetPos($DragTL)
            $aDragBR = WinGetPos($DragBR)
            WinMove($DragTL,"",$aDragTL[0],$iTop)
            WinMove($DragBR,"",$iLeft,$aDragBR[1])
        Case $DragBL
            ConsoleWrite("Drag BL" & @crlf)
            $aDragTL = WinGetPos($DragTL)
            $aDragBR = WinGetPos($DragBR)
            WinMove($DragTL,"",$iLeft,$aDragTL[1])
            WinMove($DragBR,"",$aDragBR[0],$iTop)
        Case $DragBR
            ConsoleWrite("Drag BR" & @crlf)
            $aDragBL = WinGetPos($DragBL)
            $aDragTR = WinGetPos($DragTR)
            WinMove($DragBL,"",$aDragBL[0],$iTop)
            WinMove($DragTR,"",$iLeft,$aDragTR[1])
        Case Else
            Return
    EndSwitch

    $aPos = WinGetPos($ChildGrey)
    $aDragTL = WinGetPos($DragTL)
    $aDragTR = WinGetPos($DragTR)
    $aDragBL = WinGetPos($DragBL)
    $aDragBR = WinGetPos($DragBR)

    $NewX = $aDragTL[0] - $aPos[0] + 25
    $NewY = $aDragTR[1] - $aPos[1] + 25
    $NewW = $aDragTR[0] - $aDragTL[0]
    $NewH = $aDragBL[1] - $aDragTL[1]

    _GuiHole($ChildGrey, $NewX , $NewY, $NewW , $NewH )


EndFunc

Func _GuiHole($h_win, $i_x, $i_y, $i_size_w, $i_size_h)
    Dim $pos, $outer_rgn, $inner_rgn, $wh, $combined_rgn, $ret
    $pos = WinGetPos($h_win)

    $outer_rgn = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", $pos[2], "long", $pos[3])
    If IsArray($outer_rgn) Then
        $inner_rgn = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", $i_x, "long", $i_y, "long", $i_x + $i_size_w, "long", $i_y + $i_size_h)
        If IsArray($inner_rgn) Then
            $combined_rgn = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", 0, "long", 0)
            If IsArray($combined_rgn) Then
                DllCall("gdi32.dll", "long", "CombineRgn", "long", $combined_rgn[0], "long", $outer_rgn[0], "long", $inner_rgn[0], "int", 4)
                $ret = DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $h_win, "long", $combined_rgn[0], "int", 1)
                If $ret[0] Then
                    Return 1
                Else
                    Return 0
                EndIf
            Else
                Return 0
            EndIf
        Else
            Return 0
        EndIf
    Else
        Return 0
    EndIf

EndFunc   ;==>_GuiHole

Func _Cleanup()
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

EndFunc

Func _Exit()
    Exit
EndFunc


Func _GetImageSectionScaleToFill($inWidth,$inHeight,$outWidth,$outHeight, ByRef $Ret)
    ; Local $Ret[4]

    If $outWidth > $outHeight then ;landscape

        $OutAspect = Round($outWidth/$outHeight,2)
        $inAspect = Round($inWidth/$inHeight,2)

        If $OutAspect >= $inAspect then ;Landscape Scale up

            ;take the original Width and devide it by the out aspect ratio to get the Height of the requied section
            ;take the inHeight away from the new Height and devide it by 2 to get the Y start point

            ;Msgbox(0,"Landscape","Resize the original by Width")
            ;OK on engine lid
            $newHeight = $inWidth / $OutAspect
            $StartPixel = ($inHeight - $newHeight) / 2

            $ret[0] = 0
            $ret[1] = $StartPixel
            $ret[2] = $inWidth
            $ret[3] = $newHeight
            ;_arrayDisplay($ret,"1 Landscape Enlarge" )

        Else ;Landscape Scale Down

            ;take the original height and times it by the out aspect ratio to get the width of the requied section
            ;take the new width away from the original width and devide it by 2 to get the X start point

            ;Msgbox(0,"Landscape","Resize the original by Height")
            $newWidth = $inHeight * $OutAspect
            $startPixel = ($inWidth - $newWidth) / 2

            $ret[0] = $startPixel
            $Ret[1] = 0
            $ret[2] = $newWidth
            $ret[3] = $inHeight
            ;_arrayDisplay($ret,"2 Landscape Shrink" )
        EndIf


    Else    ;If $outWidth <= $outHeight then ;Portrait

            $OutAspect = Round($outHeight/$outWidth,2)
            $InAspect = Round($inHeight/$inWidth,2)

        If $OutAspect > $inAspect then

            ;Msgbox(0,"Portrait Shrink","Resize the original by Height")
            $newWidth = $inHeight / $OutAspect
            $startPixel = ($inWidth - $newWidth) / 2

            $ret[0] = $startPixel
            $Ret[1] = 0
            $ret[2] = $newWidth
            $ret[3] = $inHeight

            ;_ArrayDisplay($ret,"3 Portrait Enlarge Resize by height")


        Else ;portrait

            ;Msgbox(0,"Portrait Shrink ","Resize the original by Width")
            ;OK on the wall
            $newHeight = $inWidth * $OutAspect
            $StartPixel = ($inHeight - $newHeight) / 2

            $ret[0] = 0
            $ret[1] = $StartPixel
            $ret[2] = $inWidth
            $ret[3] = $newHeight

            ;_ArrayDisplay($ret,"4 Portrait Enlarge Resize by Width")

            EndIf

    EndIf

        ;MSgbox(0,"","OutAspect =" & $OutAspect & @crlf & "inAspect = " & $inAspect)

    Return $Ret

EndFunc

Func _GuiCtrlCreatePNG($sImage,$iPosX, $iPosY, $hMainGUI, $onmousedown = "", $onmouseup = "")

    $Ret = GUICreate("", 0,0, $iPosX, $iPosY,$WS_POPUP,BitOR($WS_EX_LAYERED,$WS_EX_MDICHILD),$hMainGUI)
    $hImage = _GDIPlus_ImageLoadFromFile($sImage)
    SetBitmap($Ret,$hImage,255)
    GUISetState(@SW_SHOW)
    _GDIPlus_ImageDispose($hImage)

    If $onmousedown <> "" then GUISetOnEvent($GUI_EVENT_PRIMARYDOWN,$onmousedown)
    If $onmouseup <> "" then GUISetOnEvent($GUI_EVENT_PRIMARYUP,$onmouseup)
    Return $Ret

EndFunc

Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)

EndFunc ;==>SetBitmap

Example of problem Posted Image

GuiDrag.png Posted Image

Edited by ChrisL

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Well this is what I came up with in the end.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <Misc.au3>

Global Const $SC_DRAGMOVE = 0xF012
Global Const $WM_MOVING = 0x0216

Opt("GuiOnEventmode",1)
Opt("WinWaitDelay",1)

_GDIPlus_Startup()

$FileName = "DSCF0094.jpg";

$FrameW = 800
$FrameH = 600

Local $aCrop[4]
$X = 0
$Y = 1
$W = 2
$H = 3

OnAutoItExitRegister("_Cleanup")

$hMain = GUICreate("Zoom & Crop",1000,700)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Exit" )

$Label = GuiCtrlCreateLabel("",20,20,$FrameW,$FrameH,BitOr($GUI_SS_DEFAULT_LABEL,$SS_ETCHEDFRAME))
$hLabel = GUICtrlGetHandle($Label)

$ButtonCrop = GuiCtrlCreateButton("Save Crop",880,40,100,30)
GUICtrlSetOnEvent(-1,"_SaveRect")
GuiSetState()

$GuiTransparentLayer = GUICreate("",$FrameW,$FrameH,20,20,$WS_POPUP,BitOR($WS_EX_LAYERED,$WS_EX_MDICHILD),$hMain)
GUISetState()
GUISetBkColor(0xFFFFFF) ; Make it White
WinSetOnTop($GuiTransparentLayer, "", 1) ; Put it on top
WinSetTrans($GuiTransparentLayer, "", 180)

$DragHole = GUICreate("", 400, 300,180, 240,$WS_POPUP,BitOR($WS_EX_LAYERED,$WS_EX_MDICHILD),$hMain)
GUISetState()

_GuiHole($GuiTransparentLayer, 180, 240, 400, 300)

$DragTL = _GuiCtrlCreatePNG("Graphics\GuiDrag.png",180-25, 240-25, $GuiTransparentLayer)
$DragTR =  _GuiCtrlCreatePNG("Graphics\GuiDrag.png",180 + 400-25, 240-25, $GuiTransparentLayer)
$DragBL = _GuiCtrlCreatePNG("Graphics\GuiDrag.png",180-25, 240 + 300-25, $GuiTransparentLayer)
$DragBR = _GuiCtrlCreatePNG("Graphics\GuiDrag.png",180 + 400-25, 240 + 300-25, $GuiTransparentLayer)

$hImage = _GDIPlus_ImageLoadFromFile($FileName)
$ImageWidth = _GDIPlus_ImageGetWidth($hImage)
$ImageHeight = _GDIPlus_ImageGetHeight($hImage)
$hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hLabel)
_GetImageSectionScaleToFill($ImageWidth,$ImageHeight,$FrameW,$FrameH,$aCrop)

$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($FrameW, $FrameH, $hGraphic)
ConsoleWrite("hBMPBuff " & $hBMPBuff & @crlf)
$hGraphicContext = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)
ConsoleWrite("hGraphicContext " & $hGraphicContext & @crlf)
$Draw = _GDIPlus_GraphicsDrawImageRectRect($hGraphicContext, $hImage, $aCrop[$X],$aCrop[$Y],$aCrop[$W],$aCrop[$H],0,0,$FrameW,$FrameH)
ConsoleWrite("Draw = " & $Draw & @crlf)

_GDIPlus_GraphicsDrawImage($hGraphic, $hBMPBuff, 0, 0)



$ZoomStepW = $ImageWidth  /20
$ZoomStepH = $ImageHeight  /20

GUIRegisterMsg($WM_WINDOWPOSCHANGING, "WM_WINDOWPOSCHANGING")
GUIRegisterMsg($WM_NCHITTEST, "WM_NCHITTEST")
GUIRegisterMsg($WM_PAINT, "WM_PAINT")

While 1
    Sleep(100)
WEnd




Func WM_PAINT($hWnd, $Msg, $wParam, $lParam)

    _GDIPlus_GraphicsDrawImage($hGraphic, $hBMPBuff, 0, 0)

    Return $GUI_RUNDEFMSG

EndFunc


Func WM_WINDOWPOSCHANGING($hWnd, $Msg, $wParam, $lParam)

    Switch $hWnd
        Case $DragTL, $DragTR, $DragBL, $DragBR, $DragHole
        Case Else
            Return
    EndSwitch

    $aMain_Pos = WinGetPos($GuiTransparentLayer)
    If NOT IsArray($aMain_Pos) then Return

    $stDragTLRect = _WinAPI_GetWindowPlacement($DragTL)
    If @error then Return
    $stDragBLRect = _WinAPI_GetWindowPlacement($DragBL)
    If @error then Return
    $stDragTRRect = _WinAPI_GetWindowPlacement($DragTR)
    If @error then Return
    $stDragBRRect = _WinAPI_GetWindowPlacement($DragBR)
    If @error then Return

            Local $stWinPos = DllStructCreate("uint;uint;int;int;int;int;uint", $lParam)
            Local $iLeft = DllStructGetData($stWinPos, 3)
            Local $iTop = DllStructGetData($stWinPos, 4)
            Local $iWidth = DllStructGetData($stWinPos, 5)
            Local $iHeight = DllStructGetData($stWinPos, 6)
            Local $iX_Min = $aMain_Pos[0]
            Local $iX_Max = $aMain_Pos[0] + $aMain_Pos[2] - $iWidth
            Local $iY_Min = $aMain_Pos[1] -1
            Local $iY_Max = $aMain_Pos[1] + $aMain_Pos[3] - $iHeight +1

    Switch $hWnd

        Case  $DragHole

            If $iLeft < $iX_Min Then
                DllStructSetData($stWinPos, 3, $iX_Min)
                $iLeft = $iX_Min
            EndIf

            If $iLeft > $iX_Max Then
                DllStructSetData($stWinPos, 3, $iX_Max)
                $iLeft = $iX_Max
            EndIf

            If $iTop < $iY_Min Then
                DllStructSetData($stWinPos, 4, $iY_Min)
                $iTop = $iY_Min
            EndIf

            If $iTop > $iY_Max Then
                DllStructSetData($stWinPos, 4, $iY_Max)
                $iTop = $iY_Max
            EndIf

        Case  $DragTL,$DragBL, $DragTR, $DragBR

            If $iLeft < $iX_Min -25 Then
                DllStructSetData($stWinPos, 3, $iX_Min -25)
                $iLeft = $iX_Min -25
            EndIf

            If $iLeft > $iX_Max + 25 Then
                DllStructSetData($stWinPos, 3, $iX_Max + 25)
                $iLeft = $iX_Max + 25
            EndIf

            If $iTop < $iY_Min -25 Then
                DllStructSetData($stWinPos, 4, $iY_Min - 25)
                $iTop = $iY_Min - 25
            EndIf

            If $iTop > $iY_Max + 25 Then
                DllStructSetData($stWinPos, 4, $iY_Max + 25)
                $iTop = $iY_Max + 25
            EndIf

        Case Else

            Return

    EndSwitch

    $stDragTLRect = _WinAPI_GetWindowPlacement($DragTL)
    If @error then Return
    $stDragBLRect = _WinAPI_GetWindowPlacement($DragBL)
    If @error then Return
    $stDragTRRect = _WinAPI_GetWindowPlacement($DragTR)
    If @error then Return
    $stDragBRRect = _WinAPI_GetWindowPlacement($DragBR)
    If @error then Return

    Switch $hWnd

        Case $DragTL

            WinMove($DragBL,"",DllStructGetData($stDragTLRect, "rcNormalPosition", 1),Default)
            WinMove($DragTR,"",Default,DllStructGetData($stDragTLRect, "rcNormalPosition", 2))

        Case $DragTR

            WinMove($DragBR,"",DllStructGetData($stDragTRRect, "rcNormalPosition", 1),Default)
            WinMove($DragTL,"",Default,DllStructGetData($stDragTRRect, "rcNormalPosition", 2))

        Case $DragBL

            WinMove($DragTL,"",DllStructGetData($stDragBLRect, "rcNormalPosition", 1),Default)
            WinMove($DragBR,"",Default,DllStructGetData($stDragBLRect, "rcNormalPosition", 2))

            ;Return

        Case $DragBR

            WinMove($DragTR,"",DllStructGetData($stDragBRRect, "rcNormalPosition", 1),Default)
            WinMove($DragBL,"",Default,DllStructGetData($stDragBRRect, "rcNormalPosition", 2))

            ;Return
        Case $DragHole

            $stDragHoleRect =  _WinAPI_GetWindowPlacement($DragHole)
            WinMove($DragTL,"",DllStructGetData($stDragHoleRect, "rcNormalPosition", 1) -25,DllStructGetData($stDragHoleRect, "rcNormalPosition", 2)-25 )
            WinMove($DragBL,"",DllStructGetData($stDragHoleRect, "rcNormalPosition", 1) -25,DllStructGetData($stDragHoleRect, "rcNormalPosition", 4)-25 )
            WinMove($DragTR,"",DllStructGetData($stDragHoleRect, "rcNormalPosition", 3) -25,DllStructGetData($stDragHoleRect, "rcNormalPosition", 2)-25 )
            WinMove($DragBR,"",DllStructGetData($stDragHoleRect, "rcNormalPosition", 3) -25,DllStructGetData($stDragHoleRect, "rcNormalPosition", 4)-25 )
            ;Return

        Case Else

            Return

    EndSwitch

    $aDragTL = WinGetPos($DragTL)
    If NOT IsArray($aDragTL) then Return

    $aDragTR = WinGetPos($DragTR)
    If NOT IsArray($aDragTR) then Return

    $aDragBL = WinGetPos($DragBL)
    If NOT IsArray($aDragBL) then Return

    $aDragBR = WinGetPos($DragBR)
    If NOT IsArray($aDragBR) then Return

    $NewX = $aDragTL[0] - $aMain_Pos[0] + 25
    $NewY = $aDragTR[1] - $aMain_Pos[1] + 25
    $NewW = $aDragTR[0] - $aDragTL[0]
    $NewH = $aDragBL[1] - $aDragTL[1]

    _GuiHole($GuiTransparentLayer, $NewX ,$NewY, $NewW , $NewH )
    WinMove($DragHole,"", $aDragTL[0] +25 ,$aDragTR[1] + 25, $NewW , $NewH )

    WinSetOnTop($DragTL,"",1)
    WinSetOnTop($DragTR,"",1)
    WinSetOnTop($DragBL,"",1)
    WinSetOnTop($DragBR,"",1)

    Return
EndFunc





; ====================================================================================================

; Handle the WM_NCHITTEST for the layered window so it can be dragged by clicking anywhere on the image.
; ====================================================================================================

Func WM_NCHITTEST($hWnd, $iMsg, $iwParam, $ilParam)

    Switch $hWnd
        Case $DragTL, $DragTR, $DragBL, $DragBR, $DragHole
        If $iMsg = $WM_NCHITTEST Then Return $HTCAPTION
    EndSwitch


EndFunc   ;==>WM_NCHITTEST


Func _GuiHole($h_win, $i_x, $i_y, $i_size_w, $i_size_h)
    Dim $pos, $outer_rgn, $inner_rgn, $wh, $combined_rgn, $ret
    $pos = WinGetPos($h_win)

    $outer_rgn = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", $pos[2], "long", $pos[3])
    If IsArray($outer_rgn) Then
        $inner_rgn = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", $i_x, "long", $i_y, "long", $i_x + $i_size_w, "long", $i_y + $i_size_h)
        If IsArray($inner_rgn) Then
            $combined_rgn = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", 0, "long", 0)
            If IsArray($combined_rgn) Then
                DllCall("gdi32.dll", "long", "CombineRgn", "long", $combined_rgn[0], "long", $outer_rgn[0], "long", $inner_rgn[0], "int", 4)
                $ret = DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $h_win, "long", $combined_rgn[0], "int", 1)
                If $ret[0] Then
                    _WinAPI_DeleteObject($outer_rgn[0]) ;Release GDI Object
                    _WinAPI_DeleteObject($inner_rgn[0]) ;Release GDI Object
                    Return 1
                Else
                    Return 0
                EndIf
            Else
                Return 0
            EndIf
        Else
            Return 0
        EndIf
    Else
        Return 0
    EndIf
EndFunc   ;==>_GuiHole

Func _Cleanup()
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

EndFunc

Func _Exit()
    Exit
EndFunc




Func _GetImageSectionScaleToFill($inWidth,$inHeight,$outWidth,$outHeight, ByRef $Ret)
    ; Local $Ret[4]

    If $outWidth > $outHeight then ;landscape

        $OutAspect = Round($outWidth/$outHeight,2)
        $inAspect = Round($inWidth/$inHeight,2)

        If $OutAspect >= $inAspect then ;Landscape Scale up

            ;take the original Width and devide it by the out aspect ratio to get the Height of the requied section
            ;take the inHeight away from the new Height and devide it by 2 to get the Y start point

            ;Msgbox(0,"Landscape","Resize the original by Width")
            ;OK on engine lid
            $newHeight = $inWidth / $OutAspect
            $StartPixel = ($inHeight - $newHeight) / 2

            $ret[0] = 0
            $ret[1] = $StartPixel
            $ret[2] = $inWidth
            $ret[3] = $newHeight
            ;_arrayDisplay($ret,"1 Landscape Enlarge" )

        Else ;Landscape Scale Down

            ;take the original height and times it by the out aspect ratio to get the width of the requied section
            ;take the new width away from the original width and devide it by 2 to get the X start point

            ;Msgbox(0,"Landscape","Resize the original by Height")
            $newWidth = $inHeight * $OutAspect
            $startPixel = ($inWidth - $newWidth) / 2

            $ret[0] = $startPixel
            $Ret[1] = 0
            $ret[2] = $newWidth
            $ret[3] = $inHeight
            ;_arrayDisplay($ret,"2 Landscape Shrink" )
        EndIf


    Else    ;If $outWidth <= $outHeight then ;Portrait

            $OutAspect = Round($outHeight/$outWidth,2)
            $InAspect = Round($inHeight/$inWidth,2)

        If $OutAspect > $inAspect then

            ;Msgbox(0,"Portrait Shrink","Resize the original by Height")
            $newWidth = $inHeight / $OutAspect
            $startPixel = ($inWidth - $newWidth) / 2

            $ret[0] = $startPixel
            $Ret[1] = 0
            $ret[2] = $newWidth
            $ret[3] = $inHeight

            ;_ArrayDisplay($ret,"3 Portrait Enlarge Resize by height")


        Else ;portrait

            ;Msgbox(0,"Portrait Shrink ","Resize the original by Width")
            ;OK on the wall
            $newHeight = $inWidth * $OutAspect
            $StartPixel = ($inHeight - $newHeight) / 2

            $ret[0] = 0
            $ret[1] = $StartPixel
            $ret[2] = $inWidth
            $ret[3] = $newHeight

            ;_ArrayDisplay($ret,"4 Portrait Enlarge Resize by Width")

            EndIf

    EndIf

        ;MSgbox(0,"","OutAspect =" & $OutAspect & @crlf & "inAspect = " & $inAspect)

    Return $Ret

EndFunc

Func _GuiCtrlCreatePNG($sImage,$iPosX, $iPosY, $hMainGUI, $onmousedown = "", $onmouseup = "")

    $Ret = GUICreate("", 0,0, $iPosX, $iPosY,$WS_POPUP,BitOR($WS_EX_LAYERED,$WS_EX_MDICHILD),$hMainGUI)
    $hImage = _GDIPlus_ImageLoadFromFile($sImage)
    SetBitmap($Ret,$hImage,255)
    GUISetState(@SW_SHOW)
    _GDIPlus_ImageDispose($hImage)

    If $onmousedown <> "" then GUISetOnEvent($GUI_EVENT_PRIMARYDOWN,$onmousedown)
    If $onmouseup <> "" then GUISetOnEvent($GUI_EVENT_PRIMARYUP,$onmouseup)
    Return $Ret

EndFunc

Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)

EndFunc ;==>SetBitmap


Func _SaveRect()
    Local $ScaleX = $ImageWidth / $FrameW, $ScaleY = $ImageHeight / $FrameH

    $aPos = WinGetPos($GuiTransparentLayer)
    $aDragTL = WinGetPos($DragTL)
    $aDragTR = WinGetPos($DragTR)
    $aDragBR = WinGetPos($DragBR)

    $xC = (($aDragTL[0] +25) - $aPos[0] ) * $ScaleX
    $yC = (($aDragTL[1] +25) - $aPos[1] ) * $ScaleY
    $wC = ($aDragTR[0]- $aDragTL[0]) * $ScaleX
    $hC = ($aDragBR[1] - $aDragTR[1]) * $ScaleY
    ;Msgbox(0,"",$xC & " x "  & $yC &@crlf &  $wC & " x " & $hC)

    Local $hClone = _GDIPlus_BitmapCloneArea($hImage, $xC, $yC, $wC , $hC , $GDIP_PXF32ARGB)

    ; Get JPEG encoder CLSID
    $sCLSID = _GDIPlus_EncodersGetCLSID ("JPG")

    ; Set up parameters for 90 degree rotation
    $tData = DllStructCreate("int Quality")
    DllStructSetData($tData, "Quality", 95)

    $tParams = _GDIPlus_ParamInit (1)
    _GDIPlus_ParamAdd ($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "Quality"))

    ; Save image with rotation
    _GDIPlus_ImageSaveToFileEx ($hClone, @DesktopDir & "\Imagetst.jpg", $sCLSID, DllStructGetPtr($tParams))


    ;_GDIPlus_ImageSaveToFile($hClone, @DesktopDir & "\Imagetst.jpg") ;$sNewName)
    _WinAPI_DeleteObject($hClone)
    ShellExecute(@DesktopDir & "\Imagetst.jpg")

    _Exit()
EndFunc ;==>_SaveRect

Edit: Clean up GDI objects to prevent leak

Edited by ChrisL

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0