Jump to content

Move Gui with gif animation by button


Go to solution Solved by ioa747,

Recommended Posts

Posted (edited)

I need to move this gui with gif animation using the DragMe button

Here is my code :

#AutoIt3Wrapper_Au3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#NoTrayIcon
#include <Constants.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include "GIFAnimation.au3"

Opt('GUIOnEventMode', 1)
Global Const $SC_DRAGMOVE = 0xF012
; Flag to show if GUI was moved
Global $fMoved = False

Global $sFile = @ScriptDir & "\gif.gif"
HotKeySet('{ESC}', '_Exit')

; Get dimension of the GIF
Global $aGIFDimension = _GIF_GetDimension($sFile)

Global $hGUI = GUICreate('', $aGIFDimension[0], $aGIFDimension[1], -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
    _WinAPI_SetLayeredWindowAttributes($hGUI, 0xABCDEF)

Global $cButton = GUICtrlCreateButton('Drag me', 225, 100, 100, 100)
    GUICtrlSetOnEvent(-1, "_DragMe")

Global $bButtonQuit = GUICtrlCreateButton("EXIT", 225, 200, 100, 20)
GUICtrlSetOnEvent(-1, "_Exit")

    ; GIF job
Global $hGIF = _GUICtrlCreateGIF($sFile, "", 120, -75)

; Additional processing of some windows messages (for example)
GUIRegisterMsg(133, "_Refresh") ; WM_NCPAINT
GUIRegisterMsg(15, "_ValidateGIFs") ; WM_PAINT

Global $iPlay = 1

; Make GUI transparent
GUISetBkColor(345) ; some random color
_WinAPI_SetLayeredWindowAttributes($hGui, 345, 255) ; making the GUI transparent
_WinAPI_SetParent($hGui, 0)

GUISetState(@SW_SHOW, $hGUI)

Func _DragMe()
    _GIF_PauseAnimation($sFile)
    Switch GUIGetMsg()
            Case $GUI_EVENT_PRIMARYDOWN
            ; Check mouse is over button
            $cInfo = GUIGetCursorInfo($hGUI)
            If $cInfo[4] = $cButton Then
                ; Clear the flag
                $fMoved = False
                ; Get initial positions of mouse and GUI
                $aInit_Pos = MouseGetPos()
                $aWin_Pos = WinGetPos($hGUI)
                ; Wait until mouse button released
                Do
                    ; Check if mouse still pressed
                    $cInfo = GUIGetCursorInfo($hGUI)
                    ; Get current mouse position
                    $aCurr_Pos = MouseGetPos()
                    ; How far has mouse moved
                    $iDiff_X = $aInit_Pos[0] - $aCurr_Pos[0]
                    $iDiff_Y = $aInit_Pos[1] - $aCurr_Pos[1]
                    ; Move GUI
                    WinMove($hGUI, "", $aWin_Pos[0] - $iDiff_X, $aWin_Pos[1] - $iDiff_Y)
                Until Not $cInfo[2]
                ; See if mouse moved while it was pressed - allow for 5 pixel margin
                If (Abs($aCurr_Pos[0] - $aInit_Pos[0]) > 5) Or (Abs($aCurr_Pos[1] - $aInit_Pos[1]) > 5) Then
                    ; If moved then set flag
                    $fMoved = True
                EndIf
            EndIf

        EndSwitch

EndFunc

; Loop till end
While 1

Sleep(500)
Switch GUIGetMsg()

        Case $GUI_EVENT_PRIMARYDOWN
            ; Check mouse is over button
            $cInfo = GUIGetCursorInfo($hGUI)
            If $cInfo[4] = $cButton Then
                ; Clear the flag
                $fMoved = False
                ; Get initial positions of mouse and GUI
                $aInit_Pos = MouseGetPos()
                $aWin_Pos = WinGetPos($hGUI)
                ; Wait until mouse button released
                Do
                    ; Check if mouse still pressed
                    $cInfo = GUIGetCursorInfo($hGUI)
                    ; Get current mouse position
                    $aCurr_Pos = MouseGetPos()
                    ; How far has mouse moved
                    $iDiff_X = $aInit_Pos[0] - $aCurr_Pos[0]
                    $iDiff_Y = $aInit_Pos[1] - $aCurr_Pos[1]
                    ; Move GUI
                    WinMove($hGUI, "", $aWin_Pos[0] - $iDiff_X, $aWin_Pos[1] - $iDiff_Y)
                Until Not $cInfo[2]
                ; See if mouse moved while it was pressed - allow for 5 pixel margin
                If (Abs($aCurr_Pos[0] - $aInit_Pos[0]) > 5) Or (Abs($aCurr_Pos[1] - $aInit_Pos[1]) > 5) Then
                    ; If moved then set flag
                    $fMoved = True
                EndIf
            EndIf

    EndSwitch

WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _Refresh($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    _GIF_RefreshGIF($hGIF)
EndFunc ;==>_Refresh

Func _ValidateGIFs($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    _GIF_ValidateGIF($hGIF)
EndFunc ;==>_ValidateGIFs

 

GIFAnimation.au3 download from here

https://code.google.com/archive/p/gif-animation/downloads

Used gif image in my script :

gif.gif

Edited by Davidyese
  • Solution
Posted (edited)

Since you have Opt('GUIOnEventMode', 1)
GUIGetMsg() does not read it at all

(In my example I do not want to use #include "GIFAnimation.au3"
and I disabled it for that - because I haven't read/analyzed it yet)

The basic changes you need to make are

declare the event in the $hGUI
Global $hGUI = GUICreate('', $aGIFDimension[0], $aGIFDimension[1], -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_DragMe") ; *** <--- 1

disable the event from $cButton
;~ GUICtrlSetOnEvent(-1, "_DragMe") ; *** <--- 2

disabling the entire
;~ Switch GUIGetMsg()  ; *** <--- 3 
from the main loop

and in _DragMe()
clean the part
;~ Switch GUIGetMsg() ; *** <--- 4
and leave only the
$cInfo = GUIGetCursorInfo($hGUI)
If $cInfo[4] = $cButton Then
...
EndIf
 

; https://www.autoitscript.com/forum/topic/213062-move-gui-with-gif-animation-by-button/#findComment-1545090

#AutoIt3Wrapper_Au3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#NoTrayIcon
#include <Constants.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
;~ #include "GIFAnimation.au3"

Opt('GUIOnEventMode', 1)
Global Const $SC_DRAGMOVE = 0xF012
; Flag to show if GUI was moved
Global $fMoved = False

Global $sFile = @ScriptDir & "\gif.gif"
HotKeySet('{ESC}', '_Exit')

; Get dimension of the GIF
;~ Global $aGIFDimension = _GIF_GetDimension($sFile)

Global $hGUI = GUICreate('', 600, 300, -1, -1, $WS_POPUP) ;, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_DragMe") ; *** <--- 1
;~ _WinAPI_SetLayeredWindowAttributes($hGUI, 0xABCDEF)

Global $cButton = GUICtrlCreateButton('Drag me', 225, 100, 100, 100)
;~     GUICtrlSetOnEvent(-1, "_DragMe") ; *** <--- 2

Global $bButtonQuit = GUICtrlCreateButton("EXIT", 225, 200, 100, 20)
GUICtrlSetOnEvent(-1, "_Exit")

; GIF job
;~ Global $hGIF = _GUICtrlCreateGIF($sFile, "", 120, -75)

; Additional processing of some windows messages (for example)
;~ GUIRegisterMsg(133, "_Refresh") ; WM_NCPAINT
;~ GUIRegisterMsg(15, "_ValidateGIFs") ; WM_PAINT

Global $iPlay = 1

; Make GUI transparent
;~ GUISetBkColor(345) ; some random color
;~ _WinAPI_SetLayeredWindowAttributes($hGui, 345, 255) ; making the GUI transparent
;~ _WinAPI_SetParent($hGui, 0)

GUISetState(@SW_SHOW, $hGUI)

; Loop till end
While 1

    Sleep(500)
;~ Switch GUIGetMsg()  ; *** <--- 3 
;~         Case $GUI_EVENT_PRIMARYDOWN
;~             ; Check mouse is over button
;~             $cInfo = GUIGetCursorInfo($hGUI)
;~             If $cInfo[4] = $cButton Then
;~                 ; Clear the flag
;~                 $fMoved = False
;~                 ; Get initial positions of mouse and GUI
;~                 $aInit_Pos = MouseGetPos()
;~                 $aWin_Pos = WinGetPos($hGUI)
;~                 ; Wait until mouse button released
;~                 Do
;~                     ; Check if mouse still pressed
;~                     $cInfo = GUIGetCursorInfo($hGUI)
;~                     ; Get current mouse position
;~                     $aCurr_Pos = MouseGetPos()
;~                     ; How far has mouse moved
;~                     $iDiff_X = $aInit_Pos[0] - $aCurr_Pos[0]
;~                     $iDiff_Y = $aInit_Pos[1] - $aCurr_Pos[1]
;~                     ; Move GUI
;~                     WinMove($hGUI, "", $aWin_Pos[0] - $iDiff_X, $aWin_Pos[1] - $iDiff_Y)
;~                 Until Not $cInfo[2]
;~                 ; See if mouse moved while it was pressed - allow for 5 pixel margin
;~                 If (Abs($aCurr_Pos[0] - $aInit_Pos[0]) > 5) Or (Abs($aCurr_Pos[1] - $aInit_Pos[1]) > 5) Then
;~                     ; If moved then set flag
;~                     $fMoved = True
;~                 EndIf
;~             EndIf
;~     EndSwitch
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _DragMe()
;~     _GIF_PauseAnimation($sFile)
;~     Switch GUIGetMsg()   ; *** <--- 4
;~             Case $GUI_EVENT_PRIMARYDOWN
    ; Check mouse is over button
    $cInfo = GUIGetCursorInfo($hGUI)
    If $cInfo[4] = $cButton Then
        ; Clear the flag
        $fMoved = False
        ConsoleWrite("$fMoved=" & $fMoved & @CRLF)
        ; Get initial positions of mouse and GUI
        $aInit_Pos = MouseGetPos()
        $aWin_Pos = WinGetPos($hGUI)
        ; Wait until mouse button released
        Do
            ; Check if mouse still pressed
            $cInfo = GUIGetCursorInfo($hGUI)
            ; Get current mouse position
            $aCurr_Pos = MouseGetPos()
            ; How far has mouse moved
            $iDiff_X = $aInit_Pos[0] - $aCurr_Pos[0]
            $iDiff_Y = $aInit_Pos[1] - $aCurr_Pos[1]
            ; Move GUI
            WinMove($hGUI, "", $aWin_Pos[0] - $iDiff_X, $aWin_Pos[1] - $iDiff_Y)
        Until Not $cInfo[2]
        ; See if mouse moved while it was pressed - allow for 5 pixel margin
        If (Abs($aCurr_Pos[0] - $aInit_Pos[0]) > 5) Or (Abs($aCurr_Pos[1] - $aInit_Pos[1]) > 5) Then
            ; If moved then set flag
            $fMoved = True
        EndIf
    EndIf
;~         EndSwitch
EndFunc   ;==>_DragMe

Func _Refresh($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
;~     _GIF_RefreshGIF($hGIF)
EndFunc   ;==>_Refresh

Func _ValidateGIFs($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
;~     _GIF_ValidateGIF($hGIF)
EndFunc   ;==>_ValidateGIFs

 

Edited by ioa747

I know that I know nothing

Posted

I was about to answer what follows, but @ioa747 was faster :D

Help file, function GUIGetMsg :

If the GUIOnEventMode option is set to 1 then the return from GUIGetMsg is always 0 and the @error is set to 1.

You can't use at same time :

Opt('GUIOnEventMode', 1)
...
GUIGetMsg() ; will not work when GUIOnEventMode option is set to 1

"I think you are searching a bug where there is no bug... don't listen to bad advice."

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...