Example #1
0
        public static void Initialize_(Window owner = null)
        {
            try
            {
                if (null != owner)
                {
                    if (source_.RootVisual != owner)
                    {
                        createHwndSource_(owner);
                    }
                }
            }
            finally
            {
                if (null == source_ ||
                    null == hook_)
                {
                    source_ = null;
                    hook_   = null;
                }
            }

            if (null != source_ &&
                null != hook_)
            {
                source_.AddHook(hook_);
            }
        }
 //private Window window;
 //stack overflow http://stackoverflow.com/questions/2450373/set-global-hotkeys-using-c-sharp
 //private class Window : NativeWindow, IDisposable
 //{
 //    private static int WM_HOTKEY = 0x0312;
 //    private IHotKeyOwner owner;
 //    public Window(IHotKeyOwner owner)
 //    {
 //        this.owner = owner;
 //        // create the handle for the window.
 //        this.CreateHandle(new CreateParams());
 //    }
 //    /// <summary>
 //    /// Overridden to get the notifications.
 //    /// </summary>
 //    /// <param name="m"></param>
 //    protected override void WndProc(ref Message m)
 //    {
 //        base.WndProc(ref m);
 //        // check if we got a hot key pressed.
 //        if (m.Msg == WM_HOTKEY)
 //        {
 //            MessageBox.Show("asdasd");
 //            // get the keys.
 //            Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
 //            ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
 //            owner.HotKeyPressed(0);
 //        }
 //    }
 //    #region IDisposable Members
 //    public void Dispose()
 //    {
 //        this.DestroyHandle();
 //    }
 //    #endregion
 //}
 public HotKeyManager(IHotKeyOwner owner, IntPtr handle)
 {
     this.owner = owner;
     this.hook = new HwndSourceHook(WndProc);
     this.hwndsource = HwndSource.FromHwnd(handle);
     hwndsource.AddHook(hook);
 }
 public HotkeyManager(Window window)
 {
     source = (HwndSource)PresentationSource.FromVisual(window);
     hook   = new HwndSourceHook(WndProc);
     source.AddHook(hook);
     ids = new List <int>();
 }
Example #4
0
 private void hookupCloseDetection()
 {
     try
     {
         if (this._webBrowser is System.Windows.Controls.WebBrowser)
         {
             hwndHookSource = PresentationSource.FromVisual(this) as HwndSource;
             if (hwndHookSource == null)
             {
                 return;
             }
             if (hookedWindows.Contains(hwndHookSource))
             {
                 Debug.WriteLine("hookupCloseDetection: Hook already found for this HwndSource");
                 return;
             }
             Debug.WriteLine("hookupCloseDetection: Hook added");
             hookedWindows.Add(hwndHookSource);
             if (hwndSourceHook == null)
             {
                 hwndSourceHook = new HwndSourceHook(WndProc);
             }
             hwndHookSource.AddHook(hwndSourceHook);
         }
     }
     catch
     {
     }
 }
Example #5
0
        private void PresentationSourceChangedHandler(object sender, SourceChangedEventArgs args)
        {
            if (args.NewSource != null)
            {
                var newSource = (HwndSource)args.NewSource;

                source = newSource;

                if (source != null)
                {
                    var notifyDpiChanged = !matrix.Equals(source.CompositionTarget.TransformToDevice);

                    matrix     = source.CompositionTarget.TransformToDevice;
                    sourceHook = SourceHook;
                    source.AddHook(sourceHook);

                    if (notifyDpiChanged)
                    {
                        managedCefBrowserAdapter.NotifyScreenInfoChanged();
                    }
                }
            }
            else if (args.OldSource != null)
            {
                RemoveSourceHook();
            }
        }
        /// <summary>
        ///     Adds an event handler
        /// </summary>
        /// <param name="hook">HwndSourceHook</param>
        public void AddHook(HwndSourceHook hook)
        {
            var newHooks = _hooks.ToList();

            newHooks.Add(hook);
            _hooks = newHooks;
        }
 public HotkeyManager(Window window)
 {
     source = (HwndSource)PresentationSource.FromVisual(window);
     hook = new HwndSourceHook(WndProc);
     source.AddHook(hook);
     ids = new List<int>();
 }
