コード例 #1
0
        protected override sealed HWND BuildWindowCore(HWND hwndParent)
        {
            HwndSourceParameters hwndSourceParameters = new HwndSourceParameters();
            hwndSourceParameters.WindowStyle = (int)(WS.VISIBLE | WS.CHILD | WS.CLIPSIBLINGS | WS.CLIPCHILDREN);
            //hwndSourceParameters.ExtendedWindowStyle = (int)(WS_EX.NOACTIVATE);
            hwndSourceParameters.ParentWindow = hwndParent.DangerousGetHandle();

            _hwndSource = new HwndSource(hwndSourceParameters);
            _hwndSource.SizeToContent = SizeToContent.Manual;

            // TODO: make this an option
            // On Vista, or when Win7 uses vista-blit, DX content is not
            // available via BitBlit or PrintWindow?  If WPF is using hardware
            // acceleration, anything it renders won't be available either.
            // One workaround is to force WPF to use software rendering.  Of
            // course, this is only a partial workaround since other content
            // like XNA or D2D won't work either.
            //_hwndSource.CompositionTarget.RenderMode = RenderMode.SoftwareOnly;

            // Set the root visual of the HwndSource to an instance of
            // HwndSourceHostRoot.  Hook it up as a logical child if
            // we are on the same thread.
            HwndSourceHostRoot root = new HwndSourceHostRoot();
            _hwndSource.RootVisual = root;

            root.OnMeasure += OnRootMeasured;
            AddLogicalChild(_hwndSource.RootVisual);

            SetRootVisual(Child);

            return new HWND(_hwndSource.Handle);
        }
コード例 #2
0
 static HWND()
 {
     NULL = new HWND(IntPtr.Zero);
     BROADCAST = new HWND(new IntPtr(0xffff));
     MESSAGE = new HWND(new IntPtr(-3));
     DESKTOP = new HWND(new IntPtr(0));
     TOP = new HWND(new IntPtr(0));
     BOTTOM = new HWND(new IntPtr(1));
     TOPMOST = new HWND(new IntPtr(-1));
     NOTOPMOST = new HWND(new IntPtr(-2));
 }
コード例 #3
0
        /// <summary>
        ///     Transform a point from "client" coordinate space of a window
        ///     into the "screen" coordinate space.
        /// </summary>
        public static Point TransformClientToScreen(this HwndSource hwndSource, Point point)
        {
            HWND hwnd = new HWND(hwndSource.Handle);

            POINT pt = new POINT();
            pt.x = (int)point.X;
            pt.y = (int)point.Y;

            NativeMethods.ClientToScreen(hwnd, ref pt);

            return new Point(pt.x, pt.y);
        }
コード例 #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hwndSource"></param>
        /// <returns></returns>
        public static Rect GetWindowRect(this HwndSource hwndSource)
        {
            HWND hwnd = new HWND(hwndSource.Handle);

            RECT rcWindow = new RECT();
            NativeMethods.GetWindowRect(hwnd, ref rcWindow);

            // Transform from pixels into DIPs.
            Point position = new Point(rcWindow.left, rcWindow.top);
            Vector size = new Vector(rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top);
            position = hwndSource.CompositionTarget.TransformFromDevice.Transform(position);
            size = hwndSource.CompositionTarget.TransformFromDevice.Transform(size);

            return new Rect(position, size);
        }
コード例 #5
0
        /// <summary>
        ///     Hooks into the stream of messages that are dispatched to the
        ///     specified window.
        /// </summary>
        /// <remarks>
        ///     The window must be owned by the calling thread.
        /// </remarks>
        public WindowSubclass(HWND hwnd)
        {
            if (!IsCorrectThread(hwnd))
            {
                throw new InvalidOperationException("May not hook a window created by a different thread.");
            }

            _hwnd = hwnd;
            _wndproc = WndProcStub;
            _wndprocPtr = Marshal.GetFunctionPointerForDelegate(_wndproc);

            // Because our window proc is an instance method, it is connected
            // to this instance of WindowSubclass, where we can store state.
            // We do not need to store any extra state in native memory.
            NativeMethods.SetWindowSubclass(hwnd, _wndproc, IntPtr.Zero, IntPtr.Zero);
        }
