Exemple #1
0
        protected ScreenShot()
        {
            Rectangle bounds = Screen.PrimaryScreen.Bounds;

            _captureBitmap = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format24bppRgb);

            using (Graphics captureGraphics = Graphics.FromImage(_captureBitmap))
            {
                captureGraphics.CopyFromScreen(bounds.Location, new Point(0, 0), bounds.Size);
            }

            _screenWindow = ScreenShotWindow.ScreenWindow;
            _screenWindow.CullObscuredAncestors();
        }
        public static Region FindWindowVisibleRegion(ScreenShotWindow topWindow, ScreenShotWindow window, bool includeChildWindows)
        {
            Stack<ScreenShotWindow> stack = new Stack<ScreenShotWindow>();

            Region region = new Region();
            region.MakeEmpty();

            try
            {
                stack.Push(topWindow);

                bool windowHasBeenDrawn = false;

                while (stack.Count > 0)
                {
                    ScreenShotWindow currentWindow = stack.Pop();

                    if (currentWindow == window)
                    {
                        region.Union(currentWindow.Bounds);
                        windowHasBeenDrawn = true;
                    }
                    else
                    {
                        if (windowHasBeenDrawn)
                        {
                            region.Exclude(currentWindow.Bounds);
                        }
                    }

                    if (currentWindow != window || !includeChildWindows)
                    {
                        foreach (ScreenShotWindow childWindow in currentWindow.Children)
                        {
                            stack.Push(childWindow);
                        }
                    }
                }

                return region;
            }
            catch (Exception)
            {
                region.Dispose();

                throw;
            }
        }
        protected void PopulateAncestors()
        {
            _children = new List<ScreenShotWindow>();

            IList<SystemWindow> childWindows;

            if (_systemWindow == null)
            {
                childWindows = SystemWindow.TopLevelWindows;
            }
            else
            {
                childWindows = _systemWindow.ChildWindows;
            }

            foreach (SystemWindow childSystemWindow in childWindows)
            {
                if (!childSystemWindow.IsVisible) continue;

                ScreenShotWindow window = new ScreenShotWindow(childSystemWindow);

                _children.Add(window);

                window.PopulateAncestors();
            }
        }
        public static ScreenShotWindow FindWindowByPoint(ScreenShotWindow topWindow, Point point)
        {
            if (!topWindow.Bounds.Contains(point)) return null;

            ScreenShotWindow containingChild = null;

            foreach (ScreenShotWindow child in topWindow.PaintOrderChildren)
            {
                if (child.Bounds.Contains(point)) containingChild = child;
            }

            if (containingChild != null)
            {
                return FindWindowByPoint(containingChild, point);
            }
            else
            {
                return topWindow;
            }
        }