Ejemplo n.º 1
0
 private void RemoveHotKeys()
 {
     KeyboardHookHelper.UnregisterHotKey(_windowHandle, 0);
     KeyboardHookHelper.UnregisterHotKey(_windowHandle, 1);
     KeyboardHookHelper.UnregisterHotKey(_windowHandle, 2);
     _source?.RemoveHook(HwndHook);
 }
Ejemplo n.º 2
0
 private void OnSourceChanged(object sender, SourceChangedEventArgs e)
 {
     _currentHwndSource?.RemoveHook(_hook);
     _currentHwndSource = e.NewSource as HwndSource;
     _currentHwndSource?.AddHook(_hook);
     _ttl.ScalingChanged?.Invoke(_ttl.Scaling);
 }
Ejemplo n.º 3
0
        protected override void OnClosed(EventArgs e)
        {
            _source?.RemoveHook(HwndHook);
            HotKeyApi.UnregisterHotKey(_windowHandle, HotKeyApi.ActivateHotKeyId);

            base.OnClosed(e);
        }
Ejemplo n.º 4
0
        public static void RemoveGlobalKeyHook(Window window)
        {
            WindowInteropHelper helper     = new WindowInteropHelper(window);
            HwndSource          hwndSource = HwndSource.FromHwnd(helper.Handle);

            hwndSource?.RemoveHook(HwndHook);
            UnregisterHotKey(helper.Handle, WinApiConstants.HOTKEY_ID);
        }
Ejemplo n.º 5
0
 protected override void OnClosed(EventArgs e)
 {
     HotkeyManager.UnregisterHotKey(this);
     handle?.RemoveHook(WndProc);
     icon?.Dispose();
     viewModel?.Dispose();
     serverBridge?.Dispose();
 }
Ejemplo n.º 6
0
    protected virtual void Dispose(bool disposing)
    {
        _source?.RemoveHook(MessageSink);

        RawInputDevice.UnregisterDevice(HidUsageAndPage.Keyboard);
        RawInputDevice.UnregisterDevice(HidUsageAndPage.Mouse);

        _source = null;
    }
Ejemplo n.º 7
0
        private void OnClosed(object sender, EventArgs e)
        {
            ManipulationDelta     = null;
            ManipulationCompleted = null;

            _source?.RemoveHook(WndProc);

            TouchpadHelper.UnregisterInput();
        }
Ejemplo n.º 8
0
        public void UnregisterHotKeysAndHooks()
        {
            UnregisterRecordHotKey();
            SoundboardSampleCollection.ToList().ForEach(soundboardSample =>
            {
                UnregisterSoundboardSampleHotKey(soundboardSample.HotkeyId);
            });

            // Hooks
            _hwndSource?.RemoveHook(HwndHook);
            _hwndSource?.Dispose();
        }
Ejemplo n.º 9
0
        /// <inheritdoc />
        /// <summary>
        /// Unregister all the hotkeys that were previously registered
        /// </summary>
        public void Dispose()
        {
            _logController?.AddLog(new ApplicationLog("Disposing all available hotkey objects and hooks"));
            // Unregister all the registered hot keys.
            for (int i = _currentId; i > 0; i--)
            {
                NativeMethods.UnregisterHotKey(_helper.Handle, i);
            }
            // Remove the hook from the HwndSource
            _source?.RemoveHook(HwndHook);
            _source = null;

            _logController?.AddLog(new ApplicationLog("Done disposing all available hotkey objects and hooks"));
        }
Ejemplo n.º 10
0
        protected override void OnClosed(EventArgs e)
        {
            if (!skipSaveAggregation)
            {
                viewModel?.SaveAggregation();
            }

            HotkeyManager.UnregisterHotKey(this);
            handle?.RemoveHook(WndProc);
            icon?.Dispose();
            viewModel?.Dispose();
            serverBridge?.Dispose();
            saveDimensionScheduler?.Dispose();
        }
Ejemplo n.º 11
0
        protected virtual void Dispose(bool itIsSafeToAlsoFreeManagedObjects)
        {
            if (_disposed)
            {
                return;
            }

            if (itIsSafeToAlsoFreeManagedObjects)
            {
                HwndSource?source = PresentationSource.FromVisual(this) as HwndSource;
                source?.RemoveHook(WndProc);
            }

            _disposed = true;
        }
