Exemplo n.º 1
0
        void _ThreadProc()
        {
            AHookWin hk = null, hm = null; AHookAcc hwe = null;

            try {
                try {
                    if (_block.Has(BIEvents.Keys))
                    {
                        hk = AHookWin.Keyboard(_keyHookProc ??= _KeyHookProc);
                    }
                    if (_block.HasAny(BIEvents.MouseClicks | BIEvents.MouseMoving))
                    {
                        hm = AHookWin.Mouse(_mouseHookProc ??= _MouseHookProc);
                    }
                }
                catch (AuException e1) { ADebug.Print(e1); _block = 0; return; }                //failed to hook

                //This prevents occassional inserting a foreign key after the first our-script-pressed key.
                //To reproduce, let our script send small series of chars in loop, and simultaneously a foreign script send other chars.
                ATime.DoEvents();

                //AOutput.Write("started");
                Api.SetEvent(_syncEvent);

                //the acc hook detects Ctrl+Alt+Del, Win+L, UAC consent, etc. SystemEvents.SessionSwitch only Win+L.
                try { hwe = new AHookAcc(AccEVENT.SYSTEM_DESKTOPSWITCH, 0, _winEventProc ??= _WinEventProc); }
                catch (AuException e1) { ADebug.Print(e1); }                //failed to hook

                AWaitFor.Wait_(-1, WHFlags.DoEvents, _stopEvent, _threadHandle);

                if (_blockedKeys != null)
                {
                    bool onlyUp = _discardBlockedKeys || ATime.WinMilliseconds - _startTime > c_maxResendTime;
                    _blockedKeys.SendBlocked_(onlyUp);
                }
                //AOutput.Write("ended");
            }
            finally {
                _blockedKeys = null;
                hk?.Dispose();
                hm?.Dispose();
                hwe?.Dispose();
                Api.SetEvent(_syncEvent);
            }
            GC.KeepAlive(this);
        }
Exemplo n.º 2
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);
        }