0
votes

I need to make a per pixel transparent window control in WPF, however I need to keep the aero drop shadow and part of the glass border.

What I mean is, look at a standard aero window. I want to keep the drop shadow all around, the rounded corners, get rid of the title bar and title bar controls. I want to keep the glass frame, but only make it a couple pixels thick (keeping the current rounded radius corners) and then I want to make the background of the window a per pixel transparent image. LOL... I know... a lot to ask.

I've got the per pixel transparent image worked out, but as soon as I AllowTransparency=True, Windows turns off the drop shadows :(.

1

1 Answers

2
votes

To make the window transparent, you can set the GlassFrameThickness to -1. Add the following style to your window.

<Window.Style>
    <Style TargetType="Window">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome GlassFrameThickness="-1"/>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Style>

If I'm understanding correctly and you also want to hide the window buttons (min, max, close) then create the following WindowsExtension and register to the SourceInitialized event.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        SourceInitialized += (x, y) => this.HideWindowButtons();
    }
}

internal static class WindowExtensions
{
    private const int GWL_STYLE = -16;
    private const int WIN_MAXIMIZE = 0x10000;
    private const int WIN_MINIMIZE = 0x20000;
    private const int WIN_CLOSE = 0x80000;

    [DllImport("user32.dll")]
    extern private static int GetWindowLong(IntPtr hwnd, int index);

    [DllImport("user32.dll")]
    extern private static int SetWindowLong(IntPtr hwnd, int index, int value);

    internal static void HideWindowButtons(this Window window)
    {
        var hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
        var currentStyle = GetWindowLong(hwnd, GWL_STYLE);

        SetWindowLong(hwnd, GWL_STYLE, (currentStyle & ~WIN_MINIMIZE & ~WIN_MAXIMIZE & ~WIN_CLOSE));
    }
}

I wish I could remember where I found that extension example to give the guy some credit. If you plan to override the control template to add a thicker border, it looks to be about a CornerRadius of 8 to match the aero border. Anyway, that should get you started. Hopefully that is what you were after.