Пример #1
0
        public DecoratorWindow()
        {
            DecoratorRoot = new DecoratorWindowRoot {
                Background = null
            };

            _onAutoResizeHandler = OnAutoResize;
            _dpiChangedHandler   = OnDpiChanged;
            _messageFilterHook   = MessageFilter;
        }
Пример #2
0
            private static void BuildWindow(
                object psh, int x, int y, Visual placementTarget,
                bool transparent, HwndSourceHook hook, AutoResizedEventHandler handler,
                HwndDpiChangedEventHandler dpiChangedHandler)
            {
                bool isChildPopup = (bool)pshIsChildPopupProperty.GetValue(psh) !;

                Debug.Assert(!isChildPopup || (isChildPopup && !transparent), "Child popups cannot be transparent");
                transparent = transparent && !isChildPopup;

                Visual mainTreeVisual = placementTarget;

                if (isChildPopup)
                {
                    // If the popup is nested inside other popups, get out into the main tree
                    // before querying for the presentation source.
                    mainTreeVisual = FindMainTreeVisual(placementTarget);
                }

                // get visual's PresentationSource
                var hwndSource = GetPresentationSource(mainTreeVisual) as HwndSource;

                // get parent handle
                IntPtr parent = IntPtr.Zero;

                if (hwndSource != null)
                {
                    parent = GetHandle(hwndSource);
                }

                int classStyle = 0;
                int style      = NativeMethods.WS_CLIPSIBLINGS;
                int styleEx    = NativeMethods.WS_EX_TOOLWINDOW | NativeMethods.WS_EX_NOACTIVATE;

                if (isChildPopup)
                {
                    // The popup was created in an environment where it should
                    // be a child window, not a popup window.
                    style |= NativeMethods.WS_CHILD;
                }
                else
                {
                    style   |= NativeMethods.WS_POPUP;
                    styleEx |= NativeMethods.WS_EX_TOPMOST;
                }

                // set window parameters
                var param = new HwndSourceParameters(string.Empty);

                param.WindowClassStyle    = classStyle;
                param.WindowStyle         = style;
                param.ExtendedWindowStyle = styleEx;
                param.SetPosition(x, y);

                (hook.Target as PopupEx)?.CreateParams(ref param);

                if (isChildPopup)
                {
                    if (parent != IntPtr.Zero)
                    {
                        param.ParentWindow = parent;
                    }
                }
                else
                {
                    param.UsesPerPixelOpacity = transparent;
                    if (parent != IntPtr.Zero && ConnectedToForegroundWindow(parent))
                    {
                        param.ParentWindow = parent;
                    }
                }

                // create popup's window object
                var newWindow = new HwndSource(param);

                // add hook to the popup's window
                newWindow.AddHook(hook);

                // initialize the private critical window object
                pshWindowField.SetValue(psh, securityCriticalDataClassOfWindowCtor.Invoke(
                                            new object[] { newWindow }));

                // Set background color
                var hwndTarget = newWindow.CompositionTarget;

                hwndTarget.BackgroundColor = transparent ? Colors.Transparent : Colors.Black;

                // add AddAutoResizedEventHandler event handler
                newWindow.AutoResized += handler;

                newWindow.DpiChanged += dpiChangedHandler;
            }