コード例 #6
0
        public static Size GetClientSize(this HwndSource hwndSource)
        {
            HWND hwnd = new HWND(hwndSource.Handle);

            RECT rcClient = new RECT();
            NativeMethods.GetClientRect(hwnd, ref rcClient);

            // Client rect should always have (0,0) as the upper-left corner.
            Debug.Assert(rcClient.left == 0);
            Debug.Assert(rcClient.left == 0);

            // Convert from pixels into DIPs.
            Vector size = new Vector(rcClient.right, rcClient.bottom);
            size = hwndSource.CompositionTarget.TransformFromDevice.Transform(size);

            return new Size(size.X, size.Y);
        }
コード例 #7
0
        /// <summary>
        ///     Transform a rectangle from the "client" coordinate space of the
        ///     window into the "screen" coordinate space.
        /// </summary>
        public static Rect TransformClientToScreen(this HwndSource hwndSource, Rect rect)
        {
            HWND hwnd = new HWND(hwndSource.Handle);

            POINT ptUpperLeft = new POINT();
            ptUpperLeft.x = (int)rect.Left;
            ptUpperLeft.y = (int)rect.Top;

            NativeMethods.ClientToScreen(hwnd, ref ptUpperLeft);

            POINT ptLowerRight = new POINT();
            ptLowerRight.x = (int)rect.Right;
            ptLowerRight.y = (int)rect.Bottom;

            NativeMethods.ClientToScreen(hwnd, ref ptLowerRight);

            return new Rect(ptUpperLeft.x, ptUpperLeft.y, ptLowerRight.x - ptUpperLeft.x, ptLowerRight.y - ptUpperLeft.y);
        }
コード例 #8
0
            protected override IntPtr WndProcOverride(HWND hwnd, WM msg, IntPtr wParam, IntPtr lParam, IntPtr id, IntPtr data)
            {
                IntPtr? result = null;

                if (msg == WM.WINDOWPOSCHANGING)
                {
                    unsafe
                    {
                        WINDOWPOS* pWindowPos = (WINDOWPOS*)lParam;

                        CopyBitsBehavior copyBitsBehavior = _hwndHost.GetCopyBitsBehavior();

                        switch(copyBitsBehavior)
                        {
                            case CopyBitsBehavior.AlwaysCopyBits:
                                pWindowPos->flags &= ~SWP.NOCOPYBITS;
                                break;

                            case CopyBitsBehavior.CopyBitsAndRepaint:
                                pWindowPos->flags &= ~SWP.NOCOPYBITS;
                                if (!_redrawMessagePosted)
                                {
                                    NativeMethods.PostMessage(hwnd, _redrawMessage, IntPtr.Zero, IntPtr.Zero);
                                    _redrawMessagePosted = true;
                                }
                                break;

                            case CopyBitsBehavior.NeverCopyBits:
                                pWindowPos->flags |= SWP.NOCOPYBITS;
                                break;

                            case CopyBitsBehavior.Default:
                            default:
                                // do nothing.
                                break;
                        }
                    }
                }
                else if (msg == _redrawMessage)
                {
                    _redrawMessagePosted = false;

                    // Invalidate the window that moved, because it might have copied garbage
                    // due to WPF rendering through DX on a different thread.
                    NativeMethods.RedrawWindow(hwnd, IntPtr.Zero, IntPtr.Zero, RDW.INVALIDATE | RDW.ALLCHILDREN);

                    // Then immediately redraw all invalid regions within the top-level window.
                    HWND hwndRoot = NativeMethods.GetAncestor(hwnd, GA.ROOT);
                    NativeMethods.RedrawWindow(hwndRoot, IntPtr.Zero, IntPtr.Zero, RDW.UPDATENOW | RDW.ALLCHILDREN);
                }
                else if (msg == WM.MOUSEACTIVATE)
                {
                    bool raiseMouseActivateCommand = _hwndHost.GetRaiseMouseActivateCommand();
                    if (raiseMouseActivateCommand)
                    {
                        // Raise the HwndHostCommands.MouseActivate command.
                        MouseActivateParameter parameter = new MouseActivateParameter();
                        HwndHostCommands.MouseActivate.Execute(parameter, _hwndHost);

                        if (parameter.HandleMessage)
                        {
                            if (parameter.Activate == false && parameter.EatMessage == false)
                            {
                                result = new IntPtr((int)MA.NOACTIVATE);
                            }
                            else if (parameter.Activate == false && parameter.EatMessage == true)
                            {
                                result = new IntPtr((int)MA.NOACTIVATEANDEAT);
                            }
                            else if (parameter.Activate == true && parameter.EatMessage == false)
                            {
                                result = new IntPtr((int)MA.ACTIVATE);
                            }
                            else // if(parameter.Activate == true && parameter.EatMessage == true)
                            {
                                result = new IntPtr((int)MA.ACTIVATEANDEAT);
                            }
                        }
                    }
                }

                return result ?? base.WndProcOverride(hwnd, msg, wParam, lParam, id, data);
            }