Ejemplo n.º 12
0
        private async void Closing(CancelEventArgs e)
        {
            await _appData.DealExistedFileAction?.Invoke();

            var dataSourceFile = ResourcesMap.LocationDic[Location.DataSourceFile];

            if (!string.IsNullOrWhiteSpace(_appData.Config.DataSourceJsonString))
            {
                FileHelper.SaveToFile(dataSourceFile, _appData.Config.DataSourceJsonString);
            }
            else
            {
                if (FileHelper.Exists(dataSourceFile))
                {
                    File.Delete(dataSourceFile);
                }
            }

            FileHelper.SaveToJsonFile(ResourcesMap.LocationDic[Location.GlobalConfigFile], _appData.Config);
            _hwndSource?.RemoveHook(_hwndSourceHook);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public override void Dispose()
        {
            // Note Setup can be run after disposing, to "reset" this instance
            // due to the code in ChromiumWebBrowser.PresentationSourceChangedHandler
            if (!isSetup)
            {
                return;
            }

            isSetup = false;

            owner.GotFocus  -= OwnerGotFocus;
            owner.LostFocus -= OwnerLostFocus;

            owner.RemoveHandler(UIElement.MouseDownEvent, mouseDownEventHandler);

            if (source != null && sourceHook != null)
            {
                source.RemoveHook(sourceHook);
                source = null;
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Detachs self
        /// </summary>
        public void Detach()
        {
            if (!attached)
            {
                return;
            }
            attached = false;

            if (window != null)
            {
                window.KeyDown -= OnWindowKeyDown;
                window.KeyUp   -= OnWindowKeyUp;

                this.window = null;
            }

            // Hookup non client area messages
            if ((attachedHwndSource != null) && (!attachedHwndSource.IsDisposed))
            {
                attachedHwndSource.RemoveHook(WindowProc);
            }
        }
Ejemplo n.º 15
0
        public void Stop()
        {
            if (disposed)
            {
                throw new ObjectDisposedException(null);
            }

            if (source != null)
            {
                source.RemoveHook(WndProc);
                if (!source.IsDisposed)
                {
                    if (!DeregisterShellHookWindow(hWnd))
                    {
                        int error = Marshal.GetLastWin32Error();
                        throw new Win32Exception(error, "Call DeregisterShellHookWindow failed.");
                    }
                    source.Dispose();
                }
                source = null;
            }
        }
Ejemplo n.º 16
0
        private void SetRawMouseInputHook(bool enable)
        {
            try
            {
                IntPtr hWnd = this.GetHandle(true);
                if (hWnd == IntPtr.Zero)
                {
                    // The app is probably terminating
                    return;
                }

                HwndSource source = HwndSource.FromHwnd(hWnd);
                if (source == null)
                {
                    logger.Error($"Raw mouse input hook not registered: couldn't get {nameof(HwndSource)}!");
                    return;
                }

                if (enable)
                {
                    if (RawInputApi.RegisterRawMouseInput(source.Handle))
                    {
                        source.AddHook(this.RawInputWndProc);
                    }
                    else
                    {
                        logger.Error($"Failed to register raw input mouse device. Error code: {Marshal.GetLastWin32Error()}");
                    }
                }
                else
                {
                    source.RemoveHook(this.RawInputWndProc);
                }
            }
            catch (Exception e)
            {
                logger.Error($"Unhandled error while {(enable ? "en" : "dis")}abling raw mouse input hook", e);
            }
        }
Ejemplo n.º 17
0
        private void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            if (disposing)
            {
                this.SuppressException = true;
                hwndSource.RemoveHook(hook);
            }

            for (int i = GlobalHotKeyContainer.Count - 1; i >= 0; i--)
            {
                RemoveGlobalHotKey(GlobalHotKeyContainer[i]);
            }

            KeyBoardUnHook();
            LocalHotKeyContainer.Clear();
            ChordHotKeyContainer.Clear();
            disposed = true;
        }
Ejemplo n.º 18
0
        private void FrictionScrollViewer_Unloaded(object sender, RoutedEventArgs e)
        {
            if (_window == null)
            {
                return;
            }

            IntPtr windowhandle = new WindowInteropHelper(_window).Handle;

            if (windowhandle == IntPtr.Zero)
            {
                return;
            }

            HwndSource hwndSource = HwndSource.FromHwnd(windowhandle);

            hwndSource.RemoveHook(new HwndSourceHook(WndProc));

            if (f != null)
            {
                f.Cleanup();
            }
        }
        private void Cleanup()
        {
            if (!isCleanedUp)
            {
                isCleanedUp = true;

                // clean up events
                System.ComponentModel.DependencyPropertyDescriptor.FromProperty(Window.ResizeModeProperty, typeof(Window))
                .RemoveValueChanged(AssociatedObject, ResizeModePropertyChangedCallback);
                AssociatedObject.Loaded            -= AssociatedObject_Loaded;
                AssociatedObject.Unloaded          -= AssociatedObject_Unloaded;
                AssociatedObject.SourceInitialized -= AssociatedObject_SourceInitialized;
                AssociatedObject.StateChanged      -= AssociatedObject_StateChanged;
                AssociatedObject.Activated         -= AssociatedObject_Activated;
                AssociatedObject.Deactivated       -= AssociatedObject_Deactivated;
                AssociatedObject.IsVisibleChanged  -= AssociatedObject_IsVisibleChanged;
                if (hwndSource != null)
                {
                    hwndSource.RemoveHook(WindowProc);
                }
                windowChrome = null;
            }
        }
Ejemplo n.º 20
0
        // Protected implementation of Dispose pattern.
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                _handle.Dispose();
                NativeMethods.RemoveClipboardFormatListener(_hwndSource.Handle);
                _hwndSource.RemoveHook(WndProc);
                try
                {
                    _hwndSource?.Dispose();
                }
                catch (Exception)
                {
                    //if here, already disposed
                }
            }
            _disposed = true;
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Unregister, removes hook and frees identifier.
        /// </summary>
        private void InternalDispose(bool calledManually)
        {
            if (IsDisposed)
            {
                return;
            }

            _source.RemoveHook(HwndHook);

            if (IsRegistered)
            {
                Unregister();
            }

            ActiveIdentifiers.Remove(_identifier);

            IsDisposed = true;

            if (calledManually)
            {
                GC.SuppressFinalize(this);
            }
        }
Ejemplo n.º 22
0
        private void SetHotKey(bool Add)
        {
            if (Add)
            {
                hWndSource.AddHook(MainWindowProc);
                HotKeyBF = HotKey.GlobalAddAtom($"{SettingHelp.Settings.播放暂停.Item1}-{Enum.GetName(typeof(System.Windows.Forms.Keys), SettingHelp.Settings.播放暂停.Item2)}");
                HotKeyTZ = HotKey.GlobalAddAtom($"{SettingHelp.Settings.停止关闭.Item1}-{Enum.GetName(typeof(System.Windows.Forms.Keys), SettingHelp.Settings.停止关闭.Item2)}");
                HotKeyHB = HotKey.GlobalAddAtom($"{SettingHelp.Settings.开关画笔.Item1}-{Enum.GetName(typeof(System.Windows.Forms.Keys), SettingHelp.Settings.开关画笔.Item2)}");

                HotKey.RegisterHotKey(winHandle, HotKeyBF, SettingHelp.Settings.播放暂停.Item1, SettingHelp.Settings.播放暂停.Item2);
                HotKey.RegisterHotKey(winHandle, HotKeyTZ, SettingHelp.Settings.停止关闭.Item1, SettingHelp.Settings.停止关闭.Item2);
                HotKey.RegisterHotKey(winHandle, HotKeyHB, SettingHelp.Settings.开关画笔.Item1, SettingHelp.Settings.开关画笔.Item2);
            }
            else//暂时没起作用,todo
            {
                hWndSource.RemoveHook(MainWindowProc);
                HotKey.GlobalDeleteAtom((short)HotKeyBF);
                HotKey.GlobalDeleteAtom((short)HotKeyTZ);
                HotKey.GlobalDeleteAtom((short)HotKeyHB);
                HotKey.UnregisterHotKey(winHandle, HotKeyBF);
                HotKey.UnregisterHotKey(winHandle, HotKeyTZ);
                HotKey.UnregisterHotKey(winHandle, HotKeyHB);
            }
        }
        private void Cleanup()
        {
            if (!isCleanedUp)
            {
                isCleanedUp = true;

                // clean up events
                if (AssociatedObject is MetroWindow)
                {
                    System.ComponentModel.DependencyPropertyDescriptor.FromProperty(MetroWindow.IgnoreTaskbarOnMaximizeProperty, typeof(MetroWindow))
                    .RemoveValueChanged(AssociatedObject, IgnoreTaskbarOnMaximizePropertyChangedCallback);
                }
                AssociatedObject.Loaded            -= AssociatedObject_Loaded;
                AssociatedObject.Unloaded          -= AssociatedObject_Unloaded;
                AssociatedObject.SourceInitialized -= AssociatedObject_SourceInitialized;
                AssociatedObject.StateChanged      -= AssociatedObject_StateChanged;
                AssociatedObject.Activated         -= AssociatedObject_Activated;
                if (hwndSource != null)
                {
                    hwndSource.RemoveHook(WindowProc);
                }
                windowChrome = null;
            }
        }
Ejemplo n.º 24
0
        private void Cleanup(bool isClosing)
        {
            if (isCleanedUp)
            {
                return;
            }

            isCleanedUp = true;

            OnCleanup();

            // clean up events
            AssociatedObject.SourceInitialized -= AssociatedObject_SourceInitialized;
            AssociatedObject.Loaded            -= AssociatedObject_Loaded;
            AssociatedObject.Unloaded          -= AssociatedObject_Unloaded;
            AssociatedObject.Closed            -= AssociatedObject_Closed;
            AssociatedObject.StateChanged      -= AssociatedObject_StateChanged;
            AssociatedObject.LostFocus         -= AssociatedObject_LostFocus;
            AssociatedObject.Deactivated       -= AssociatedObject_Deactivated;

            hwndSource?.RemoveHook(WindowProc);

            _RestoreStandardChromeState(isClosing);
        }
Ejemplo n.º 25
0
        private void PresentationSourceChangedHandler(object sender, SourceChangedEventArgs args)
        {
            if (args.NewSource != null)
            {
                var newSource = (HwndSource)args.NewSource;

                source = newSource;

                if (source != null)
                {
                    matrix     = source.CompositionTarget.TransformToDevice;
                    sourceHook = SourceHook;
                    source.AddHook(sourceHook);
                }
            }
            else if (args.OldSource != null)
            {
                if (source != null && sourceHook != null)
                {
                    source.RemoveHook(sourceHook);
                    source = null;
                }
            }
        }
Ejemplo n.º 26
0
 private void RemoveHwndHook()
 {
     AssociatedObject.SourceInitialized -= AssociatedObject_SourceInitialized;
     _mHWNDSource.RemoveHook(HwndHook);
 }
Ejemplo n.º 27
0
 public static void RemoveHotkey(Window parentWindow)
 {
     HwndSource.RemoveHook(HwndHook);
     HwndSource = null;
     UnregisterHotKey(parentWindow);
 }
Ejemplo n.º 28
0
 public void RemoveHooksAndUnregister()
 {
     PresentationSource.RemoveHook(WndProc);
     IsHookActive = false;
 }
Ejemplo n.º 29
0
        } //

        private void StopListeningToClipboard()
        {
            ChangeClipboardChain(_HWndSource.Handle, _HWndNextViewer); // remove from cliboard viewer chain
            _HWndNextViewer = IntPtr.Zero;
            _HWndSource.RemoveHook(WinProc);
        } //
        /// <summary>
        /// Create an observable for the specified window or HwndSource
        /// </summary>
        /// <param name="window">Window</param>
        /// <param name="hWndSource">HwndSource</param>
        /// <param name="before">Func which does something as soon as the observable is created</param>
        /// <param name="disposeAction">Action which disposes something when the observable is disposed</param>
        /// <returns>IObservable</returns>
        internal static IObservable <WindowMessageInfo> WinProcMessages <TState>(Window window, HwndSource hWndSource, Func <IntPtr, TState> before = null, Action <TState> disposeAction = null)
        {
            if (window == null && hWndSource == null)
            {
                throw new NotSupportedException("One of Window or HwndSource must be supplied");
            }

            return(Observable.Create <WindowMessageInfo>(observer =>
            {
                // This handles the message, and generates the observable OnNext
                IntPtr WindowMessageHandler(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
                {
                    observer.OnNext(WindowMessageInfo.Create(hWnd, msg, wParam, lParam));
                    // ReSharper disable once AccessToDisposedClosure
                    if (hWndSource.IsDisposed)
                    {
                        observer.OnCompleted();
                    }
                    return IntPtr.Zero;
                }

                TState state = default;

                void HwndSourceDisposedHandle(object sender, EventArgs e)
                {
                    observer.OnCompleted();
                }

                void RegisterHwndSource()
                {
                    if (before != null)
                    {
                        state = before(hWndSource.Handle);
                    }
                    hWndSource.Disposed += HwndSourceDisposedHandle;
                    hWndSource.AddHook(WindowMessageHandler);
                }

                if (window != null)
                {
                    hWndSource = window.ToHwndSource();
                }
                if (hWndSource != null)
                {
                    RegisterHwndSource();
                }
                else if (window != null)
                {
                    // No, try to get it later
                    window.SourceInitialized += (sender, args) =>
                    {
                        hWndSource = window.ToHwndSource();
                        RegisterHwndSource();
                        // Simulate the WM_NCCREATE
                        observer.OnNext(WindowMessageInfo.Create(hWndSource.Handle, (int)WindowsMessages.WM_NCCREATE, IntPtr.Zero, IntPtr.Zero));
                    };
                }

                return Disposable.Create(() =>
                {
                    disposeAction?.Invoke(state);
                    hWndSource.Disposed -= HwndSourceDisposedHandle;
                    hWndSource.RemoveHook(WindowMessageHandler);
                    hWndSource.Dispose();
                });
            })
                   // Make sure there is always a value produced when connecting
                   .Publish()
                   .RefCount());
        }