private Process GetProcessFromActiveWindow() { try { IntPtr foregroundWnd = User32.GetForegroundWindow(); uint prcId; User32.GetWindowThreadProcessId(foregroundWnd, out prcId); Process prc = Process.GetProcessById((int)prcId); return(prc); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return(null); } }
public void StartMirroring() { try { Thread.Sleep(200); if (_mirroredForm == null) { CreateMirrorForm(); } _clicked = true; uint processID; IntPtr foregroundWnd = User32.GetForegroundWindow(); User32.GetWindowThreadProcessId(foregroundWnd, out processID); Process prc = Process.GetProcessById((int)processID); _mirrorState.SelectedProcess = prc; if (_defaultScreenshotWindows.Contains(_mirrorState.SelectedProcess.ProcessName)) { _mirrorState.MirrorType = MirrorState.MirrorTypes.Screenshot; User32.ShowWindow(_mirrorState.SelectedProcess.MainWindowHandle, User32.SW_SHOWMAXIMIZED); } else { _mirrorState.MirrorType = MirrorState.MirrorTypes.Window; } if (InvokeRequired) { Invoke((Action)(() => { WindowState = FormWindowState.Minimized; Thread.Sleep(250); DrawImageToForm(); timer1.Start(); _mirroredForm.Show(); _mirrorState.Active = true; _itemStop.Enabled = true; notifyIcon1.Icon = Properties.Resources.icon_active; })); } else { this.WindowState = FormWindowState.Minimized; Thread.Sleep(250); DrawImageToForm(); timer1.Start(); _mirroredForm.Show(); _mirrorState.Active = true; _itemStop.Enabled = true; notifyIcon1.Icon = Properties.Resources.icon_active; } GC.Collect(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); Application.Exit(); } }