Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropShadow for WPF Borderless Window

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).

like image 836
TripShock Avatar asked Jul 30 '10 14:07

TripShock


3 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;
        }
    }
}
like image 165
cprcrack Avatar answered Nov 04 '22 04:11

cprcrack


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;
}  
like image 27
Omer Ran Avatar answered Nov 04 '22 02:11

Omer Ran


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.

like image 3
Rob Perkins Avatar answered Nov 04 '22 03:11

Rob Perkins