Example #1
0
        /// <summary>
        /// Creates image from a user-selected area of screen pixels. Or gets single pixel color, or just rectangle.
        /// Returns false if cancelled.
        /// </summary>
        /// <param name="result">Receives results.</param>
        /// <param name="flags"></param>
        /// <param name="toolWindow">Owner window. Temporarily hides it and its owner windows.</param>
        /// <remarks>
        /// Gets all screen pixels and shows in a full-screen topmost window, where the user can select an area.
        /// </remarks>
        public static bool CaptureUI(out WICResult result, WICFlags flags = 0, AnyWnd toolWindow = default)
        {
            result = default;

            switch (flags & (WICFlags.Image | WICFlags.Color | WICFlags.Rectangle))
            {
            case 0:
            case WICFlags.Image:
            case WICFlags.Color:
            case WICFlags.Rectangle: break;

            default: throw new ArgumentException();
            }

            AWnd[] aw = null; AWnd wTool = default;
            try {
                if (!toolWindow.IsEmpty)
                {
                    wTool = toolWindow.Wnd;
                    aw    = wTool.Get.OwnersAndThis(true);
                    foreach (var w in aw)
                    {
                        w.ShowLL(false);
                    }
                    using (new AInputBlocker(BIEvents.MouseClicks)) ATime.SleepDoEvents(300);                    //time for animations
                }

g1:
                RECT rs = SystemInformation.VirtualScreen;
                //RECT rs = AScreen.Primary.Bounds; //for testing, to see Write output in other screen
                Bitmap bs;
                bool   windowPixels = flags.HasAny(WICFlags.WindowDC | WICFlags.PrintWindow);
                if (windowPixels)
                {
                    if (!_WaitForHotkey("Press F3 to select window from mouse pointer."))
                    {
                        return(false);
                    }
                    var w = AWnd.FromMouse(WXYFlags.NeedWindow);
                    w.GetClientRect(out var rc, inScreen: true);
                    using var bw = Capture(w, w.ClientRect, flags.Has(WICFlags.PrintWindow));
                    bs           = new Bitmap(rs.Width, rs.Height);
                    using var g  = Graphics.FromImage(bs);
                    g.Clear(Color.Black);
                    g.DrawImage(bw, rc.left, rc.top);
                }
                else
                {
                    bs = Capture(rs);
                }

                var f = new _Form(bs, flags);
                f.Bounds = rs;
                switch (f.ShowDialog())
                {
                case DialogResult.OK: break;

                case DialogResult.Retry:
                    if (!windowPixels && !_WaitForHotkey("Press F3 when ready for new screenshot."))
                    {
                        return(false);
                    }
                    goto g1;

                default: return(false);
                }

                var r = f.Result;
                r.wnd  = _WindowFromRect(r);
                result = r;
            }
            finally {
                if (aw != null)
                {
                    foreach (var w in aw)
                    {
                        w.ShowLL(true);
                    }
                    if (wTool.IsAlive)
                    {
                        wTool.ShowNotMinimized();
                        wTool.ActivateLL();
                    }
                }
            }
            return(true);
        }