I have a WPF Window with WindowStyle set to none. Is there some way I can force this window to drop a shadow (like the one you get when WindowStyle is not none)? I don't want to set AllowTransparency to true, because it affects the performance. And I also don't want to disable hardware rendering (I read somewhere that transparency performs better with it disabled).
4 Answers
I have written a little utility class that is able to do exactly what you want: drop a standard shadow over a borderless Window
but having AllowsTransparency
set to false
.
You just have to call the DropShadowToWindow(Window window)
method. It is preferred that you make this call just after the window's constructor's InitializeComponent()
, but it will work even if you call it after the window is shown.
using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
public static class DwmDropShadow
{
[DllImport("dwmapi.dll", PreserveSig = true)]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);
/// <summary>
/// Drops a standard shadow to a WPF Window, even if the window is borderless. Only works with DWM (Windows Vista or newer).
/// This method is much more efficient than setting AllowsTransparency to true and using the DropShadow effect,
/// as AllowsTransparency involves a huge performance issue (hardware acceleration is turned off for all the window).
/// </summary>
/// <param name="window">Window to which the shadow will be applied</param>
public static void DropShadowToWindow(Window window)
{
if (!DropShadow(window))
{
window.SourceInitialized += new EventHandler(window_SourceInitialized);
}
}
private static void window_SourceInitialized(object sender, EventArgs e)
{
Window window = (Window)sender;
DropShadow(window);
window.SourceInitialized -= new EventHandler(window_SourceInitialized);
}
/// <summary>
/// The actual method that makes API calls to drop the shadow to the window
/// </summary>
/// <param name="window">Window to which the shadow will be applied</param>
/// <returns>True if the method succeeded, false if not</returns>
private static bool DropShadow(Window window)
{
try
{
WindowInteropHelper helper = new WindowInteropHelper(window);
int val = 2;
int ret1 = DwmSetWindowAttribute(helper.Handle, 2, ref val, 4);
if (ret1 == 0)
{
Margins m = new Margins { Bottom = 0, Left = 0, Right = 0, Top = 0 };
int ret2 = DwmExtendFrameIntoClientArea(helper.Handle, ref m);
return ret2 == 0;
}
else
{
return false;
}
}
catch (Exception ex)
{
// Probably dwmapi.dll not found (incompatible OS)
return false;
}
}
}

