/// <summary> /// Callback function that the system calls in response to event sytem foreground /// </summary> /// <param name="hWinEventHook">Handle to an event hook function</param> /// <param name="eventType">Specifies the event that occurred</param> /// <param name="hwnd">Handle to the window that generates the event, or NULL if no window is associated with the event</param> /// <param name="idObject">Identifies the object associated with the event</param> /// <param name="idChild">Identifies whether the event was triggered by an object or a child element of the object</param> /// <param name="dwEventThread">Identifies the thread that generated the event, or the thread that owns the current window</param> /// <param name="dwmsEventTime">Specifies the time, in milliseconds, that the event was generated</param> private void ForegroundEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { if (idObject != 0 || idChild != 0 || eventType != (uint)EventConstants.EVENT_SYSTEM_FOREGROUND) { return; } ForegroundWindowChanged?.Invoke(this, new EventArgs()); }
protected override void WinEventCallback(WinEventConstant eventType, IntPtr handle, int objectId, int childId) { // Only process events related to FG capture if (eventType != WinEventConstant.EVENT_SYSTEM_FOREGROUND && eventType != WinEventConstant.EVENT_SYSTEM_MINIMIZEEND) { return; } // Only process events with valid parameters if (handle == null || objectId != 0) { return; } // Get process ID _ = NativeMethods.GetWindowThreadProcessId(handle, out uint windowThreadProcId); // Ignore these windows var windowStyle = NativeMethods.GetWindowStyleEx(handle); if ((windowStyle & WindowStylesEx.WS_EX_NOACTIVATE) == WindowStylesEx.WS_EX_NOACTIVATE) { return; } // Ignore ghost window when target is unresponsive // Ghost windows take foreground but target window doesn't trigger EVENT_SYSTEM_FOREGROUND // when responsive again. var className = NativeMethods.GetClassName(handle); var currentTitle = NativeMethods.GetWindowText(handle); if (className == "Ghost" && _lastTitle == currentTitle) { return; } // Get process path string processName = NativeMethods.GetFullProcessName((int)windowThreadProcId); // Send event ForegroundWindowChanged?.Invoke(this, new ForegroundWindowChangedEventArgs { Handle = handle, WindowThreadProcId = windowThreadProcId, ProcessPath = processName }); // Store title _lastTitle = currentTitle; }
public static void CheckForegroundWindow(object sender, EventArgs e) { IntPtr hwnd = FocusManager.GetForegroundWindow(); FocusManager.GetWindowThreadProcessId(hwnd, out uint procId); Process proc = Process.GetProcessById((int)procId); if (proc.ProcessName == "TERA" || proc.ProcessName == "TCC" || proc.ProcessName == "devenv") { ForegroundWindowChanged?.Invoke(true); } else { ForegroundWindowChanged?.Invoke(false); } }
private void FireForegroundWindowChanged(IntPtr hwnd) { this.hwnd = hwnd; ForegroundWindowChanged?.Invoke(hwnd); }