コード例 #1
0
ファイル: AppSystem.cs プロジェクト: Temetra/MouseTrap
        // Constructor
        public AppSystem(IForegroundWindowHook fgHook, IWindowUpdateHook winHook, IMouseHook msHook)
        {
            _state = new InitialState();

            _stateContext = new AppStateContext
            {
                PathModeLocksToHandle = false,
                ForegroundHook        = fgHook,
                WindowHook            = winHook,
                MouseHook             = msHook,
                SetCurrentState       = (next) => SetCurrentState(next),
                SendLockStateChange   = () => LockStateChanged.Invoke(this, new Events.LockStateChangedEventArgs {
                    IsLocked = IsLocked
                }),
                SendPathChange = (path) => PathChanged.Invoke(this, new Events.PathChangedEventArgs {
                    Path = path
                }),
                SendTitleChange = (title) => TitleChanged.Invoke(this, new Events.TitleChangedEventArgs {
                    Title = title
                }),
                SendDimensionsChange = (dimensions) => DimensionsChanged.Invoke(this, new Events.DimensionsChangedEventArgs {
                    Dimensions = dimensions
                }),
                SendForegroundChange = (inForeground) => ForegroundChanged.Invoke(this, new Events.ForegroundStateChangedEventArgs {
                    InForeground = inForeground
                })
            };

            fgHook.ForegroundWindowChanged += ForegroundHook_ForegroundWindowChanged;
            winHook.WindowClosed           += WindowHook_WindowClosed;
            winHook.DimensionsChanged      += WindowHook_DimensionsChanged;
            winHook.TitleChanged           += WindowHook_TitleChanged;
        }
コード例 #2
0
 public static void CheckForegroundWindow(object sender, ElapsedEventArgs e)
 {
     WindowManager.IsFocused = IsActive();
     if (IsActive() != _isActive)
     {
         ForegroundChanged?.Invoke();
     }
     _isActive = IsActive();
 }
コード例 #3
0
 private static void CheckForegroundWindow(object sender, ElapsedEventArgs e)
 {
     if (IsForeground == IsActive)
     {
         return;
     }
     IsForeground = IsActive;
     ForegroundChanged?.Invoke();
 }
コード例 #4
0
 void WinEventCallback(IntPtr hWinEventHook, WinAPI.WinEvent eventType, IntPtr hWnd,
                       int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
 {
     switch (eventType)
     {
     case WinAPI.WinEvent.SystemForeground:
         ForegroundChanged?.Invoke(this, new WinEventArgs(hWnd));
         break;
     }
 }
コード例 #5
0
        public WindowMonitor()
        {
            _foregroundWindowChanged = (hook, type, hwnd, idObject, child, thread, time) =>
            {
                // ignore any event not pertaining directly to the window
                if (idObject != User32.NativeMethods.OBJID_WINDOW)
                {
                    return;
                }

                // Ignore if this is a bogus hwnd (shouldn't happen)
                if (hwnd == IntPtr.Zero)
                {
                    return;
                }
                var(processId, windowText, windowClass) = ProcessWindowInformation(hwnd);

                //Couldn't find the processId of the window
                if (processId == 0)
                {
                    return;
                }

                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        var process     = Process.GetProcessById((int)processId);
                        var processName = process.MainModule?.FileName ?? "N/A";
                        ForegroundChanged?.Invoke(this, new Event(processId, processName, windowText, windowClass, hwnd));
                    }
                    catch (Exception e)
                    {
                        Log.Warning(e, "Couldn't get info about foreground process");
                    }
                });
            };

            ComThread.Invoke(() =>
            {
                User32.NativeMethods.SetWinEventHook(User32.NativeMethods.EVENT_SYSTEM_MINIMIZEEND,
                                                     User32.NativeMethods.EVENT_SYSTEM_MINIMIZEEND,
                                                     IntPtr.Zero, _foregroundWindowChanged,
                                                     0,
                                                     0,
                                                     User32.NativeMethods.WINEVENT_OUTOFCONTEXT);

                User32.NativeMethods.SetWinEventHook(User32.NativeMethods.EVENT_SYSTEM_FOREGROUND,
                                                     User32.NativeMethods.EVENT_SYSTEM_FOREGROUND,
                                                     IntPtr.Zero, _foregroundWindowChanged,
                                                     0,
                                                     0,
                                                     User32.NativeMethods.WINEVENT_OUTOFCONTEXT);
            });
        }
コード例 #6
0
 private void OnForegroundChange(object o, ProcessArgs e)
 {
     ForegroundChanged?.Invoke(o, e);
 }