Ejemplo n.º 1
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            // Set hook
            var foregroundWindowChangedHandler = new WinEventHandler(
                (handle, type, windowHandle, objectId, childId, thread, timeMs) =>
            {
                // Ignore events from non-windows
                if (objectId != 0)
                {
                    return;
                }

                // Get foreground window text and class name
                var text      = NativeMethods.GetWindowText(windowHandle);
                var className = NativeMethods.GetWindowClassName(windowHandle);

                // Output to console
                LogLine($"Handle: {windowHandle}");
                LogLine($"Text: {text}");
                LogLine($"Class: {className}");
                LogLine();
            });

            NativeMethods.SetWinEventHook(0x0003, foregroundWindowChangedHandler);
        }
        private void btnEventHandler_Click(object sender, RoutedEventArgs e)
        {
            WinEventHandler win = new WinEventHandler(updateInfo, commandHandler);

            win.Owner = System.Windows.Window.GetWindow(this);
            if (win.InitObject(UBT))
            {
                win.ShowDialog();
            }
            win = null;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Register a windows event hook
        /// </summary>
        public IntPtr RegisterWinEvent(
            uint eventId, WinEventHandler handler,
            uint processId = 0, uint threadId = 0, uint flags = 0)
        {
            var handle = NativeMethods.SetWinEventHook(eventId, eventId, IntPtr.Zero, handler, processId, threadId, flags);

            if (handle == IntPtr.Zero)
            {
                Debug.WriteLine($"Could not register WinEventHook for {eventId}", GetType().Name);
                return(IntPtr.Zero);
            }

            _hookHandlerDic.Add(handle, handler);
            return(handle);
        }
Ejemplo n.º 4
0
        public WindowsWindowService()
        {
            _hookManager = new HookManager();

            var foregroundWindowLocationChangedEventHandler = new WinEventHandler(
                (hook, type, hwnd, idObject, child, thread, time) =>
            {
                if (idObject != 0)
                {
                    return;                    // only events from windows
                }
                if (hwnd != _lastForegroundWindow)
                {
                    return;                                    // skip non-foreground windows
                }
                IsForegroundFullScreen = IsWindowFullScreen(hwnd);
            });
            var foregroundWindowChangedEventHandler = new WinEventHandler(
                (hook, type, hwnd, idObject, child, thread, time) =>
            {
                if (idObject != 0)
                {
                    return;                    // only events from windows
                }
                _lastForegroundWindow  = hwnd;
                IsForegroundFullScreen = IsWindowFullScreen(hwnd);

                // Hook location changed event for foreground window
                if (_foregroundWindowLocationChangedHook != IntPtr.Zero)
                {
                    _hookManager.UnregisterWinEvent(_foregroundWindowLocationChangedHook);
                }
                _foregroundWindowLocationChangedHook = _hookManager.RegisterWinEvent(0x800B,
                                                                                     foregroundWindowLocationChangedEventHandler, 0, thread);
            });

            _hookManager.RegisterWinEvent(0x0003, foregroundWindowChangedEventHandler);

            // Init
            IsForegroundFullScreen = IsWindowFullScreen(GetForegroundWindow());
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Hook a WinEvent
 /// </summary>
 /// <param name="winEvent"></param>
 /// <param name="winEventHandler"></param>
 /// <returns>true if success</returns>
 public void Hook(WinEvent winEvent, WinEventHandler winEventHandler)
 {
     Hook(winEvent, winEvent, winEventHandler);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Hook a WinEvent
        /// </summary>
        /// <param name="winEventStart"></param>
        /// <param name="winEventEnd"></param>
        /// <param name="winEventHandler"></param>
        public void Hook(WinEvent winEventStart, WinEvent winEventEnd, WinEventHandler winEventHandler)
        {
            var hookPtr = SetWinEventHook(winEventStart, winEventEnd, IntPtr.Zero, _winEventHandler, 0, 0, WinEventHookFlags.WINEVENT_SKIPOWNPROCESS | WinEventHookFlags.WINEVENT_OUTOFCONTEXT);

            _winEventHandlers.Add(hookPtr, winEventHandler);
        }
Ejemplo n.º 7
0
 public static IntPtr SetWinEventHook(uint eventId, WinEventHandler handler)
 {
     return(SetWinEventHookInternal(eventId, eventId, IntPtr.Zero, handler, 0, 0, 0));
 }
Ejemplo n.º 8
0
 private static extern IntPtr SetWinEventHookInternal(
     uint eventMin, uint eventMax,
     IntPtr eventHandlerAssemblyHandle, WinEventHandler eventHandler,
     uint processId, uint threadId, uint flags);
Ejemplo n.º 9
0
 public static extern IntPtr SetWinEventHook(
     uint eventMin, uint eventMax,
     IntPtr hmodWinEventProc, WinEventHandler lpfnWinEventProc,
     uint idProcess, uint idThread, uint dwFlags);