The June CTP of .Net Framework 3.0 was released on Friday. One new June CTP feature that I am very excited to introduce is a new property on Window called AllowsTransparency. AllowsTransparency enables you to create those semi-transparent windows and rounded-corner windows for which many of you have been asking.
I have written a small sample to illustrate some of the things to think about when creating semi-transparent windows. The central part of the sample is the following XAML:
<Window x:Class="WindowTransparency.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowTransparency" Height="300" Width="300" AllowsTransparency="True" WindowStyle="None" Background="Transparent" > <Border CornerRadius="50" Background="Blue" Opacity=".7"> <Button Click="CloseWindow" Height="30" Width="100">Close Me </Button> </Border> </Window>
There are a few things to notice in this XAML. First is that I set Window.AllowsTransparency
to true. Of course, as the name implies, this simply means that the Window may be made transparent (under the covers, it switches to using a layered window), it does not actually set the background to transparent. To achieve a transparent window, I set Window.Background
to Transparent
. Now that I have a transparent window surface, I can draw whatever content inside of it that I like. In this example, I’m simply using a Border with rounded corners to show that you can create rounded-corner windows and dialogs.
You’ll also notice that I have set Window.WindowStyle
to None
. For now, Windows with AllowsTransparency=true
are not compatible with any of the other WindowStyles, and we expect that this will be fine for most scenarios (if you are using a transparent window, you probably don’t want the standard Win32 window chrome). Drawing your own Window chrome is easy (as an example, I’ve created a close button that I’ve hooked up to Window.Close()). Making your Window user-draggable is actually just as easy to do. The following code does it:
public Window1() { InitializeComponent(); MouseLeftButtonDown += new MouseButtonEventHandler (Window1_MouseLeftButtonDown); } void Window1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DragMove(); }
You can download the full sample here.