Exemple #1
0
        public static WindowsHwndSource FromHwnd(IntPtr hwnd)
        {
            const int GWLP_WNDPROC = -4;

            uint tid = NativeMethods.GetWindowThreadProcessId(hwnd, IntPtr.Zero);

            if (tid == 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            var source = new WindowsHwndSource(hwnd);

            source.hWndProcHook = NativeMethods.SetWindowLongPtr(hwnd, GWLP_WNDPROC, Marshal.GetFunctionPointerForDelegate(source.fnWndProcHook));
            if (source.hWndProcHook == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return(source);
        }
Exemple #2
0
        private static void TryAddGlobalHook(Window window)
        {
            if (window == null)
            {
                return;
            }

            IntPtr hwnd = window.PlatformImpl == null ? MainWindow.PlatformImpl.Handle.Handle : window.PlatformImpl.Handle.Handle;

            if (_HookedWindows.ContainsKey(hwnd))
            {
                return;
            }

            WindowsHwndSource source = WindowsHwndSource.FromHwnd(hwnd);

            if (source == null)
            {
                return;
            }

            _HookedWindows.Add(hwnd, new GlobalHooks(source, window));
        }
Exemple #3
0
 private GlobalHooks(WindowsHwndSource source, Window window)
 {
     _source                = source;
     _windowRef             = new WeakReference <Window>(window);
     source.WndProcCallback = WndProc;
 }