Sign in to follow this  
Followers 0
ryantollefson

Listen for a window position change

5 posts in this topic

Is there a way to see when a window has changed position? For example, if I have a calculator window open, and I want my script to know if I have moved the calculator (moving it with the mouse, not moving it with the script)? Ideally I would also like to be able to see when the program has been minimized as well. The other thing I would like for this to do is to be similar to OnEvent, I don't want to be constantly polling & comparing the window position if I can avoid it.

I was looking at the WM_WINDOWPOSCHANGED and the winapi, but I don't know how use it for this (or if it can be done).

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I guess you could do something like this:

While 2
$pos = WinGetPos ("", "")
While 1
    $pos2 = WinGetPos ("", "")
    If $pos[0] <> $pos2[0] Then
    ;window has moved
        TrayTip ("Window has moved!", "The window has moved!", 1)
        ExitLoop
    ElseIf $pos[1] <> $pos2[1] Then
    ;window has moved
        TrayTip ("Window has moved!", "The window has moved!", 1)

        ExitLoop
    Else
    ;Nothing
    EndIf
    
WEnd
WEnd

Win_Api would work better though.

Edited by Dampe

Share this post


Link to post
Share on other sites

Win_Api would work better though.

That's what I was thinking too, just wasn't sure how to use it for this. :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This is how you could use it:

#include <GUIConstants.au3>

$Form1 = GUICreate("Form1", 633, 447, 193, 115)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_WINDOWPOSCHANGED, "_WinPosChanged")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

;~ typedef struct {
;~     HWND hwnd;
;~     HWND hwndInsertAfter;
;~     int x;
;~     int y;
;~     int cx;
;~     int cy;
;~     UINT flags;
;~ } WINDOWPOS;
Func _WinPosChanged($hWndGUI, $MsgID, $WParam, $LParam)
    Local $winpos_Struct = DllStructCreate("hwnd hwnd;hwnd hwndInsertAfter;int x;int y;int cx;int cy;uint flags", $LParam)
    If @error Then _Exit("Error while creating the struct")
    
    ConsoleWrite("Position Changed to:" & @LF)
    ConsoleWrite("X: " & DllStructGetData($winpos_Struct, "x") & @LF)
    ConsoleWrite("Y: " & DllStructGetData($winpos_Struct, "y") & @LF)
EndFunc


Func _Exit($s_Msg)
    
    MsgBox(0, "Error", $s_Msg)
    Exit
    
