Check size and position in background

83 views Asked by At

I want to detect if a window is resized or moved using this code:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 195, 47, 192, 124, BitOR($WS_SIZEBOX, $WS_SYSMENU))
$Button1 = GUICtrlCreateButton("OK", 16, 8, 75, 25)
$Button2 = GUICtrlCreateButton("CANCEL", 104, 8, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

    AutoItSetOption('WinTitleMatchMode', 2)

    Local $aPos[4], $aNewPos[4]


$aPos = WinGetPos($Form1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $Button1
            MsgBox(0, '', 'Button OK is pressed')
            
        Case $Button2
            MsgBox(0, '', 'Button CANCEL is presses')
    EndSwitch
WEnd

I try to check window size and position but I can't make a loop because the script stops while in the while loop.

I have also tried to edit while, call position etc.

1

There are 1 answers

0
Xenobiologist On

Because I'm still not 100% sure, what you are trying to achieve. I'll post two solutions.

  1. Detect Move or Resize of your own Autoit-GUI.

includes: #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3>



#include 
#include 
#include 

$hGUI = GUICreate("Test", 500, 500, 10, 10, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
Global $fResized = False
GUISetState()

GUIRegisterMsg($WM_MOVE, "_WM_MOVE")
GUIRegisterMsg($WM_SIZE, "_WM_SIZE")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; If the flag is set
    If $fResized Then
        MsgBox(0, "Hi", "Now you can run your function")
        ; Clear the flag again
        $fResized = False
    EndIf
WEnd

Func _WM_MOVE($hWnd, $iMsg, $wParam, $lParam)
    If $hWnd = $hGUI Then
        ConsoleWrite("Moving at " & @MSEC & ' to ' & _ArrayToString(WinGetPos($hGUI)) & @CRLF)
    EndIf
EndFunc   ;==>_WM_MOVE

Func _WM_SIZE($hWnd, $iMsg, $wParam, $lParam)
    ; Set the flag
    $fResized = True
    If $hWnd = $hGUI Then
        ConsoleWrite("Resized at " & @MSEC & ' to ' & _ArrayToString(WinGetPos($hGUI)) & @CRLF)
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_SIZE

  1. Check if any window is resized/moved like notepad.exe window

includes: #include <WinAPIConstants.au3> #include <WinAPISys.au3> #include <WindowsConstants.au3>


#include 
#include 
#include 

Opt( "MustDeclareVars", 1)

Global $g_hEventProc = DllCallbackRegister("_EventProc", "none", "ptr;dword;hwnd;long;long;dword;dword")
Global $g_hEventHook = _WinAPI_SetWinEventHook($EVENT_MIN, $EVENT_MAX, DllCallbackGetPtr($g_hEventProc))

Global $g_hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
Global $g_hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($g_hStub_KeyProc), _WinAPI_GetModuleHandle(0))

OnAutoItExitRegister(OnAutoItExit)

Local $iPid = Run(@SystemDir & "\notepad.exe")

While ProcessExists($iPid)
  Sleep(1000)
WEnd

;======================================
Func _EventProc($g_hEventHook, $iEvent, $hWnd, $iObjectID, $iChildID, $iThreadId, $iEventTime)

    Switch $iEvent
        Case $EVENT_SYSTEM_MOVESIZESTART
            ConsoleWrite("Start : a window is being moved or resized . " & _
                "Window title : " & WinGetTitle($hWnd) & @crlf)

        Case $EVENT_SYSTEM_MOVESIZEEND
            ConsoleWrite("End : the movement or resizing of a window has finished . " & _
                "Window title : " & WinGetTitle($hWnd) & @crlf & @crlf)
  EndSwitch
EndFunc   ;==>_EventProc

;======================================
Func _KeyProc($nCode, $wParam, $lParam)

    If $nCode _KeyProc

;======================================
Func OnAutoItExit()

    _WinAPI_UnhookWinEvent($g_hEventHook)
    DllCallbackFree($g_hEventProc)

    _WinAPI_UnhookWindowsHookEx($g_hHook)
    DllCallbackFree($g_hStub_KeyProc)
EndFunc   ;==>_OnAutoItExit