コード例 #9
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                StrongHWND strongHwnd = _hwnd as StrongHWND;

                if (strongHwnd != null)
                {
                    // Replace the StrongHWND reference with a regular "weak"
                    // HWND reference so that messages processed during
                    // disposing do not have to deal with a partially disposed
                    // SafeHandle.
                    _hwnd = new HWND(strongHwnd.DangerousGetHandle());

                    strongHwnd.Dispose();

                    // All done, replace the "weak" HWND reference with null.
                    _hwnd = null;
                }
            }
        }
コード例 #10
0
 internal void TransferHandleOwnership(StrongHWND hwnd)
 {
     Debug.Assert(hwnd == _hwnd); // equivalency, not reference equals
     _hwnd = hwnd;
 }
コード例 #11
0
 public bool Equals(HWND other)
 {
     if (Object.ReferenceEquals(other, null))
     {
         return handle == IntPtr.Zero;
     }
     else
     {
         return other.handle == handle;
     }
 }
コード例 #12
0
 public static extern IntPtr DefWindowProc(HWND hWnd, WM Msg, IntPtr wParam, IntPtr lParam);
コード例 #13
0
            protected override IntPtr WndProcOverride(HWND hwnd, WM msg, IntPtr wParam, IntPtr lParam, IntPtr id, IntPtr data)
            {
                // IE doesn't seem to really need to erase its background, since
                // it will paint the entire window with the web page in WM_PAINT.
                // However, it causes flickering on some systems, so we just
                // ignore the message.
                if (msg == WM.ERASEBKGND)
                {
                    return new IntPtr(1);
                }

                return base.WndProcOverride(hwnd, msg, wParam, lParam, id, data);
            }
コード例 #14
0
        private IntPtr WndProc(HWND hwnd, WM msg, IntPtr wParam, IntPtr lParam, IntPtr id, IntPtr data)
        {
            IntPtr retval = IntPtr.Zero;

            try
            {
                retval = WndProcOverride(hwnd, msg, wParam, lParam, id, data);
            }
            finally
            {
                if (_hwnd != HWND.NULL)
                {
                    Debug.Assert(_hwnd == hwnd);

                    if (msg == WM.NCDESTROY)
                    {
                        Dispose();
                    }
                    else if (msg == _disposeMessage && wParam == _wndprocPtr)
                    {
                        DisposeHelper(lParam == IntPtr.Zero ? false : true);
                    }
                }
            }

            return retval;
        }
