Jump to content

Need Windows message code for when window loses focus


Go to solution Solved by ioa747,

Recommended Posts

Posted

I'm trying to get a message anytime my GUI gains focus and also when it loses focus.

I've been going through the list of Windows Message Codes and trying different things and so far nothing has 

Some of what I've tried so far:

GUIRegisterMsg($WM_SETFOCUS, WM_SETFOCUS)
    GUIRegisterMsg($WM_KILLFOCUS, WM_KILLFOCUS)
    GUIRegisterMsg($WM_WINDOWPOSCHANGED, WM_WINDOWPOSCHANGED)
    GUIRegisterMsg($WM_WINDOWPOSCHANGED, WM_WINDOWPOSCHANGING)

They all seem to work properly if I minimize the GUI and restore it. However, none of them work when the GUI simply loses focus and regains focus. For example, you bring another app in front of the GUI and bring then bring the GUI back in focus. Several of them let me know when it gains focus, but not when it loses focus.

There are probably a few other ways that I can think of to achieve the same thing. But I was really hoping that the windows message codes would do the trick and keep things simple.

Here is my testing example:

#include <WindowsNotifsConstants.au3>
#include <GUIConstantsEx.au3>

Global $hGUI

Example()

Func Example()
    ; Create a GUI with various controls.
    $hGUI = GUICreate("Example", 400, 400)
    Local $idBtn_OK = GUICtrlCreateButton("OK", 310, 370, 85, 25)

    GUIRegisterMsg($WM_SETFOCUS, WM_SETFOCUS)
    GUIRegisterMsg($WM_KILLFOCUS, WM_KILLFOCUS)
    GUIRegisterMsg($WM_WINDOWPOSCHANGED, WM_WINDOWPOSCHANGED)
    GUIRegisterMsg($WM_WINDOWPOSCHANGING, WM_WINDOWPOSCHANGING)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idBtn_OK
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func WM_SETFOCUS($wParam, $lParam)
    ConsoleWrite("WM_SETFOCUS" & @CRLF)
EndFunc

Func WM_KILLFOCUS($wParam, $lParam)
    ConsoleWrite("WM_KILLFOCUS" & @CRLF)
EndFunc

Func WM_WINDOWPOSCHANGED($hWnd, $MsgID, $wParam, $lParam)   ; Sync movement of $hGUI and $hCover
    #forceref $MsgID, $wParam, $lParam
    ConsoleWrite("WM_WINDOWPOSCHANGED" & @CRLF)
    Return $GUI_RUNDEFMSG
EndFunc

Func WM_WINDOWPOSCHANGING($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ConsoleWrite("WM_WINDOWPOSCHANGING" & @CRLF)
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_WINDOWPOSCHANGING

 

If anybody knows of windows message code that does this, I would be very thankful. Otherwise I might have to do a hook or another function to continuously check the foreground window and compare to previous foreground window.

Thank you. :)

  • Solution
Posted (edited)

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; WA_ACTIVATE constants
Global Const $WA_INACTIVE = 0x0000
Global Const $WA_ACTIVE = 0x0001
Global Const $WA_CLICKACTIVE = 0x0002

Global $hGUI

Example()

Func Example()
    ; Create a GUI with various controls.
    $hGUI = GUICreate("Focus Check - ()", 400, 400)
    Local $idBtn_OK = GUICtrlCreateButton("OK", 310, 370, 85, 25)

    GUIRegisterMsg($WM_ACTIVATE, "WM_ACTIVATE_Handler")
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idBtn_OK
                ExitLoop
        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func WM_ACTIVATE_Handler($hWnd, $MsgID, $wParam, $lParam)
    #forceref $MsgID, $wParam, $lParam

    Local $sAction = ""

    Switch $WParam
        Case $WA_INACTIVE ; 0x0000
            $sAction = "(Inactive)"

        Case $WA_ACTIVE ; 0x0001
            $sAction = "(Active)"

        Case $WA_CLICKACTIVE ; 0x0002
            $sAction = "(Click Active)"

        Case Else
            $sAction = "(Unknown Action)"
    EndSwitch

    WinSetTitle ($hWnd, "", "Focus Check - " & $sAction)

    Return $GUI_RUNDEFMSG
EndFunc

 

Edited by ioa747

I know that I know nothing

Posted

@ioa747 That is absolutely perfect. The function is fast which I was hoping it would be as well which is a nice bonus.

I was not familiar at all with any of the $WA_* values. Your solution was very helpful and I'm also learning a lot from it. Thank you. I appreciate it. :)

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...