WildByDesign Posted Tuesday at 05:42 PM Posted Tuesday at 05:42 PM 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: expandcollapse popup#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 ioa747 Posted Tuesday at 07:43 PM Solution Posted Tuesday at 07:43 PM (edited) expandcollapse popup#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 Tuesday at 07:44 PM by ioa747 I know that I know nothing
WildByDesign Posted Tuesday at 08:30 PM Author Posted Tuesday at 08:30 PM 1 hour ago, ahmet said: Have sou tried wm_activate? No, for some reason I must have overlooked that one. Thank you.
WildByDesign Posted Tuesday at 08:33 PM Author Posted Tuesday at 08:33 PM @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. ioa747 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now