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.
Are the transparent windows still rendered in software mode?
ha, you beat me to the punch. i second that question! :D
Schweet! Does it work in partial trust? ;)
Aaron,
I don’t think you can use this stuff in partial trust – you cannot create a new Window.
I’ll third the question about software vs. hardware.
Yipeeee!
Those five pixels non-transparent in each corner of my app annoy me (see at http://channel9.msdn.com/ShowPost.aspx?PostID=204528#204528). But please don’t kill hardware rendering. I’m not sure I can justify a performance hit for twenty pixels you can hardly see!
Sorry about the delay in responding to your questions about HW acceleration. I actually don’t know whether or not HW acceleration for layered windows made it into the June CTP. I’m hunting around for a build of Perforator that matches the June CTP so that I can check, but I’m out of the office until Wednesday 7/5, so I likely will not have an answer until then.
I have tried your sample using Perforator and it seems that it is hardware rendering. Good job!
I have the June CTP on XP but I have no AllowTransparency property and if I add it to the xaml is just throws an error.
any advice?
Valentin,
Thanks for checking into HW rendering!
Simon,
What kind of error are you seeing? Does your app compile?
However, as Nick Kramer pointed out a while back, HwndHost is not supported with layered windows which means that a element in the window that is navigated to a non-XAML Uri (ex: http://www.microsoft.com) is rendered fully transparent or invisible/blank. So, this AllowsTransparency=true feature is currently useless for any application that has support for loading external web content into a frame element in the XAML construction.
An important note from Nick Kramer’s blog:
“Windows Vista will support hardware accelerated layered windows, but we’re not sure about Windows XP… it’s possible we’ll need to pull support for layered window acceleration on XP”
[http://blogs.msdn.com/nickkramer/archive/2006/07/08/659656.aspx]
I also have the June CTP on XP and when using the AllowTransparency property I get an error.
Is allow transparency only supported on Vista?
Use AllowsTransparency, not AllowTransparency :)
On Windows Server 2003 it works fine :)
Thanks for catching that, Pavel. :) The property is indeed called AllowsTransparency, not AllowTransparency.
Have anybody notice my question on this topic? Here it is. http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=534864&SiteID=1
Leo,
Thanks for the heads up. I just responded to your forum question.
Hi! I have the Jun CTP and I can’t find the AllowsTransparency property :(. Here is a screenshot of my Visual Studio’s Intellisence –> http://www.subir-imagenes.com/subir-fotos/3a34d75f69.jpg
Damian,
If you try typing in the property name manually (without Intellisense), does your app compile? My guess is that this is just a bug in our Intellisense, and you should still be able to use the property. If your app does not build, please let me know.
Yes the app compile perfectly. But… in my PC with Windows Vista (5456) the transparency effect works, in other computer with the same version of Windows Vista, the same video card, the same video drivers, the Jun Runtimes, SDK, etc, when I start the application this doesn’t appear :S
If I disable aero and then I enabled it again, the application it works perfectly. Can you help me with that please? .
PD: Sorry about my english, I’m from Argentina.-
Rounded corners are cool but, can it take a generic shape object to define the outer shape of the window? Also, can the transparency be given a gradient or topographic contour? That would be cool! :) (forgive me if WPF can already do all of this… I’m relatively new to it)
-Brian A.
Is WindowStyle going to ultimately allow for the full set of possible styles? I’m trying to add a transparency slider to some windows in my app but the windows are really just supposed to be title bar & content, ala mIRC…nothing fancy.
Keith,
I’ll pass your feedback along to the team to consider for v2. If you could give me any more details — e.g. the analogous Win32 style that you are trying to emulate, or some screen shots of what you are trying to emulate, that would be very helpful.
I’m really just going for the effect here: http://www.ishiboo.com/~nirva/Projects/froost/transparent.gif, with or without the title bar being transparent. For now, I think I’m going to draw my own title bar/border, although I have to acquaint myself with the new scheme for referencing system settings to duplicate as much of the current XP look as possible & then hook in some drag/drop & context menu.
What about changing the AllowsTransparency property at runtime? Currently there is an exception thrown stating that exact thing is not possible. This might be an issue for skinning engines that might want to switch between two different window types…
I have the September CTP of .net 3.0 installed on an older XP SP2 machine and a newer Vista RC1 machine. I’m using multiple windows that have AllowsTransparency=”true”. They run fine and DragMove works, except on the Vista machine transformations and storyboards run very very slow. I hope this will be fixed in later builds but for the meantime is there a temporary fix without me having to create “fake” mdi-style children inside one window as oppose to creating multiple real windows with transparency? Thanks.
Can I do the button not transparent?
Luiz,
You should be able to set the Button to non-transparent by setting its Opacity.
I have an xbap application which is launching a WPF window with rounded corners. If i apply some tooltips or try to open some kind of popup windows inside this window, It crashes giving a message “Cannot create top level child windows” But if i remove the AllowsTransparency property of the WPF window then it works perfectly, But then i wont be getting Rounded corners. But i want to do both? Please help me!!
(by the way no such issues in Desktop application)
Hi, I am using WPF Sept CTP on XP xp2, and as suggested by you, transperancy thing works gr8, but it is not glass effect ( the vista one ) I have seen some apps (http://www.launchy.net/) which seem to give that effect in dot net 2.0 so how abt getting those same effects in WPF for XPsp2
For vista we can do it this way : http://msdn2.microsoft.com/en-us/library/ms748975.aspx but what about XPsp2?
I looked it up on internet and found http://blogs.msdn.com/unnir/archive/2006/03/01/541154.aspx
however the code does not compile.. at least on my machine.. it complains about some baml file which is not found.