Shaped Windows in WPF
In previous versions of Microsoft .Net Framework, creating shaped windows was a time-consuming process that involved Win API calls.Not anymore – one of the modern aspects of WPF is that you can make non-rectangular windows in simple way.Creating shaped forms works in the following manner.First, you create an image, which, when we are finished, will act as your window. Second, you change a XAML to make this image become the form.
- Create a transparent image file that will be used to set the desired shape of the window.
- Create a Windows Application(WPF) project and add the image control to it.Remember to make the window transparent as shown in code below
<Window x:Class="WindowsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowsApplication1" Height="300" Width="300"
AllowsTransparency="True"
WindowStyle="None"
Background="Transparent"
>
<Grid>
<Image Name="image1" />
</Grid>
</Window>
- Add code that recreates the functionality that the title bar provided, such as moving the window and closing it.
using System;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace WindowsApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
image1.Source = new BitmapImage(
new Uri(@"C:\Documents and Settings\username\Desktop\Untitled.png"));
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
DragMove();
}
protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
{
base.OnMouseRightButtonDown(e);
Close();
}
}
}
Technorati Tags: WPF, XAML, dotnet