Example #8
0
        //private Window window;

        //stack overflow http://stackoverflow.com/questions/2450373/set-global-hotkeys-using-c-sharp
        //private class Window : NativeWindow, IDisposable
        //{
        //    private static int WM_HOTKEY = 0x0312;
        //    private IHotKeyOwner owner;
        //    public Window(IHotKeyOwner owner)
        //    {
        //        this.owner = owner;
        //        // create the handle for the window.
        //        this.CreateHandle(new CreateParams());
        //    }

        //    /// <summary>
        //    /// Overridden to get the notifications.
        //    /// </summary>
        //    /// <param name="m"></param>
        //    protected override void WndProc(ref Message m)
        //    {
        //        base.WndProc(ref m);

        //        // check if we got a hot key pressed.
        //        if (m.Msg == WM_HOTKEY)
        //        {
        //            MessageBox.Show("asdasd");
        //            // get the keys.
        //            Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
        //            ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);

        //            owner.HotKeyPressed(0);
        //        }
        //    }

        //    #region IDisposable Members

        //    public void Dispose()
        //    {
        //        this.DestroyHandle();
        //    }

        //    #endregion
        //}

        public HotKeyManager(IHotKeyOwner owner, IntPtr handle)
        {
            this.owner      = owner;
            this.hook       = new HwndSourceHook(WndProc);
            this.hwndsource = HwndSource.FromHwnd(handle);
            hwndsource.AddHook(hook);
        }
Example #9
0
        ///<summary>
        ///
        /// Add the necessary listeners for the Keyboard and Mouse events.
        /// Today we listen for: MouseDownEventID, KeyUpEventID, KeyDown, GotKeyboardFocus and LostKeyboardFocus
        ///
        ///</summary>
        public void  StartRecording()
        {
            PresentationSource source = PresentationSource.FromVisual(container);


            _wndproc = new HwndSourceHook(wndProc);

            if (source != null)
            {
                if (source is HwndSource)
                {
                    ((HwndSource)source).AddHook(_wndproc);
                }
                else
                {
                    throw new InvalidOperationException("HwndSource expected");
                }
            }

            container.AddHandler(System.Windows.Input.Mouse.MouseDownEvent, new MouseButtonEventHandler(mouseDownHandler), true);
            container.AddHandler(System.Windows.Input.Keyboard.KeyUpEvent, new KeyEventHandler(keyUpHandler), true);
            container.AddHandler(System.Windows.Input.Keyboard.KeyDownEvent, new KeyEventHandler(keyDownHandler), true);
            container.AddHandler(System.Windows.Input.Keyboard.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(gotFocusHandler), true);
            container.AddHandler(System.Windows.Input.Keyboard.LostKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(lostFocusHandler), true);
            _stopListening = false;
        }
        /// <summary>
        ///     Removes the event handlers that were added by AddHook
        /// </summary>
        /// <param name="hook">HwndSourceHook, The event handler to remove.</param>
        public void RemoveHook(HwndSourceHook hook)
        {
            var newHooks = _hooks.ToList();

            newHooks.Remove(hook);
            _hooks = newHooks;
        }
Example #11
0
            static void RegisterHook(ScrollViewer scrollViewer)
            {
                RemoveHook(scrollViewer);
                if (PresentationSource.FromVisual(scrollViewer) is HwndSource source)
                {
                    HwndSourceHook hook = Hook;
                    scrollViewer.SetValue(HorizontalScrollHookProperty, hook);
                    source.AddHook(hook);
                }

                IntPtr Hook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
                {
                    const int WM_MOUSEHWHEEL = 0x020E;

                    switch (msg)
                    {
                    case WM_MOUSEHWHEEL:
                        int tilt = (short)((wParam.ToInt64() >> 16) & 0xFFFF);
                        OnMouseTilt(tilt);
                        return((IntPtr)1);
                    }
                    return(IntPtr.Zero);
                }

                void OnMouseTilt(int tilt)
                {
                    scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + tilt);
                }
            }
