Maximum custom window loses shadow effect

I have a custom WPF window defined as:

<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" MinHeight="300" Height="350" MinWidth="600" Width="700" ResizeMode="CanResizeWithGrip" AllowsTransparency="True" WindowStyle="None"> 

I found an online class that creates shadow for shadows, as shown below. This works well, even with downsizing, until I get the maximum window. As soon as I enlarge the window or change the state of the window of another window (for example, Visual Studio), I lose the shadow and I can not return it. Any ideas?




Shadow class:

 Public Class DropShadow Private Shared _handler As EventHandler = New EventHandler(AddressOf window_SourceInitialized) <DllImport("dwmapi.dll", PreserveSig:=True)> _ Private Shared Function DwmSetWindowAttribute(hwnd As IntPtr, attr As Integer, ByRef attrValue As Integer, attrSize As Integer) As Integer End Function <DllImport("dwmapi.dll")> _ Private Shared Function DwmExtendFrameIntoClientArea(hWnd As IntPtr, ByRef pMarInset As Margins) As Integer End Function Public Shared Sub DropShadowToWindow(window As Window) If Not DropShadow(window) Then AddHandler window.SourceInitialized, _handler AddHandler window.SizeChanged, New SizeChangedEventHandler(AddressOf windowSizeChanged) End If End Sub Private Shared Sub window_SourceInitialized(sender As Object, e As EventArgs) Dim window As Window = DirectCast(sender, Window) DropShadow(window) RemoveHandler window.SourceInitialized, _handler End Sub Private Shared Function DropShadow(window As Window) As Boolean Try Dim helper As New WindowInteropHelper(window) Dim val As Integer = 2 Dim ret1 As Integer = DwmSetWindowAttribute(helper.Handle, 2, val, 4) If ret1 = 0 Then Dim m As New Margins() With { _ .Bottom = 0, _ .Left = 0, _ .Right = 0, _ .Top = 0 _ } Dim ret2 As Integer = DwmExtendFrameIntoClientArea(helper.Handle, m) Return ret2 = 0 Else Return False End If Catch ex As Exception ' Probably dwmapi.dll not found (incompatible OS) Return False End Try End Function Private Shared Sub windowSizeChanged(sender As Object, e As SizeChangedEventArgs) Dim window As Window = DirectCast(sender, Window) DropShadow(window) End Sub End Class 
+12
wpf window xaml
Sep 10 2018-11-11T00:
source share
3 answers

To create a shadow effect while being able to resize the form, try the following:

  • Set the following properties in the window:

    • ResizeMode = "CanResizeWithGrip"
    • AllowsTransparency = "True"
    • WindowStyle = "None"
    • Background = Transparent
    • BorderThickness = "3"
  • After declaring a window, add a Border element.

  • Create a Border.Effect Inside the Frame
  • For the border effect, add the following:

     <DropShadowEffect BlurRadius="5" Color="Black" Opacity="0.8" ShadowDepth="0.5" /> 

This will create the following (without a control panel in the upper right corner):

enter image description here

Full XAML:

 <Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" MinHeight="500" Height="350" MinWidth="300" Width="700" ResizeMode="CanResizeWithGrip" AllowsTransparency="True" WindowStyle="None" Background="White" BorderThickness="3"> <Border> <Border.Effect> <DropShadowEffect BlurRadius="5" Color="Black" Opacity="0.8" ShadowDepth="0.5" /> </Border.Effect> <!-- Put your content in here --> </Border> </Window> 
+15
Sep 10 '11 at 10:09
source share

So, I found a way to make this work.

You need to use the WPF Shell integration library ( here ) to do the job for you. As written by MS, they fixed (apparently) any problems with P / Invoke code execution.

Thus, it is easy to get a window that does not have Aero glass, resizes around the edges, has a title bar that behaves with Aero snapping, and has a shadow that appears again after min / maxing.

This is the code for my window (note that you need to specify Microsoft.Windows.Shell )

 <Window x:Class="MyLibrary.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell" Title="MainWindow" WindowStyle="SingleBorderWindow" ResizeMode="CanResizeWithGrip" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="449" d:DesignWidth="677" Foreground="White" Background="Black"> <shell:WindowChrome.WindowChrome> <shell:WindowChrome CaptionHeight="35" GlassFrameThickness="0,0,0,1" ResizeBorderThickness="5" /> </shell:WindowChrome.WindowChrome> <Grid x:Name="LayoutRoot"> </gGrid> </Window> 

<shell:WindowChrome> is the place where you set all the variables to interact with.

  • CaptionHeight : This is the height of the title CaptionHeight (title), which allows you to use Aero, double-clicking, as usual, in the title bar.
  • GlassFrameThickness : setting this parameter to 0,0,0,1 for some reason removes chrome (glass), preserves a square frame and adds shadows.
  • ResizeBorderThickness : This is the thickness at the edge of the window where you can resize the window.

Other things to consider as you keep the Window.WindowStyle property set to SingleBorderWindow and allow the Shell library to remove the title, buttons, and other chrome.

So, I kind of wasted my generosity, but it seems like a perfectly viable solution that works!

EDIT:

Here is the image of the result: Sample Metro WPF Application

I also suggested a sample project http://code.google.com/p/sample-metro-wpf-application/ . This is a MIT license, and people can use it, but they want to.

+21
Dec 29 '11 at 7:11
source share

Here is some minimal code that does what you need.

 <Window x:Class="WindowChromeSpike.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <WindowChrome.WindowChrome> <WindowChrome GlassFrameThickness="0,0,0,1" CornerRadius="0" /> </WindowChrome.WindowChrome> <!-- window contents: just a blue rectangle for demo purposes --> <Border Background="#0093C0" /> </Window> 

This window behaves like a regular window in which it can be:

  • changed around the edges
  • dragged to header area
  • right-click the title bar to display the system menu
  • maximized / restored by double-clicking on the title bar
  • tied to the sides of the screen by dragging or using hot keys (Win 10)

He also has a shadow.




The end result is as follows:

enter image description here

+3
Oct 08 '16 at 11:58 on
source share



All Articles