Anybody using DwmExtendFrameIntoClientArea?
-
Just using the stock example from Microsoft:
void MainWindow\_Loaded(object sender, RoutedEventArgs e) { IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle; HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr); //mainWindowSrc.CompositionTarget.RenderMode = RenderMode.SoftwareOnly; // <=== UNCOMMENT THIS LINE mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0); // Get System Dpi System.Drawing.Graphics desktop = System.Drawing.Graphics.FromHwnd(mainWindowPtr); float DesktopDpiX = desktop.DpiX; float DesktopDpiY = desktop.DpiY; // Set Margins MARGINS margins = new MARGINS(); // Extend glass frame into client area // Note that the default desktop Dpi is 96dpi. The margins are // adjusted for the system Dpi. margins.cxLeftWidth = Convert.ToInt32(5 \* (DesktopDpiX / 96)); margins.cxRightWidth = Convert.ToInt32(5 \* (DesktopDpiX / 96)); margins.cyTopHeight = Convert.ToInt32(((int)90) \* (DesktopDpiX / 96)); margins.cyBottomHeight = Convert.ToInt32(5 \* (DesktopDpiX / 96)); int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); // if (hr < 0) { //DwmExtendFrameIntoClientArea Failed } }
On my machine, I get a white rectangle over the entire window for about 1 second and then the window redraws properly. This code works on .NET 4.5.1, but this seems to have cropped up in .NET 4.5.2.
-
Just using the stock example from Microsoft:
void MainWindow\_Loaded(object sender, RoutedEventArgs e) { IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle; HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr); //mainWindowSrc.CompositionTarget.RenderMode = RenderMode.SoftwareOnly; // <=== UNCOMMENT THIS LINE mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0); // Get System Dpi System.Drawing.Graphics desktop = System.Drawing.Graphics.FromHwnd(mainWindowPtr); float DesktopDpiX = desktop.DpiX; float DesktopDpiY = desktop.DpiY; // Set Margins MARGINS margins = new MARGINS(); // Extend glass frame into client area // Note that the default desktop Dpi is 96dpi. The margins are // adjusted for the system Dpi. margins.cxLeftWidth = Convert.ToInt32(5 \* (DesktopDpiX / 96)); margins.cxRightWidth = Convert.ToInt32(5 \* (DesktopDpiX / 96)); margins.cyTopHeight = Convert.ToInt32(((int)90) \* (DesktopDpiX / 96)); margins.cyBottomHeight = Convert.ToInt32(5 \* (DesktopDpiX / 96)); int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); // if (hr < 0) { //DwmExtendFrameIntoClientArea Failed } }
On my machine, I get a white rectangle over the entire window for about 1 second and then the window redraws properly. This code works on .NET 4.5.1, but this seems to have cropped up in .NET 4.5.2.
Try calling the code from the window's
SourceInitialized
event[^], which should fire earlier than theLoaded
event. You might also need to callDwmEnableBlurBehindWindow
to reset the blur effect for the window. Here's the code I use: https://gist.github.com/RichardD2/ef4ee339ce3dcc10264b[^] That lets you enable the glass effect and set up the thickness using two attached properties. There's an additional real-only attached property which indicates whether the glass effect has been applied, which you can use to adapt your styles where necessary.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Try calling the code from the window's
SourceInitialized
event[^], which should fire earlier than theLoaded
event. You might also need to callDwmEnableBlurBehindWindow
to reset the blur effect for the window. Here's the code I use: https://gist.github.com/RichardD2/ef4ee339ce3dcc10264b[^] That lets you enable the glass effect and set up the thickness using two attached properties. There's an additional real-only attached property which indicates whether the glass effect has been applied, which you can use to adapt your styles where necessary.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Tried moving it to SourceInitialized and get the same result. 4.5.1 works perfectly and 4.5.2 has the 1 - 2 second start up lag. This isn't even a question of framework targeting, just if you have > 4.5.1 installed on the machine, it happens.