Exemplo n.º 1
0
 private static bool RectContains(Native.User32.RECT rect1, Native.User32.RECT rect2)
 {
     return(rect1.Left <= rect2.Left &&
            rect1.Top <= rect2.Top &&
            rect1.Right >= rect2.Right &&
            rect1.Bottom >= rect2.Bottom);
 }
Exemplo n.º 2
0
        public static List <WindowInfo> GetVisibleWindowPositions()
        {
            var windows = FindWindows((wnd, param) =>
            {
                var windowTitle = GetWindowsText(wnd);
                return(Native.User32.IsWindowVisible(wnd) && !string.IsNullOrWhiteSpace(windowTitle));
            });

            var result = new List <WindowInfo>();

            foreach (var window in windows)
            {
                // Ignore Program Manager and Settings?
                if (window.Title == "Program Manager" || window.Title == "Settings")
                {
                    continue;
                }
                // Check if window is maximized and adjust window rectangle
                Native.User32.GetWindowPlacement(window.Hwnd, out var windowPlacement);
                Native.User32.GetWindowInfo(window.Hwnd, out var windowInfo);
                var winRect = new Native.User32.RECT();
                if (windowPlacement.ShowCmd == Native.User32.SW_MAXIMIZE)
                {
                    winRect.Top    = window.Rect.Top + (int)windowInfo.cyWindowBorders;
                    winRect.Height = window.Rect.Height - (int)windowInfo.cyWindowBorders * 2;
                }
                else
                {
                    winRect.Top    = window.Rect.Top;
                    winRect.Height = window.Rect.Height - (int)windowInfo.cyWindowBorders;
                }
                winRect.Left  = window.Rect.Left + (int)windowInfo.cxWindowBorders;
                winRect.Width = window.Rect.Width - (int)windowInfo.cxWindowBorders * 2;
                window.Rect   = winRect;

                if (result.Count == 0 || !result.Any(r => RectContains(r.Rect, window.Rect)))
                {
                    result.Add(window);
                }
            }
            return(result);
        }
        public WindowCaptureWindow(List <WindowInfo> windows, Native.User32.RECT screenRect)
        {
            InitializeComponent();

            var left = screenRect.Left;

            Left = screenRect.Left;
            var top = screenRect.Top;

            Top    = screenRect.Top;
            Width  = screenRect.Width;
            Height = screenRect.Height;

            Loaded += (s, e) =>
            {
                BackgroundRect.Rect = new Rect(0, 0, Width, Height);
                Activate();
            };

            MouseMove += (s, e) =>
            {
                var mousePoint       = e.GetPosition(HighlightCanvas);
                var mousePointScreen = new Point(mousePoint.X + left, mousePoint.Y + top);
                foreach (var windowInfo in windows)
                {
                    if (RectContainsPoint(windowInfo.Rect, mousePointScreen))
                    {
                        var currentRect = new Rect(windowInfo.Rect.Left + Math.Abs(left),
                                                   windowInfo.Rect.Top + Math.Abs(top),
                                                   windowInfo.Rect.Width,
                                                   windowInfo.Rect.Height);
                        CutoutRect.Rect         = currentRect;
                        _internalSelectedWindow = windowInfo;
                        break;
                    }
                }
            };
        }
 private static bool RectContainsPoint(Native.User32.RECT rect, Point p)
 {
     return(p.X > rect.Left && p.X < rect.Left + rect.Width &&
            p.Y > rect.Top && p.Y < rect.Top + rect.Height);
 }