Example #12
0
        public static void MainWindowAppendMenu(MenuFlags menuFlag, string menuTitle, Action action)
        {
            var wih = new WindowInteropHelper(Application.Current.MainWindow);
            var myHWnd = wih.Handle;

            var hMenu = GetSystemMenu(myHWnd, false);

            var uIDNewItem = (uint) _menuActionDictionary.Count + 1001;
            _menuActionDictionary.Add(uIDNewItem, action);

            AppendMenu(hMenu, menuFlag, uIDNewItem, menuTitle);

            var source = HwndSource.FromHwnd(myHWnd);
            // NOTE: hookしなおす必要があるのか分からん
            if (_hook != null)
            {
                source?.RemoveHook(_hook);
            }

            _hook = (IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
            {
                if (msg == WM_SYSCOMMAND && _menuActionDictionary.ContainsKey((uint) wParam))
                {
                    _menuActionDictionary[(uint) wParam]();
                    handled = true;
                }
                return IntPtr.Zero;
            };
            source?.AddHook(_hook);
        }
Example #13
0
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            // 获取窗体句柄
            IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;

            curHwndSource = HwndSource.FromHwnd(hwnd);
            if (curHwndSource == null)
            {
                return;
            }

            //添加关于菜单
            // Get a handle to a copy of this form's system (window) menu
            IntPtr hSysMenu = NativeMethods.GetSystemMenu(hwnd, false);

            // Add a separator
            NativeMethods.AppendMenu(hSysMenu, NativeMethods.MF_SEPARATOR, 0, string.Empty);

            // Add the About menu item
            NativeMethods.AppendMenu(hSysMenu, NativeMethods.MF_STRING, NativeMethods.SYSMENU_ABOUT_ID, "&About");

            //去除窗体边缘阴影特效
            NativeMethods.RemoveDropShadow(hwnd);

            curHwndSourceHook = new HwndSourceHook(this.WndProc);
            curHwndSource.AddHook(curHwndSourceHook);

            // 计算边框尺寸
            this.CalculateBorderSize();
        }
Example #14
0
        public static void HookHotKey(Window window, Key key,
                                      ModifierKeys modifiers, int keyId, EventHandler hookHandler)
        {
            HwndSourceHook hook = delegate(IntPtr hwnd, int msg, IntPtr wParam,
                                           IntPtr lParam, ref bool handled)
            {
                if (msg == HotKeyHelper.WM_HOTKEY)
                {
                    // TODO: parse out the lParam to see if it is actually the one registered
                    // see http://msdn.microsoft.com/en-us/library/ms646279(VS.85).aspx

                    hookHandler(window, EventArgs.Empty);
                    handled = true;
                }
                return(IntPtr.Zero);
            };

            HwndSource source = PresentationSource.FromVisual(window) as HwndSource;

            if (source == null)
            {
                throw new ApplicationException("window doesn't have a source yet. Did you wait till after the SourceInitialized event?");
            }
            source.AddHook(hook);

            RegisterHotKey(window, key, modifiers, keyId);
        }
 private void hookupWinMsgDetection()
 {
     try
     {
         hwndSource = PresentationSource.FromVisual(this) as HwndSource;
         if (hwndSource == null)
         {
             return;
         }
         if (hookedWindows.Contains(hwndSource))
         {
             Debug.WriteLine("hookupCloseDetection: Hook already found for this HwndSource");
             return;
         }
         hookedWindows.Add(hwndSource);
         if (hwndSourceHook == null)
         {
             hwndSourceHook = new HwndSourceHook(WndProc);
         }
         hwndSource.AddHook(hwndSourceHook);
     }
     catch
     {
     }
 }
Example #16
0
        protected override void OnSourceInitialized(EventArgs e)
        {
            CK.Windows.Interop.Win.Functions.SetWindowLong(_interopHelper.Handle, CK.Windows.Interop.Win.WindowLongIndex.GWL_EXSTYLE, (uint)CK.Windows.Interop.Win.WS_EX.NOACTIVATE);

            HwndSource mainWindowSrc = HwndSource.FromHwnd(_interopHelper.Handle);

            mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
            mainWindowSrc.CompositionTarget.RenderMode      = RenderMode.Default;

            if (OSVersionInfo.IsWindowsVistaOrGreater && CK.Windows.Interop.Dwm.Functions.IsCompositionEnabled())
            {
                CK.Windows.Interop.Win.Margins m = new CK.Windows.Interop.Win.Margins()
                {
                    LeftWidth = -1, RightWidth = -1, TopHeight = -1, BottomHeight = -1
                };
                CK.Windows.Interop.Dwm.Functions.ExtendFrameIntoClientArea(_interopHelper.Handle, ref m);
            }
            else
            {
                this.Background = new SolidColorBrush(Colors.WhiteSmoke);
            }

            _wndHook = new HwndSourceHook(WndProc);
            mainWindowSrc.AddHook(_wndHook);

            base.OnSourceInitialized(e);
        }
