I’m working on a WPF application and came across something that was hair-pulling. I couldn’t find anything on google about it and thought to share so others wouldn’t have to go through the pain.
In this application, the main window needs custom chrome (min, max, restore buttons, resize borders, etc). So I set about giving the WindowStyle a “None” value and the ResizeMode property to “NoResize”. Then I noticed that you lose the drop shadow the DWM provides! Ok, so naturally I just wanted to do an AllowsTransparency = True and add my own drop shadow…but AllowsTransparency has such atrocious performance (sad, huh?), it wasn’t an option for me.
Took a lot guess and checking, but here’s a snippet to get back your drop shadow on your window w/o sacrificing performance:
Image without code:
Image with code:
[StructLayout(LayoutKind.Sequential)]
public struct Margins
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
[DllImport("dwmapi.dll", PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);
void ShellWindow_SourceInitialized(object sender, EventArgs e)
{
var helper = new WindowInteropHelper(this);
int val = 2;
DwmSetWindowAttribute(helper.Handle, 2, ref val, 4);
var m = new Margins { bottomHeight = -1, leftWidth = -1, rightWidth = -1, topHeight = -1 };
DwmExtendFrameIntoClientArea(helper.Handle, ref m);
}
There you go again with all your interop voodoo! I’m actually surprised that WindowStyle none doesn’t support bitmap effects. Anyway keep up with the mad science!
--Mike
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Jeremiah Morrill
Sent: Tuesday, August 03, 2010 1:39 AM
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] OS Composed DropShadows on WPF Windows with Custom Chrome
I’m working on a WPF application and came across something that was hair-pulling. I couldn’t find anything on google about it and thought to share so others wouldn’t have to go through the pain.