MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / WinActive

Function WinActive

source/window.cpp:538–577  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

536
537
538HWND WinActive(global_struct &aSettings, LPTSTR aTitle, LPTSTR aText, LPTSTR aExcludeTitle, LPTSTR aExcludeText
539 , bool aUpdateLastUsed)
540// This function must be kept thread-safe because it may be called (indirectly) by hook thread too.
541// In addition, it must not change the value of anything in aSettings except when aUpdateLastUsed==true.
542{
543 HWND target_window;
544 if (USE_FOREGROUND_WINDOW(aTitle, aText, aExcludeTitle, aExcludeText))
545 {
546 // User asked us if the "active" window is active, which is true if it's not a
547 // hidden window, or if DetectHiddenWindows is ON:
548 SET_TARGET_TO_ALLOWABLE_FOREGROUND(aSettings.DetectHiddenWindows)
549 #define UPDATE_AND_RETURN_LAST_USED_WINDOW(hwnd) \
550 {\
551 if (aUpdateLastUsed && hwnd)\
552 aSettings.hWndLastUsed = hwnd;\
553 return hwnd;\
554 }
555 UPDATE_AND_RETURN_LAST_USED_WINDOW(target_window)
556 }
557
558 HWND fore_win = GetForegroundWindow();
559 if (!fore_win)
560 return NULL;
561
562 if (!(*aTitle || *aText || *aExcludeTitle || *aExcludeText)) // Use the "last found" window.
563 return (fore_win == GetValidLastUsedWindow(aSettings)) ? fore_win : NULL;
564
565 // Only after the above check should the below be done. This is because "IfWinActive" (with no params)
566 // should be "true" if one of the script's GUI windows is active:
567 if (!aSettings.DetectWindow(fore_win)) // In this case, the caller's window can't be active.
568 return NULL;
569
570 WindowSearch ws;
571 ws.SetCandidate(fore_win);
572
573 if (ws.SetCriteria(aSettings, aTitle, aText, aExcludeTitle, aExcludeText) && ws.IsMatch()) // aSettings.DetectHiddenWindows was already checked above.
574 UPDATE_AND_RETURN_LAST_USED_WINDOW(fore_win) // This also does a "return".
575 else // If the line above didn't return, indicate that the specified window is not active.
576 return NULL;
577}
578
579
580

Callers 3

WinCloseFunction · 0.85
BIF_DECLFunction · 0.85
HotCriterionAllowsFiringFunction · 0.85

Calls 5

GetValidLastUsedWindowFunction · 0.85
DetectWindowMethod · 0.80
SetCandidateMethod · 0.80
SetCriteriaMethod · 0.80
IsMatchMethod · 0.80

Tested by

no test coverage detected