Example #17
0
        private void SetupEventHandlers()
        {
            var ownerWindow = Window.GetWindow(_textBox);

            ownerWindow.PreviewMouseDown += OwnerWindow_PreviewMouseDown;
            ownerWindow.Deactivated      += OwnerWindow_Deactivated;

            var wih            = new WindowInteropHelper(ownerWindow);
            var hwndSource     = HwndSource.FromHwnd(wih.Handle);
            var hwndSourceHook = new HwndSourceHook(HookHandler);

            hwndSource.AddHook(hwndSourceHook);
            //hwndSource.RemoveHook();?

            _textBox.TextChanged    += TextBox_TextChanged;
            _textBox.PreviewKeyDown += TextBox_PreviewKeyDown;
            _textBox.LostFocus      += TextBox_LostFocus;

            _listBox.PreviewMouseLeftButtonDown += ListBox_PreviewMouseLeftButtonDown;
            _listBox.MouseLeftButtonUp          += ListBox_MouseLeftButtonUp;
            _listBox.PreviewMouseMove           += ListBox_PreviewMouseMove;

            _resizeGrip.PreviewMouseLeftButtonDown += ResizeGrip_PreviewMouseLeftButtonDown;
            _resizeGrip.PreviewMouseMove           += ResizeGrip_PreviewMouseMove;
            _resizeGrip.PreviewMouseUp             += ResizeGrip_PreviewMouseUp;
        }
Example #18
0
 public void Initialize(IntPtr handle)
 {
     _source = HwndSource.FromHwnd(handle);
     _hook   = new HwndSourceHook(WndProc);
     _nextClipboardViewer = SetClipboardViewer(_source.Handle);
     _source.AddHook(_hook);
 }
 protected void OnLoaded(object sender, EventArgs e)
 {
     WindowInteropHelper helper = new WindowInteropHelper(this);
     _hwndSource = HwndSource.FromHwnd(helper.Handle);
     _wndProcHandler = new HwndSourceHook(HookHandler);
     _hwndSource.AddHook(_wndProcHandler);
 }
Example #20
0
        /// <summary>
        ///     Subscribe a window to process raw input: hooks to specified WNDPROC and registers raw keyboard/mouse.
        /// </summary>
        /// <param name="window"></param>
        /// <param name="hook"></param>
        public static void Subscribe([NotNull] Window window, [NotNull] HwndSourceHook hook)
        {
            if (window == null)
            {
                throw new ArgumentNullException(nameof(window));
            }

            if (hook == null)
            {
                throw new ArgumentNullException(nameof(hook));
            }

            var source = GetHwndSource(window);

            source.AddHook(hook);

            var devices = new[]
            {
                new RawInputDevice(0x01, 0x02, 0x00, source.Handle), // mouse
                new RawInputDevice(0x01, 0x06, 0x00, source.Handle)  // keyboard
            };

            var size = (uint)Marshal.SizeOf <RawInputDevice>();

            if (!NativeMethods.RegisterRawInputDevices(devices, (uint)devices.Length, size))
            {
                throw new Win32Exception("Could not register raw input devices.");
            }
        }
        /// <summary>
        /// Setup the Ime Keyboard Handler specific hooks and events
        /// </summary>
        /// <param name="source">HwndSource.</param>
        public override void Setup(HwndSource source)
        {
            if (isSetup)
            {
                return;
            }

            isSetup = true;

            this.source = source;
            sourceHook  = SourceHook;
            source.AddHook(SourceHook);

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

            mouseDownEventHandler = new MouseButtonEventHandler(OwnerMouseDown);

            owner.AddHandler(UIElement.MouseDownEvent, mouseDownEventHandler, true);

            // If the owner had focus before adding the handler then we have to run the "got focus" code here
            // or it won't set up IME properly in all cases
            if (owner.IsFocused)
            {
                SetActive();
            }
        }