コード例 #15
0
        private bool IsCorrectThread(HWND hwnd)
        {
            int processId;
            int threadId = NativeMethods.GetWindowThreadProcessId(hwnd, out processId);

            return (processId == NativeMethods.GetCurrentProcessId() && threadId == NativeMethods.GetCurrentThreadId());
        }
コード例 #16
0
 protected virtual IntPtr WndProcOverride(HWND hwnd, WM msg, IntPtr wParam, IntPtr lParam, IntPtr id, IntPtr data)
 {
     // Call the next window proc in the subclass chain.
     return NativeMethods.DefSubclassProc(hwnd, msg, wParam, lParam);
 }
コード例 #17
0
        protected override sealed void DestroyWindowOverride(HWND hwnd)
        {
            Debug.Assert(hwnd.DangerousGetHandle() == _hwndSource.Handle);

            _hwndSource.Dispose();
            _hwndSource = null;
        }
コード例 #18
0
        protected override sealed HWND BuildWindowOverride(HWND hwndParent)
        {
            HwndSourceParameters hwndSourceParameters = new HwndSourceParameters();
            hwndSourceParameters.WindowStyle = (int)(WS.VISIBLE | WS.CHILD | WS.CLIPSIBLINGS | WS.CLIPCHILDREN);
            hwndSourceParameters.ParentWindow = hwndParent.DangerousGetHandle();

            _hwndSource = new HwndSource(hwndSourceParameters);
            _hwndSource.SizeToContent = SizeToContent.Manual;

            // Set the root visual of the HwndSource to an instance of
            // HwndSourceHostRoot.  Hook it up as a logical child if
            // we are on the same thread.
            HwndSourceHostRoot root = new HwndSourceHostRoot();
            _hwndSource.RootVisual = root;

            root.OnMeasure += OnRootMeasured;
            AddLogicalChild(_hwndSource.RootVisual);

            SetRootVisual(Child);

            return new HWND(_hwndSource.Handle);
        }
コード例 #19
0
 public static extern bool DestroyWindow(HWND hwnd);
コード例 #20
0
        private static IntPtr GetIEWindow(WebBrowser webBrowser)
        {
            HWND hwndIeWindow = HWND.NULL;

            HWND hwndShellEmbeddingWindow = new HWND(webBrowser.Handle);
            if (hwndShellEmbeddingWindow != HWND.NULL)
            {
                HWND hwndShellDocObjectView = NativeMethods.GetWindow(hwndShellEmbeddingWindow, GW.CHILD);
                if (hwndShellDocObjectView != HWND.NULL)
                {
                    hwndIeWindow = NativeMethods.GetWindow(hwndShellDocObjectView, GW.CHILD);
                }
            }

            return hwndIeWindow.DangerousGetHandle();
        }
コード例 #21
0
 public IEWindowHook(HWND hwnd)
     : base(hwnd)
 {
 }
コード例 #22
0
 private IntPtr WndProcStub(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, IntPtr id, IntPtr data)
 {
     HWND hwnd2 = new HWND(hwnd);
     return WndProc(hwnd2, (WM)msg, wParam, lParam, id, data);
 }
コード例 #23
0
        /// <summary>
        ///     Creates the hosted child window.
        /// </summary>
        /// <remarks>
        ///     Derived types override the BuildWindowCore virtual method to
        ///     create the child window.  The child window is parented to
        ///     a seperate top-level window for the purposes of redirection.
        ///     The SafeWindowHandle type controls the lifetime of the
        ///     child window.  It will be disposed when the RedirectedHwndHost
        ///     is disposed, or when the SafeWindowHandle is finalized.  Set
        ///     the SafeWindowHandle.DestroyWindowOnRelease property to true
        ///     if you want the window destroyed automatically.
        /// </remarks>
        protected virtual HWND BuildWindowCore(HWND hwndParent)
        {
            StrongHWND hwndChild = StrongHWND.CreateWindowEx(
                0,
                "STATIC",
                "Default RedirectedHwndHost Window",
                WS.CHILD | WS.CLIPSIBLINGS | WS.CLIPCHILDREN,
                0,
                0,
                0,
                0,
                hwndParent,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero);

            return hwndChild;
        }