EndFuncoÝ÷ ØúènW¦ËajÒ¢}ý¶IèÂ0¶h×è®az¼"Ú0°ýz-²êÞÝý²,²&å{­­iÙe~)^jÈ­(!¶·ªº*Þj(±ßÛ!زv( xÛaȬ­!ƦzêpØZºÚ"µÍÚ[ÛYH   ÑÕRPÛÛÝ[Ë]LÉÝÂÛØ[ÛÛÝ  ÌÍÕÒÐÐSÓÐÈH
ÌÍÑÜLHHÕRPÜX]J    ][ÝÑÜLI][ÝË
ÌË


ËNLËLMJBÌÍÕXYQHØ[
    ][ÝÚÙ[Ì ][ÝË  ][ÝÙÛÜ  ][ÝË  ][ÝÑÙ]Ý[XYY ][ÝÊBÌÍÐØ[XÚÈHØ[XÚÔYÚÝ ][Ý×ÐØ[ÛØÉ][ÝË ][ÝÛÛÉ][ÝË    ][ÝÚ[ÝÜ[[I][ÝÊBÌÍÒÓÒÈHØ[
    ][ÝÝÙÌ  ][ÝË  ][ÝÚÛ    ][ÝË  ][ÝÔÙ]Ú[ÝÜÒÛÚÑ^   ][ÝË  ][ÝÚ[ ][ÝË  ÌÍÕÒÐÐSÓÐË ][ÝÜ][ÝËØ[XÚÑÙ] ÌÍÐØ[XÚÊK ][ÝÚÛ    ][ÝË  ][ÝÙÛÜ  ][ÝË  ÌÍÕXYQÌJBÕRTÙ]Ý]JÕ×ÔÒÕÊBÚ[HBIÌÍÛÙÈHÕRQÙ]ÙÊ
BTÝÚ]Ú   ÌÍÛÙÂBPØÙH   ÌÍÑÕRWÑUSÐÓÔÑBBBQ^]Q[ÝÚ]ÚÑ[[ÈÐØ[ÛØÊ  ÌÍÛÛÙK ÌÍÝÔ[K  ÌÍÛ[JBBRY    ÌÍÛÛÙH È[BSØØ[  ÌÍÚWÔ][
    ][ÝÝÙÌ  ][ÝË  ][ÝÛÛÉ][ÝË    ][ÝÐØ[^ÛÚÑ^   ][ÝË  ][ÝÚÛ    ][ÝË  ÌÍÒÓÒÖÌK ][ÝÚ[ ][ÝË  ÌÍÛÛÙK ][ÝÝÜ[I][ÝË    ÌÍÝÔ[][ÝÛ[I][ÝË  ÌÍÛ[JBBT]    ÌÍÚWÔ]ÌBQ[YBSØØ[ ÌÍÐÕÔÕPÕHÝXÝÜX]][ÝÛÛÈ[NÛÛÈÔ[NÝZ[YÜØYÙNÚÛÛ   ][ÝË  ÌÍÛ[JBRYÜ[Ñ^]
    ][ÝÑÜÚ[HÜX][ÈÝXÝ    ][ÝÈ  [ÈÜBBRYÝXÝÙ]]J ÌÍÐÕÔÕPÕ ][ÝÓYÜØYÙI][ÝÊH  ÉÝÈ  ÌÍÕÓWÕÒSÕÔÔÐÒSÑQ[BIÌÍÐÕÔÕPÕHBT]Q[YBSØØ[    ÌÍÝÚ[Ü×ÔÝXÝHÝXÝÜX]J ][ÝÚÛÛÚÛÛ[ÙYÚ[Ú[[ÞÚ[ÞNÝZ[YÜÉ][ÝËÝXÝÙ]]J ÌÍÐÕÔÕPÕ ][ÝÛ[I][ÝÊJBBPÛÛÛÛUÜ]J ][ÝÔÜÚ][ÛÚ[ÙYÎ][ÝÈ    [ÈBÛÛÛÛUÜ]J   ][ÝÖ  ][ÝÈ  [ÈÝXÝÙ]]J   ÌÍÝÚ[Ü×ÔÝXÝ    ][ÝÞ  ][ÝÊH [ÈBÛÛÛÛUÜ]J   ][ÝÖN ][ÝÈ  [ÈÝXÝÙ]]J   ÌÍÝÚ[Ü×ÔÝXÝ    ][ÝÞI][ÝÊH  [ÈBBIÌÍÐÕÔÕPÕHIÌÍÝÚ[Ü×ÔÝXÝHB[[Â[ÈÑ^]
    ÌÍÜ×ÓÙÊBBPÛÛÛÛUÜ]][ÝÑÜ    ][ÝÈ  [È ÌÍÜ×ÓÙÈ  [ÈBQ^]B[[Â[ÈÛ]]Ò]^]

BBQØ[
    ][ÝÝÙÌ  ][ÝË  ][ÝÚÛ    ][ÝË  ][ÝÕ[ÛÚÕÚ[ÝÜÒÛÚÑ^   ][ÝË  ][ÝÚÛ    ][ÝË  ÌÍÒÓÒÖÌJBQØ[XÚÑYJ ÌÍÐØ[XÚÊBB[[

This is probably the way to go towards hooking into another window, but I'm afraid you still need to use a DLL for this type of hook, as of now it seems it will only work on your own applicaiton. :/

Edited by FreeFry

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