Example #22
0
        public static void HandleSecondInstanceMessages(Window window, Action <IEnumerable <string> > handler)
        {
            HwndSource     hwnd = null;
            HwndSourceHook hook = (IntPtr handle, int message, IntPtr wParam, IntPtr lParam, ref bool handled) => {
                if (message == SecondInstanceMessage)
                {
                    try {
                        handler(ReceiveSomeData());
                        (window as DpiAwareWindow)?.BringToFront();
                    } catch (Exception e) {
                        Logging.Warning("Can’t handle message: " + e);
                    }
                }

                return(IntPtr.Zero);
            };

            RoutedEventHandler[] handlers = { null, null };

            // loaded
            handlers[0] = (sender, args) => {
                window.Loaded -= handlers[0];

                try {
                    hwnd = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle);
                    if (hwnd == null)
                    {
                        Logging.Warning("Can’t add one-instance hook: HwndSource is null");
                        return;
                    }

                    hwnd.AddHook(hook);
                    window.Unloaded += handlers[1];
                } catch (Exception e) {
                    Logging.Warning("Can’t add one-instance hook: " + e);
                    hook = null;
                }
            };

            // unloaded
            handlers[1] = (sender, args) => {
                window.Unloaded -= handlers[1];

                try {
                    hwnd.RemoveHook(hook);
                } catch (Exception e) {
                    Logging.Warning("Can’t remove one-instance hook: " + e);
                    hook = null;
                }
            };

            if (!window.IsLoaded)
            {
                window.Loaded += handlers[0];
            }
            else
            {
                handlers[0](null, null);
            }
        }
Example #23
0
        public static void RemoveWindowsHook(this Window window, HwndSourceHook wmHandler)
        {
            IntPtr     hwnd   = new WindowInteropHelper(window).Handle;
            HwndSource source = HwndSource.FromHwnd(hwnd);

            source.RemoveHook(wmHandler);
        }
Example #24
0
        public void ReceivingTwoMessagesNotifiesConsumerTwice()
        {
            HwndSourceHook windowHookCallback = null;
            var            fakeWindow         = Substitute
                                                .For <IHookableWindow>()
                                                .With(
                x => {
                x.AddHwndSourceHook(
                    Arg.Do <HwndSourceHook>(
                        h => windowHookCallback = h));
            });

            SystemUnderTest.TargetWindow = fakeWindow;
            SystemUnderTest.Connect();

            Assert.IsNotNull(windowHookCallback);

            var handled = false;

            windowHookCallback(IntPtr.Zero, (int)Message.WM_HOTKEY, IntPtr.Zero, IntPtr.Zero, ref handled);
            windowHookCallback(IntPtr.Zero, (int)Message.WM_HOTKEY, IntPtr.Zero, IntPtr.Zero, ref handled);

            var fakeConsumerLoop = Container.Resolve <IConsumerThreadLoop>();

            fakeConsumerLoop
            .Received(2)
            .Notify(
                Arg.Any <Func <Task> >(),
                Arg.Any <CancellationToken>());
        }
Example #25
0
        protected virtual void OnLoaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= new RoutedEventHandler(OnLoaded);

            _hwndSrc     = PresentationSource.FromDependencyObject(this) as HwndSource;
            _hwndSrcHook = new HwndSourceHook(FilterMessage);
            _hwndSrc.AddHook(_hwndSrcHook);
        }
        private void RegisterWndProcCallback()
        {
            var handle = new WindowInteropHelper(this).EnsureHandle();
            var hock   = new HwndSourceHook(WndProc);
            var source = HwndSource.FromHwnd(handle);

            source.AddHook(hock);
        }
Example #27
0
        public ScreenService()
        {
            hwndSourceHook = new HwndSourceHook(WndProc);

            timer          = new DispatcherTimer();
            timer.Tick    += new EventHandler(timer_Tick);
            timer.Interval = new TimeSpan(0, 0, 3);
        }
