If you come from a Windows Forms background, you must have heard of shaped Windows Forms or non-rectangular Windows Forms. A non-rectangular Windows Forms has a user interface that is not a typical rectangular Window you see in Windows user interfaces. The window can be of any shape such as a drawing, a fruit, a stereo player and so on.

In WPF, there is no concept of Windows Forms. Every user interface in WPF is represented by a Window. In this article, you will learn how to create non-rectangular shaped Windows in WPF.

In Windows Forms, to create non-rectangular shaped Forms, we used to create a Path and set the Path property of a Path.

Creating non-rectangular interfaces in WPF is actually simpler than in Windows Forms. We just have to set the AllowsTransparency property of a Window to True and the Background to Transparent. After that whatever you place on that Window does not have a typical Window background.

  1. <Window x:Class="NonRectWindowSample.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Window1"
  5. AllowsTransparency="True"
  6. Background="Transparent">
  7. </Window>

So let's say we need to create a user interface that looks like Figure 1. We simply have to create a Path with the similar UI and place it on a transparent Window. That will do the trick.

Shaped Windows In WPF
Figure 1

The complete code of the Window in XAML looks like Listing 1.

  1. <Window x:Class="NonRectWindowSample.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Window1" SizeToContent="WidthAndHeight"
  5. MouseLeftButtonDown="Window_MouseLeftButtonDown"
  6. WindowStyle="None"
  7. AllowsTransparency="True"
  8. Background="Transparent">
  9. <Canvas Width="400" Height="400" Name="RootLayout" >
  10. <Path Stroke="Gray" StrokeThickness="2" Name="UIPath" >
  11. <Path.Fill>
  12. <LinearGradientBrush StartPoint="0.2,0" EndPoint="0.8,1" >
  13. <GradientStop Color="Green" Offset="0" />
  14. <GradientStop Color="Yellow" Offset="0.35" />
  15. <GradientStop Color="Orange" Offset="0.65" />
  16. <GradientStop Color="Red" Offset="0.85" />
  17. </LinearGradientBrush>
  18. </Path.Fill>
  19. <Path.Data>
  20. <PathGeometry >
  21. <PathFigure StartPoint="50,100">
  22. <ArcSegment Size="150,150" RotationAngle="45" IsLargeArc="True"
  23. SweepDirection="CounterClockwise" Point="100,50" />
  24. <LineSegment Point="20,20"/>
  25. </PathFigure>
  26. </PathGeometry>
  27. </Path.Data>
  28. </Path>
  29. <Label Width="226" Height="68" FontSize="20" FontFamily="Georgia" FontWeight="Bold"
  30. HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
  31. Canvas.Left="60" Canvas.Top="127"
  32. Foreground="Blue" >
  33. Drag Me and Watch!
  34. </Label>
  35. <Button Canvas.Left="206" Canvas.Top="42" Height="0" Width="0"
  36. ToolTip="Click me to close the form." Name="CloseButton" Click="CloseButton_Click">
  37. <Button.Template>
  38. <ControlTemplate>
  39. <Canvas>
  40. <Rectangle Width="20" Height="20" Stroke="DarkBlue" RadiusX="2" RadiusY="2">
  41. <Rectangle.Fill>
  42. <SolidColorBrush x:Name="myAnimatedBrush" Color="Blue" />
  43. </Rectangle.Fill>
  44. </Rectangle>
  45. <Line X1="3" Y1="3" X2="17" Y2="17" Stroke="White" StrokeThickness="2"></Line>
  46. <Line X1="17" Y1="3" X2="3" Y2="17" Stroke="White" StrokeThickness="2"></Line>
  47. </Canvas>
  48. </ControlTemplate>
  49. </Button.Template>
  50. </Button>
  51. <Button Canvas.Left="131" Canvas.Top="272" Height="30" Name="BlackNWhiteButton" Width="112"
  52. Foreground="White" Background="Crimson" Click="BlackNWhiteButton_Click" FontWeight="Bold">
  53. Black and White
  54. </Button>
  55. </Canvas>
  56. </Window>

Listing 1

We also write code listed in Listing 2 for the left mouse button click event handler and call DragMove method of the Window that is responsible for moving a Window position when a Window is dragged. The Close button click event handler simply closes the Window.

On the Black and White button click event handler, we change the background of the Window to a black and white color gradient.

  1. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
  2. this.DragMove();
  3. }
  4. private void CloseButton_Click(object sender, RoutedEventArgs e) {
  5. this.Close();
  6. }
  7. private void BlackNWhiteButton_Click(object sender, RoutedEventArgs e) {
  8. // Create a linear gradient brush with five stops
  9. LinearGradientBrush blacknwhiteBrush = new LinearGradientBrush();
  10. blacknwhiteBrush.StartPoint = new Point(0, 0);
  11. blacknwhiteBrush.EndPoint = new Point(1, 1);
  12. // Create and add Gradient stops
  13. GradientStop blackGS = new GradientStop();
  14. blackGS.Color = Colors.Black;
  15. blackGS.Offset = 0.0;
  16. blacknwhiteBrush.GradientStops.Add(blackGS);
  17. GradientStop whiteGS = new GradientStop();
  18. whiteGS.Color = Colors.White;
  19. whiteGS.Offset = 1.0;
  20. blacknwhiteBrush.GradientStops.Add(whiteGS);
  21. UIPath.Fill = blacknwhiteBrush;
  22. }

Listing 2

Clicking the Black and White button changes the Window that looks like Figure 2,

Shaped Windows In WPF
Figure 2