DropShadow para Ventana sin bordes de WPF


Tengo una ventana WPF con WindowStyle establecido en none. ¿Hay alguna forma en que pueda forzar esta ventana a dejar caer una sombra (como la que obtienes cuando WindowStyle no es ninguno)? No quiero establecer AllowTransparency en true, porque afecta el rendimiento. Y tampoco quiero deshabilitar el renderizado por hardware (leí en alguna parte que la transparencia funciona mejor si está deshabilitada).

Author: TripShock, 2010-07-30

5 answers

He escrito una pequeña clase de utilidad que es capaz de hacer exactamente lo que quieres: colocar una sombra estándar sobre un Window sin bordes pero teniendo AllowsTransparency establecido en false.

Solo tienes que llamar al método DropShadowToWindow(Window window). Es preferible que realice esta llamada justo después del constructor de la ventana InitializeComponent(), pero funcionará incluso si la llama después de que se muestre la ventana.

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;
        }
    }
}
 31
Author: cprcrack,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-16 13:30:03

Use la Biblioteca de integración de Shell de Microsoft WPF , más fácil y con mejor rendimiento

 5
Author: Eduardo Molteni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-15 00:17:33

La respuesta de Patrick funciona muy bien, excepto cuando se aloja una ventana de win32. Cuando eso sucede, te das cuenta de que la ventana alojada está "lavada" (parece que windows está aplicando el efecto "hoja de vidrio" a toda la ventana alojada). Este comportamiento impar se fija al definir la estructura localmente, por ejemplo,

[StructLayout(LayoutKind.Sequential)]
public struct Margins
{
    public int Left;
    public int Right;
    public int Top;
    public int Bottom;
}  
 5
Author: Omer Ran,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-14 16:18:47

Si permite que la ventana tenga bordes de redimensionamiento, configurando ResizeMode a CanResize, entonces obtendrá la sombra paralela del sistema operativo. A continuación, puede establecer el MaxWidth, MinWidth, MaxHeight, y MinHeight a valores que evitarán el redimensionamiento.

Si tienes una ventana sin bordes sin estilo tendrás que proporcionar toda la apariencia de la ventana en tu propio árbol visual, incluyendo una sombra paralela, ya que esta combinación de ajustes es lo mismo que decir que no quieres lo que proporciona el sistema operativo.

EDITAR:

Desde ese punto, si el tamaño de su ventana es fijo, simplemente agregue el dropshadow, tal vez como un <Rectangle/> como el primer elemento en el contenido de un <Canvas/>

Algo como esto:

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

Tenga en cuenta que la propiedad Fill de ese primer Rectangle es parcialmente transparente, lo que también podría hacer con la propiedad Opacity de Rectangle. Puede utilizar un gráfico propio o una forma diferente, para personalizar la apariencia de la sombra paralela.

Tenga en cuenta que esto viola tu requisito de tener AllowsTransparency ser False, pero no tienes opción: si quieres transparencia, tienes que permitirla.

 3
Author: Rob Perkins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-04 19:21:43

¿Por qué no simplemente crear la sombra con el mismo objeto que su "ventana", pero más grande y detrás de ella.

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

O si necesita una barra de título transparente, podría ser reemplazada por una <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>

Editar: Acabo de notar que OP quiere AllowsTransparency establecido en False. No puedo ver una sombra que funcione sin que sea "Verdadera", thouth.

 0
Author: Carol,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-23 21:06:47