Example #28
0
        protected void OnLoaded(object sender, EventArgs e)
        {
            WindowInteropHelper helper = new WindowInteropHelper(this);

            _hwndSource     = HwndSource.FromHwnd(helper.Handle);
            _wndProcHandler = new HwndSourceHook(HookHandler);
            _hwndSource.AddHook(_wndProcHandler);
        }
 /// <summary>
 ///     Unsubscribe a hook
 /// </summary>
 /// <param name="hook">HwndSourceHook</param>
 private void Unsubscribe(HwndSourceHook hook)
 {
     lock (Lock)
     {
         MessageHandlerWindow.RemoveHook(hook);
         _hooks.Remove(hook);
     }
 }
Example #30
0
 public static void Init(IntPtr windowHandle)
 {
     _windowHandle = windowHandle;
     RegisterPowerSettingNotification(_windowHandle, ref monitorGuid, 0);
     source = HwndSource.FromHwnd(_windowHandle);
     hook   = new HwndSourceHook(WndProc);
     source.AddHook(hook);
 }
 void DetachWindow()
 {
     if (_hwndSrc != null)
     {
         _hwndSrc.RemoveHook(_hwndSrcHook);
         _hwndSrc     = null;
         _hwndSrcHook = null;
     }
 }
Example #32
0
        public bool InitializeHook()
        {
            var helper = new WindowInteropHelper(Application.Current.MainWindow);

            _windowHandle = helper.EnsureHandle();
            _sourceHook   = new HwndSourceHook(HwndSourceHook);
            HwndSource.FromHwnd(_windowHandle).AddHook(_sourceHook);
            return(true);
        }
Example #33
0
        public HotKey(HwndSource hwndSource)
        {
            hook = WndProc;
            this.hwndSource = hwndSource;
            hwndSource.AddHook(hook);

            var rand = new Random((int)DateTime.Now.Ticks);
            id = rand.Next();
        }
Example #34
0
 public virtual void Setup(HwndSource source)
 {
     this.source = source;
     if (source != null)
     {
         sourceHook = SourceHook;
         source.AddHook(SourceHook);
     }
 }
        public ClassicMdiFrame()
        {
            _hook = new HwndSourceHook(OnMessage);

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                PresentationSource.AddSourceChangedHandler(this, OnPresentationSourceChanged);
            }
        }
 public HotKeyControl()
 {
     this.GotFocus += new RoutedEventHandler(HotKeyControl_GotFocus);
     this.hook = new HwndSourceHook(WndProc); //Hook to to Windows messages.
     this.LostFocus += new RoutedEventHandler(HotKeyControl_LostFocus);
     this.ContextMenu = null; //Disable shortcuts.
     Text = Keys.None.ToString();
     this.IsReadOnly = true;
     this.PreviewKeyDown += new KeyEventHandler(HotKeyControl_PreviewKeyDown);
 }
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     this.handle = new WindowInteropHelper(this).Handle;            
     sourceHandle = HwndSource.FromHwnd(this.handle);
     
     // the hook allows to recieve Windows messages
     sourceHookHandle = new HwndSourceHook(WndProc);
     sourceHandle.AddHook(sourceHookHandle);
     
     // allows WndProc() to receive clipboard messages
     WinAPI.NativeMethods.AddClipboardFormatListener(this.handle);            
 }
Example #38
0
        protected override void OnSourceInitialized( EventArgs e )
        {
            _interopHelper = new WindowInteropHelper( this );

            Win.Functions.SetWindowLong( _interopHelper.Handle, Win.WindowLongIndex.GWL_EXSTYLE, (uint)CK.Windows.Interop.Win.WS_EX_NOACTIVATE );

            HwndSource mainWindowSrc = HwndSource.FromHwnd( _interopHelper.Handle );

            _wndHook = new HwndSourceHook( WndProc );
            mainWindowSrc.AddHook( _wndHook );

            base.OnSourceInitialized( e );
        }
        /// <summary>
        /// Creates a new HotKeyManager Object.
        /// </summary>
        /// <param name="hwndSource">The handle of the window. Must not be null.</param>
        public HotKeyManager(Window window, bool SuppressExceptions)
        {
            if (window == null)
                throw new ArgumentNullException("window");

            this.hook = new HwndSourceHook(WndProc); //Hook to to Windows messages.

            this.hwndSource = (HwndSource)HwndSource.FromVisual(window); // new WindowInteropHelper(window).Handle // If the InPtr is needed.
            this.hwndSource.AddHook(hook);
            this.SuppressException = SuppressException;
            this.Enabled = true;

            //AutoDispose
            window.Closing += delegate { this.Dispose(); };
        }
