Exemplo n.º 1
0
        private static WindowExtensions _EnsureAttachedExtensions(Window window)
        {
            Assert.IsNotNull(window);

            var ext = (WindowExtensions)window.GetValue(WindowExtensionsProperty);

            if (ext == null)
            {
                ext = new WindowExtensions(window);
                window.SetValue(WindowExtensionsProperty, ext);
            }

            return(ext);
        }
Exemplo n.º 2
0
        private static void _OnHwndBackgroundBrushChanged(DependencyObject d)
        {
            if (DesignerProperties.GetIsInDesignMode(d))
            {
                return;
            }

            var window = d as Window;

            Verify.IsNotNull(window, "window");

            WindowExtensions ext = _EnsureAttachedExtensions(window);

            if (ext._hwnd == IntPtr.Zero)
            {
                ext.WindowSourceInitialized += () => _OnHwndBackgroundBrushChanged(window);
                return;
            }

            SolidColorBrush backgroundBrush = (SolidColorBrush)window.GetValue(HwndBackgroundBrushProperty);
            //if (backgroundBrush == null)
            //{
            // Nothing to change.
            //    return;
            //}

            Color backgroundColor = backgroundBrush.Color;

            // Not really handling errors here, but they shouldn't matter... Might leak an HBRUSH.

            IntPtr hBrush = NativeMethods.CreateSolidBrush(Utility.RGB(backgroundColor));

            // Note that setting this doesn't necessarily repaint the window right away.
            // Since the WPF content should cover the HWND background this doesn't matter.
            // The new background will get repainted when the window is resized.
            IntPtr hBrushOld = NativeMethods.SetClassLongPtr(ext._hwnd, GCLP.HBRBACKGROUND, hBrush);

            if (IntPtr.Zero != hBrushOld)
            {
                NativeMethods.DeleteObject(hBrushOld);
            }
        }