Пример #1
0
        private void BuildWindow(HandleRef hwndParent)
        {
            this.DemandIfUntrusted();
            this._hwnd = this.BuildWindowCore(hwndParent);
            if (this._hwnd.Handle == IntPtr.Zero || !UnsafeNativeMethods.IsWindow(this._hwnd))
            {
                throw new InvalidOperationException(SR.Get("ChildWindowNotCreated"));
            }
            int windowLong = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, this._hwnd.Handle), -16);

            if ((windowLong & 1073741824) == 0)
            {
                throw new InvalidOperationException(SR.Get("HostedWindowMustBeAChildWindow"));
            }
            if (hwndParent.Handle != UnsafeNativeMethods.GetParent(this._hwnd))
            {
                throw new InvalidOperationException(SR.Get("ChildWindowMustHaveCorrectParent"));
            }
            if (DpiUtil.GetDpiAwarenessContext(this._hwnd.Handle) != DpiUtil.GetDpiAwarenessContext(hwndParent.Handle))
            {
                this._hasDpiAwarenessContextTransition = true;
            }
            int num;
            int windowThreadProcessId = UnsafeNativeMethods.GetWindowThreadProcessId(this._hwnd, out num);

            if (windowThreadProcessId == SafeNativeMethods.GetCurrentThreadId() && num == SafeNativeMethods.GetCurrentProcessId())
            {
                this._hwndSubclass = new HwndSubclass(this._hwndSubclassHook);
                this._hwndSubclass.CriticalAttach(this._hwnd.Handle);
            }
            UnsafeNativeMethods.ShowWindowAsync(this._hwnd, 0);
            NativeMethods.RECT rect = default(NativeMethods.RECT);
            SafeNativeMethods.GetWindowRect(this._hwnd, ref rect);
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(this, false);
            Point point  = new Point((double)rect.left, (double)rect.top);
            Point point2 = new Point((double)rect.right, (double)rect.bottom);

            point             = presentationSource.CompositionTarget.TransformFromDevice.Transform(point);
            point2            = presentationSource.CompositionTarget.TransformFromDevice.Transform(point2);
            this._desiredSize = new Size(point2.X - point.X, point2.Y - point.Y);
            base.InvalidateMeasure();
        }
Пример #2
0
        private void BuildWindow(HandleRef hwndParent)
        {
            // Demand unmanaged code to the caller. IT'S RISKY TO REMOVE THIS
            DemandIfUntrusted();

            // Allow the derived class to build our HWND.
            _hwnd = BuildWindowCore(hwndParent);

            if (_hwnd.Handle == IntPtr.Zero || !UnsafeNativeMethods.IsWindow(_hwnd))
            {
                throw new InvalidOperationException(SR.Get(SRID.ChildWindowNotCreated));
            }

            // Make sure that the window that was created is indeed a child window.
            int windowStyle = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, _hwnd.Handle), NativeMethods.GWL_STYLE);

            if ((windowStyle & NativeMethods.WS_CHILD) == 0)
            {
                throw new InvalidOperationException(SR.Get(SRID.HostedWindowMustBeAChildWindow));
            }

            // Make sure the child window is the child of the expected parent window.
            if (hwndParent.Handle != UnsafeNativeMethods.GetParent(_hwnd))
            {
                throw new InvalidOperationException(SR.Get(SRID.ChildWindowMustHaveCorrectParent));
            }

            // Test to see if hwndParent and _hwnd have different DPI_AWARENESS_CONTEXT's
            if (DpiUtil.GetDpiAwarenessContext(_hwnd.Handle) != DpiUtil.GetDpiAwarenessContext(hwndParent.Handle))
            {
                _hasDpiAwarenessContextTransition = true;
            }

            // Only subclass the child HWND if it is owned by our thread.
            int idWindowProcess;
            int idWindowThread = UnsafeNativeMethods.GetWindowThreadProcessId(_hwnd, out idWindowProcess);

#if WCP_SERVER2003_OR_LATER_ENABLED
            IntPtr hCurrentThread = UnsafeNativeMethods.GetCurrentThread();
            if ((idWindowThread == SafeNativeMethods.GetThreadId(hCurrentThread)) &&
                (idWindowProcess == UnsafeNativeMethods.GetProcessIdOfThread(hCurrentThread)))
#else
            if ((idWindowThread == SafeNativeMethods.GetCurrentThreadId()) &&
                (idWindowProcess == SafeNativeMethods.GetCurrentProcessId()))
#endif
            {
                _hwndSubclass = new HwndSubclass(_hwndSubclassHook);
                _hwndSubclass.CriticalAttach(_hwnd.Handle);
            }

            // Initially make sure the window is hidden.  We will show it later during rendering.
            UnsafeNativeMethods.ShowWindowAsync(_hwnd, NativeMethods.SW_HIDE);

            // Assume the desired size is the initial size.  If the window was
            // created with a 0-length dimension, we assume this means we
            // should fill all available space.
            NativeMethods.RECT rc = new NativeMethods.RECT();
            SafeNativeMethods.GetWindowRect(_hwnd, ref rc);

            // Convert from pixels to measure units.
            // PresentationSource can't be null if we get here.
            PresentationSource source = PresentationSource.CriticalFromVisual(this, false /* enable2DTo3DTransition */);
            Point ptUpperLeft         = new Point(rc.left, rc.top);
            Point ptLowerRight        = new Point(rc.right, rc.bottom);
            ptUpperLeft  = source.CompositionTarget.TransformFromDevice.Transform(ptUpperLeft);
            ptLowerRight = source.CompositionTarget.TransformFromDevice.Transform(ptLowerRight);
            _desiredSize = new Size(ptLowerRight.X - ptUpperLeft.X, ptLowerRight.Y - ptUpperLeft.Y);

            // We have a new desired size, so invalidate measure.
            InvalidateMeasure();
        }