- 17,118
- 7
- 88
- 91
-
2This is great, except I'm getting a problem where opening a child window (also with a dropshadow) it reduces the dropshadow on the parent, and then when the childwindow is closed, removes it entirely. strange bug, not found whats causing it yet. It also seems to do it when childwindow is not using dropshadow) – Stephen Price Oct 18 '11 at 07:23
-
2Note: when I tried to use this approach in a native app, the shadow did not appear until I made the margins nonzero. (For a borderless window, actual values don't seem to matter as long as they are different from 0). – Nikita Nemkin Sep 06 '13 at 11:36
-
I wanted to add to this: DWM seems to do some image processing so you aren't guaranteed to have a drop shadow. Mainly, if your window's edges are dark, it'll give you a weak shadow (presumably because dark glows + dark rectangle is ugly). I tried setting background to white and covering that whiteness with a dark control, and it didn't help (presumably because they work with the flattened image of your window, rather than reading its background color). – Warty Oct 21 '13 at 05:25
-
2Although this approach is promising, there is another problem with it. If you switch away from your application and then back, the shadow is gone. – Gábor Nov 07 '13 at 13:16
-
Great! Works with windows 8.1 too! Any idea on how to increase the shadow spread (like how it looks in mac)? – Mangesh Nov 01 '14 at 11:58
-
That's good to know, I edited accordingly. To increase the shadow spread you will have to switch `AllowsTransparency` to `true` (which decrease performance) and use your own black alpha gradient in the borders of the window. – cprcrack Nov 01 '14 at 16:14
-
2Don't use `System.Drawing.Printing.Margins`, it causes strange graphics glitches on some machines. Instead define the struct locally as @Omer Ran suggested below. – Kelly Elton Jun 26 '17 at 17:55
-
Is it possible to adjust the drop shadow, for example using a hard shadow on two sides only? – The Muffin Man Oct 30 '17 at 02:40
-
I noticed that on some Windows 10 machines (that had the latest updates) that this code would cause the window to be extremely over saturated. Couldn't find out why it worked on some, but not others. – The Muffin Man Nov 26 '17 at 23:36
-
See Omer's answer below. Need to use Margins as a struct, not class. – The Muffin Man May 02 '19 at 01:23
Patrick's answer works great, except when a win32 window is hosted. When that happens, you notice that the hosted window is "washed out" (it looks like windows is applying the 'glass sheet' effect to the entire hosted window). This odd behavior is fixed when defining the structure locally, e.g.
[StructLayout(LayoutKind.Sequential)]
public struct Margins
{
public int Left;
public int Right;
public int Top;
public int Bottom;
}

- 121
- 2
- 3
-
2This is a really good point. It's also good to note that this problem is only reproducible on a small subset of machines, so it's particularly nasty. – Kelly Elton Jun 26 '17 at 17:53
-
So far I've noticed it on an Intel HD 520 graphics card, as well as other Intel HD cards on other laptops. – Kelly Elton Jun 26 '17 at 19:01
-
Also, if you convert `Margins` into `class`, then the problem comes back. It must be related to the fact that `System.Drawing.Printing` is also a `class`. – Kelly Elton Jun 26 '17 at 19:40
If you permit the window to have resize borders, by setting ResizeMode
to CanResize
, then you will get the OS drop shadow. You can then set the MaxWidth
, MinWidth
, MaxHeight
, and MinHeight
to values which will prevent the resize.
If you have a borderless window without a style you will have to provide all the appearance for the window in your own visual tree, including a drop shadow, since this combination of settings is the same as saying that you don't want what the OS provides.
EDIT:
From that point, if your window size is fixed, simply add the dropshadow, perhaps as a <Rectangle/>
as the first element in the content of a <Canvas/>
something like this:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" AllowsTransparency="True" Background="Transparent" WindowStyle="None">
<Canvas>
<Rectangle Fill="#33000000" Width="100" Height="100"/>
<Rectangle Fill="#FFFF0000" Width="95" Height="95" />
</Canvas>
</Window>
Note that the Fill
property of that first Rectangle
is partially transparent, which you could also do with the Opacity
property of the Rectangle
. You could use a graphic of your own or a different shape, to customize the appearance of the drop shadow.
Note that this violates your requirement to have AllowsTransparency
be False
, but you have no choice: if you want transparency, you have to allow it.

- 3,088
- 1
- 30
- 51
-
Regarding edit: I did try something like this, but it really kills performance. I'm doing this on Windows XP, dunno about Vista/7. – TripShock Aug 05 '10 at 13:50
-
You don't necessarily have to chose between setting `AllowsTransparency` to `False` and being able to drop a shadow. You can set `AllowsTranspareceny` to `True` just in 4 windows located on the edges of you main window, that will be in charge of dropping the shadow. In this way, you main window performance will be intact. – cprcrack Aug 19 '11 at 13:02
Why not just create the shadow with the same object as your "window" but bigger and behind it.
<Window x:Class="WPF_Custom_Look.ShadowWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ShadowWindow" Height="400" Width="450" ResizeMode="NoResize" Background="Transparent" AllowsTransparency="True" WindowStyle="None">
<Grid>
<Rectangle Fill="Black" Width="330" Opacity="0.5" Height="279">
<Rectangle.Effect>
<BlurEffect Radius="30"/>
</Rectangle.Effect>
</Rectangle>
<Rectangle Fill="#FFFDFDFD" Width="312" Height="260"/>
</Grid>
Or if you need a transparent title bar, it could be replaced by a <Border>
<Canvas>
<Border BorderBrush="Black" BorderThickness="7" Height="195" Width="304" Canvas.Left="53" Canvas.Top="25">
<Border.Effect>
<BlurEffect Radius="20"/>
</Border.Effect>
</Border>
<Rectangle Fill="#FF86B0F9" Width="285" Height="177" Opacity="0.7" Canvas.Left="62" Canvas.Top="34" MouseDown="Border_MouseDown"/>
<Rectangle Fill="#FFFDFDFD" Width="285" Height="143" Canvas.Left="62" Canvas.Top="68"/>
</Canvas>
Edit: I just noticed OP wants AllowsTransparency set to False. I can't see a shadow to work without it being "True", thouth.

- 1,852
- 26
- 29