Example #40
0
        public HiddenWindow(EPdeviceManager deviceManager)
        {
            _EPdevices = deviceManager;

            hwnd = new WindowInteropHelper(this).EnsureHandle(); //.Handle;
            source = HwndSource.FromHwnd(hwnd);
            if (source != null)
            {
                hook = new HwndSourceHook(WndProc);
                source.AddHook(hook);
            }

            _EPdevices.Initialize(hwnd);

            this.Visibility = Visibility.Hidden;
        }
Example #41
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HotKeyManager"/> class.
        /// </summary>
        /// <param name="window">The <see cref="System.Windows.Window"/> that handles the hotkeys associated with the manager.</param>
        public HotKeyManager(System.Windows.Window window)
        {
            if (window == null)
                throw new ArgumentNullException("window");
            this.owner = window;

            this.hook = new HwndSourceHook(WndProc);
            if (this.owner.IsLoaded)
                registerKeys((System.Windows.Interop.HwndSource)System.Windows.Interop.HwndSource.FromVisual(this.owner));
            else
            {
                this.owner.Loaded += window_Loaded;
                this.hwndSource = null;
            }

            this.hotkeys = new Dictionary<int, HotKey>();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="AeroWindow"/> class.
 /// </summary>
 public AeroWindow()
 {
     HandleMouseMovement = true;
     _hook = WndProc;
 }
 public static void RemoveWindowsHook(this Window window, HwndSourceHook wmHandler)
 {
     IntPtr hwnd = new WindowInteropHelper(window).Handle;
     HwndSource source = HwndSource.FromHwnd(hwnd);
     source.RemoveHook(wmHandler);
 }
Example #44
0
        public HotKeyControl()
        {
            this.GotFocus += this.TextBoxGotFocus;
            this.LostFocus += this.TextBoxLostFocus;
            this.TextChanged += this.TextBoxTextChanged;
            this.PreviewKeyDown += this.TextBoxKeyDown;

            this.hook = new HwndSourceHook(WndProc);
            this.ContextMenu = null; //Disable shortcuts.
            this.IsReadOnly = true;
            this.AllowDrop = false;
        }
 public static void RemoveHook(this Window window, HwndSourceHook hook)
 {
     HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle);
     source.RemoveHook(hook);
 }
Example #46
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;
                }
            }
        }
        /// <summary>
        /// 客户端加载函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WinLync_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                LogManager.SystemLog.Debug("Start WinLync_Loaded");
                this.Visibility = Visibility.Hidden; ;
                this.StateChanged += new EventHandler(WinLync_StateChanged);

                SelectedUserList = new List<string>();
                WPFWidth = (int)this.Width;
                WPFHeight = (int)this.Height;
                backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);//在此事件处理程序中调用耗时的操作
                backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);//当后台操作已完成、被取消或引发异常时发生
                backgroundWorker.RunWorkerAsync();//开始执行后台操作
                threadRefreshToolBar = new Thread(new ThreadStart(WinLync_RefreshToolBar));//在 Thread 上执行的方法
                threadRefreshToolBar.Priority = ThreadPriority.BelowNormal;    
                threadRefreshToolBar.Start();
                
                LogInBusiness.GetUCStateChanged = UCContactStateChanged;//获取UC登录状态 
                LogInBusiness.StatusChanged = UCStatusChanged;
                LogInBusiness.AVSessAdded = UCAVSessAdded;
                LogInBusiness.AVSessionClosed = UCAVSessionClosed;
                LogInBusiness.AVSessionConnected = UCAVSessionConnected;

                hook = new HwndSourceHook(WndProc);//MainWindow中注册Hook
                hs = PresentationSource.FromVisual(this) as HwndSource;              
                if (null == hs)
                {
                    LogManager.SystemLog.Error("HwndSourceHook = NULL");
                    return;
                }
                hs.AddHook(hook);
                LogManager.SystemLog.Debug("End WinLync_Loaded");
            }
            catch (System.Exception ex)
            {
                LogManager.SystemLog.Error(ex.ToString());
            }

        }
 public void RemoveHook(HwndSourceHook hook)
 {
 }
        public void RemoveHook(HwndSourceHook hook)
        {
            SecurityHelper.DemandUIWindowPermission();

            //this.VerifyAccess();
            
            _hooks -= hook;
            if(_hooks == null)
            {
                _hwndWrapper.RemoveHook(_publicHook);
            }
        }
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            Loaded -= OnLoaded;

            this.SetParentToMainWindowOf(Model.Root.Manager);


            _hwndSrc = PresentationSource.FromDependencyObject(this) as HwndSource;
            _hwndSrcHook = FilterMessage;
            _hwndSrc.AddHook(_hwndSrcHook);
        }
        private void AddSourceHookIfNotAlreadyPresent()
        {
            if (source != null)
            {
                return;
            }

            source = (HwndSource)PresentationSource.FromVisual(this);

            if (source != null)
            {
                matrix = source.CompositionTarget.TransformToDevice;
                sourceHook = SourceHook;
                source.AddHook(sourceHook);
            }
        }
 public void AddHwndSourceHook(HwndSourceHook hook)
 {
     HandleSource.AddHook(hook);
 }
