/// <summary> /// Capture window /// The whole idea is to pick the window that we want to capture with mouse click /// First, we will use an transparent window to catch user click point /// Then we will use 'WindowFromPoint' native method to get the window handle /// </summary> private void ClickWindowToCapture() { // The target window IntPtr hWnd; // Create a dummy window to capture click point Window DummyWindow = new Window() { WindowStyle = WindowStyle.None, Topmost = true, ShowInTaskbar = false, AllowsTransparency = true, Background = Brushes.Black, Opacity = 0.1, WindowState = WindowState.Maximized, ResizeMode = ResizeMode.NoResize }; Point ClickedPoint = new Point(); // On press {ESC}: Cancel window capturing DummyWindow.KeyUp += (s, e) => { if (e.Key == Key.Escape) { DummyWindow.Close(); this.Show(); } }; // On mouse click: Get position DummyWindow.MouseDown += (s, e) => { ClickedPoint = e.GetPosition(DummyWindow); }; // After click DummyWindow.MouseUp += (s, e) => { if (ClickedPoint.X > 5 && ClickedPoint.X < (SystemParameters.PrimaryScreenWidth - 5) && ClickedPoint.Y > 5 && ClickedPoint.Y < (SystemParameters.PrimaryScreenHeight - 5)) { // We don't need it anymore DummyWindow.Close(); // Get window handle from that position hWnd = NativeMethods.WindowFromPoint(new System.Drawing.Point((int)ClickedPoint.X, (int)ClickedPoint.Y)); NativeMethods.SetForegroundWindow(hWnd); // Capture window BitmapSource bSource = ScreenCapturer.CaptureWindow(hWnd); HandleCapture(bSource); } }; notification.ShowBalloonTip(this.Title, "Click the window you want to capture", BalloonIcon.Info); this.Hide(); DummyWindow.Show(); }