public AppWindowActivity(IMonitor monitor, Type monitorType, string processName, TimeSpan interval) : base(monitor, monitorType) { this.processName = processName; D3D9Capture = new D3D9Capture(); GDICapture = new GDICapture(); Interval = interval; Timer = new System.Timers.Timer(Interval.TotalMilliseconds); Timer.AutoReset = true; Timer.Elapsed += Timer_Elapsed; previousCapture = new Bitmap(1, 1); Status = ApiStatus.Ok; }
private void Timer_Elapsed(object sender, ElapsedEventArgs e) { Process[] processes = Process.GetProcessesByName(processName); if (processes == null || processes.Length == 0) { return; } Bitmap capture = null; IntPtr activeWindowHandle = UnsafeNativeMethods.GetForegroundWindow(); string title = Interop.GetWindowTitle(activeWindowHandle); Rectangle rect = Interop.GetWindowRect(activeWindowHandle); int l = rect.Left, t = rect.Top; var tid = UnsafeNativeMethods.GetWindowThreadProcessId(activeWindowHandle, out uint pid); if (processes.Any(p => p.Id == pid)) { Debug("{0} window detected at ({1},{2}).", processName, l, t); try { capture = D3D9Capture.CaptureWindow(activeWindowHandle); } catch (Direct3D9Exception d3de) { Debug("{0}. Falling back to GDI capture.", d3de.Message); capture = GDICapture.CaptureWindow(activeWindowHandle); } if (capture == null) { Error("Could not detect any window activity for app process {0}.", processName); return; } } else if (processName == "chrome" && title.Contains("Google Chrome")) { Debug("Google Chrome incognito mode window detected at ({1},{2}).", processName, rect.Left, rect.Top); try { capture = D3D9Capture.CaptureWindow(activeWindowHandle); } catch (Direct3D9Exception d3de) { Debug("{0}. Falling back to GDI capture.", d3de.Message); capture = GDICapture.CaptureWindow(activeWindowHandle); } if (capture == null) { Error("Could not detect window activity for Google Chrome."); return; } } else if ((processName == "MicrosoftEdge" || processName == "MicrosoftEdgeCP") && title.Contains("Microsoft Edge")) { Debug("Microsoft Edge window detected at ({1},{2}).", processName, rect.Left, rect.Top); try { capture = D3D9Capture.CaptureWindow(activeWindowHandle); } catch (Direct3D9Exception d3de) { Debug("{0}. Falling back to GDI capture.", d3de.Message); capture = GDICapture.CaptureWindow(activeWindowHandle); } if (capture == null) { Error("Could not detect window activity for Microsoft Edge."); return; } } else { return; } if (!ImagesAreDuplicate(previousCapture, capture)) { Interlocked.Exchange(ref previousCapture, capture); Global.MessageQueue.Enqueue <AppWindowActivity>(new AppWindowActivityMessage(processName, capture, title)); } else { Debug("{0} window did not change.", processName); } }