Example #53
0
        protected void OnLoaded(object sender, EventArgs e)
        {
            WindowInteropHelper helper = new WindowInteropHelper(this);
            _hwndSource = HwndSource.FromHwnd(helper.Handle);
            _wndProcHandler = new HwndSourceHook(FilterMessage);
            _hwndSource.AddHook(_wndProcHandler);

            if (HostedPane.Items.Count > 0)
            {
                ManagedContent cntHosted = HostedPane.Items[0] as ManagedContent;
                if (!cntHosted.IsCloseable)
                {
                    DisableXButton();
                }
            }
        }
 public void RemoveHwndSourceHook(HwndSourceHook hook)
 {
     HandleSource.RemoveHook(hook);
 }
 public void Hook(HwndSourceHook hookDelegate)
 {
     if (source == null)
     {
         IntPtr handle = new WindowInteropHelper(this).Handle;
         nextClipboardViewer = ClipboardMonitor.SetClipboardViewer(handle);
         source = HwndSource.FromHwnd(handle);
         delegateHook = new HwndSourceHook(hookDelegate);
         source.AddHook(delegateHook);
     }
 }
 /// <summary>
 ///     给指定WPF窗体添加消息钩子。
 /// </summary>
 /// <param name="this">WPF窗体实例。</param>
 /// <param name="hook"><see cref="System.Windows.Interop.HwndSourceHook" /> 实例。</param>
 /// <example>
 ///     <code>
 ///          <![CDATA[
 ///          ]]>
 ///     </code>
 /// </example>
 public static void RegisterHook(this Window @this, HwndSourceHook hook)
 {
     @this.GetHwndSource().AddHook(hook);
 }
        private void PresentationSourceChangedHandler(object sender, SourceChangedEventArgs args)
        {
            if (args.NewSource != null)
            {
                var newSource = (HwndSource)args.NewSource;

                source = newSource;

                if (source != null)
                {
                    var notifyDpiChanged = !matrix.Equals(source.CompositionTarget.TransformToDevice);

                    matrix = source.CompositionTarget.TransformToDevice;
                    sourceHook = SourceHook;
                    source.AddHook(sourceHook);

                    if (notifyDpiChanged)
                    {
                        managedCefBrowserAdapter.NotifyScreenInfoChanged();
                    }
                }
            }
            else if (args.OldSource != null)
            {
                RemoveSourceHook();
            }
        }
        public void AddHook(HwndSourceHook hook)
        {
            SecurityHelper.DemandUIWindowPermission();
            Verify.IsNotNull(hook, "hook");

            CheckDisposed(true);

            if(_hooks == null)
            {
                _hwndWrapper.AddHook(_publicHook);
            }
            _hooks += hook;
        }
        void OnLoaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= new RoutedEventHandler(OnLoaded);

            this.SetParentToMainWindowOf(Model.Root.Manager);

            _hwndSrc = HwndSource.FromDependencyObject(this) as HwndSource;
            _hwndSrcHook = new HwndSourceHook(FilterMessage);
            _hwndSrc.AddHook(_hwndSrcHook);
        }
 public void AddHook(HwndSourceHook hook)
 {
 }