コード例 #24
0
 public static extern HWND CreateWindowEx(WS_EX dwExStyle, string lpClassName, string lpWindowName, WS dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
コード例 #25
0
 public static extern IntPtr DefSubclassProc(HWND hwnd, WM msg, IntPtr wParam, IntPtr lParam);
コード例 #26
0
        /// <summary>
        ///     Called from WindowClass.CreateWindow to intialize this instance
        ///     when the HWND has been created.
        /// </summary>
        /// <param name="hwnd">
        ///     The HWND that was created.
        /// </param>
        /// <param name="param">
        ///     The creation parameter that was passsed to
        ///     WindowClass.CreateWindow.
        /// </param>
        internal IntPtr InitializeFromFirstMessage(IntPtr hwnd, int message, IntPtr wParam, IntPtr lParam)
        {
            _hwnd = new HWND(hwnd);

            // Replace the window proceedure for this window instance.
            IntPtr wndProc = Marshal.GetFunctionPointerForDelegate(_wndProc);
            NativeMethods.SetWindowLongPtr(_hwnd, GWL.WNDPROC, wndProc);

            // Give the window a chance to initialize.
            Initialize();

            // Manually invoke the window proceedure for this message.
            return OnMessage((WM) message, wParam, lParam);
        }
コード例 #27
0
        protected virtual void Dispose(bool disposing)
        {
            if(_hwnd == null || !IsCorrectThread(_hwnd))
            {
                throw new InvalidOperationException("Dispose virtual should only be called by WindowSubclass once on the correct thread.");
            }

            NativeMethods.RemoveWindowSubclass(_hwnd, _wndproc, IntPtr.Zero);
            _hwnd = null;
        }
コード例 #28
0
        public static StrongHWND CreateWindowEx(WS_EX dwExStyle, string lpClassName, string lpWindowName, WS dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam)
        {
            HWND hwnd = NativeMethods.CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);

            return new StrongHWND(hwnd.DangerousGetHandle());
        }
コード例 #29
0
        protected virtual void OnCurrentHwndSourceChanged(DependencyPropertyChangedEventArgs e)
        {
            Initialize();

            // Unregister the old keyboard input site.
            IKeyboardInputSite keyboardInputSite = ((IKeyboardInputSink)this).KeyboardInputSite;
            if (keyboardInputSite != null)
            {
                ((IKeyboardInputSink)this).KeyboardInputSite = null;
                keyboardInputSite.Unregister();
            }

            // Register the new keyboard input site with the containing
            // HwndSource.
            IKeyboardInputSink sink = CurrentHwndSource;
            if (sink != null)
            {
                ((IKeyboardInputSink)this).KeyboardInputSite = sink.RegisterKeyboardInputSink(this);
            }

            // Set the owner of the RedirectedWindow to our CurrentHwndSource.
            // This keeps the RedirectedWindow on top of the HwndSource.
            if (CurrentHwndSource != null)
            {
                HWND hwndSource = new HWND(CurrentHwndSource.Handle);
                HWND hwndRoot = hwndSource; // User32NativeMethods.GetAncestor(hwndSource, GA.ROOT); // need to get the top-level window?
                NativeMethods.SetWindowLongPtr(
                    _redirectedWindow.Handle,
                    GWL.HWNDPARENT,
                    hwndRoot.DangerousGetHandle());
            }
        }
コード例 #30
0
 private static extern IntPtr _SetWindowLongPtr(HWND hwnd, GWL nIndex, IntPtr dwNewLong);