Search...

Wednesday, September 25, 2013

Create Custom Windows in WPF with DropShadowEffect.

In this article, I am demonstrating how to create a custom window with with DropShadowEffect, In some situations, you need to change your window ‘Standard’ view and make it ‘Rounded’.

Lets do it. It takes few lines of code and its very easy to get clear. First, you need to hide current window styles.

Set WindowStyle to none, Background to transparent and AllowsTransparency to true. It is always better to create a visual template.
One more thing to note is that while defining the window template, you must declare theContentPresenter(which ultimately contains window content) within AdornerDecorator tag (which is the adorner layer for the window) as this is a WPF requirement.

Here is the template I have created. You can download the full source code here.


<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
        <Setter Property="FontSize" Value="12" />
        <Setter Property="ShowActivated" Value="True"/>
        <Setter Property="WindowState" Value="Normal"/>
        <Setter Property="ResizeMode" Value="NoResize" />
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="AllowsTransparency" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border BorderBrush="Transparent" BorderThickness="0,0,1,1" CornerRadius="0,0,10,10">
                        <Grid MouseLeftButtonDown="Window_MouseLeftButtonDown" Margin="0,0,10,10">
                            <Border BorderThickness="1" Background="White" BorderBrush="{DynamicResource WinBorderBrush}">
                                <DockPanel LastChildFill="True">
                                    <Border x:Name="PART_TITLEBAR" 
                                        Margin="0" 
                                        Height="30" 
                                        DockPanel.Dock="Top" 
                                        Background="Transparent">
                                        <DockPanel LastChildFill="False" Background="{DynamicResource WinBackground}" Margin="0">
                                            <Image Source="{TemplateBinding Icon}" Height="20" Width="20" Margin="5,0"/>
                                            <TextBlock Padding="8,0,0,4" 
                                                   Margin="0"
                                                   VerticalAlignment="Center" 
                                                   FontStretch="UltraExpanded" 
                                                   Foreground="Black" 
                                                   TextTrimming="CharacterEllipsis" 
                                                   TextWrapping="NoWrap" 
                                                   Text="{TemplateBinding Title}"
                                                   FontWeight="DemiBold"
                                                   FontSize="16"/>
                                            <Button x:Name="PART_CLOSE" 
                                                DockPanel.Dock="Right" 
                                                ToolTip="Close"
                                                Click="PART_CLOSE_Click"
                                                Style="{DynamicResource WinButton}">
                                                <Image Source="../Images/close.png"/>
                                            </Button>
                                            <Button x:Name="PART_MAXIMIZE_RESTORE" 
                                                DockPanel.Dock="Right"
                                                ToolTip="Maximize"
                                                Click="PART_MAXIMIZE_RESTORE_Click"
                                                Style="{DynamicResource WinButton}">
                                                <Image Source="../Images/max.ico"/>
                                            </Button>
                                            <Button x:Name="PART_MINIMIZE" 
                                                DockPanel.Dock="Right"
                                                ToolTip="Minimize"
                                                Click="PART_MINIMIZE_Click"
                                                Style="{DynamicResource WinButton}">
                                                <Image Source="../Images/min.png"/>
                                            </Button>
                                        </DockPanel>
                                    </Border>

                                    <AdornerDecorator DockPanel.Dock="Bottom">
                                        <ContentPresenter Content="{TemplateBinding Content}" Margin="{TemplateBinding Margin}" DataContext="{TemplateBinding DataContext}" ContextMenu="{TemplateBinding ContextMenu}"/>
                                    </AdornerDecorator>

                                </DockPanel>
                            </Border>
                        </Grid>
                        <Border.Effect>
                            <DropShadowEffect Color="Black"
                              Direction="315"
                              BlurRadius="10"
                              ShadowDepth="7" />
                        </Border.Effect>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

The window created looks like the following:


No comments: