我知道已经提出并回答了类似的问题。然而,我的问题分为三个部分。
就本问题而言,请记住以下几点:
首先,是否可以在无边框窗口的背景中使用玻璃效果?如果是这样(我相信它),这是怎么做到的?
此外,由于Windows 8及更新版本不再使用半透明玻璃效果,这仍然可以在这些操作系统上完成吗?
我想要平滑的原生DWM模糊效果,但我不想要有光泽的玻璃覆盖,我也不想在用户的主题设置中预定义窗口颜色。
是否可以仅将此效果应用于窗口的某个部分?更重要的是,是否可以调整模糊效果的强度(半径)?
根据要求,我已经发布了一些我希望实现的效果的例子。
上面的图片是我的WPF应用程序的实际屏幕截图(仍在开发中)。它极简主义的设计在很大程度上依赖于动画和窗口大小的动画。
使用一些photoshop技巧,我已经渲染了上面的图像,展示了我想要创建的效果,完全我想要的方式。请注意以下事项:
答案 0 :(得分:13)
因为你想要实现的不是玻璃效果而是更多的透明+模糊,你可以使用以下方法在窗口后面模糊。
Windows 7 :您可以使用DwmEnableBlurBehindWindow模糊窗口。
Window 8 :由于在Windows 8中删除了DwmEnableBlurBehindWindow,我找不到可行的解决方案。
Windows 10 :您可以使用SetWindowCompositionAttribute在窗口后面模糊。
以上解决方案仅在窗口后面应用模糊效果,它将由窗口定义透明度和颜色。
使用这些方法,您只能在整个窗口下方模糊,并且您可以在要透明的窗口部分上使用Alpha通道。我不认为你可以定义模糊半径。
答案 1 :(得分:6)
请原谅我对Stackoverflow的经验不足,但我想我会尽力帮你解决一下。
通过关注Tom发布的链接,我能够遇到这个代码块(最初是在c#中)。所以看来这个代码对大多数人来说并不容易获得,这里是:
Imports System.Runtime.InteropServices
Imports System.Windows.Interop
'Import namespace ("name of project" . "name of namespace")
Imports Blurred_Opacity.BlurBehind
Namespace BlurTest
Enum AccentState
ACCENT_DISABLED = 0
ACCENT_ENABLE_GRADIENT = 1
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
ACCENT_ENABLE_BLURBEHIND = 3
ACCENT_INVALID_STATE = 4
End Enum
Structure AccentPolicy
Public AccentState As AccentState
Public AccentFlags As Integer
Public GradientColor As Integer
Public AnimationId As Integer
End Structure
Structure WindowCompositionAttributeData
Public Attribute As WindowCompositionAttribute
Public Data As IntPtr
Public SizeOfData As Integer
End Structure
Enum WindowCompositionAttribute
WCA_ACCENT_POLICY = 19
End Enum
End Namespace
Class MainWindow
<DllImport("user32.dll")>
Friend Shared Function SetWindowCompositionAttribute(hwnd As IntPtr, ByRef data As WindowCompositionAttributeData) As Integer
End Function
Sub Window_Loaded() handles me.loaded
EnableBlur()
End Sub
Sub Window_MouseDown() handles me.MouseLeftButtonDown
DragMove()
End Sub
Sub EnableBlur()
Dim windowHelper = New WindowInteropHelper(Me)
Dim accent = New AccentPolicy()
accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND
Dim accentStructSize = Marshal.SizeOf(accent)
Dim accentPtr = Marshal.AllocHGlobal(accentStructSize)
Marshal.StructureToPtr(accent, accentPtr, False)
Dim Data = New WindowCompositionAttributeData()
Data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY
Data.SizeOfData = accentStructSize
Data.Data = accentPtr
SetWindowCompositionAttribute(windowHelper.Handle, Data)
Marshal.FreeHGlobal(accentPtr)
End Sub
End Class
一旦实现,这将影响整个Window,如下所示:
在尝试复制你的设计约5分钟后,我想出了这个:
我相信你能比我更好地完成设计。只需调整背景颜色(在窗口上)即可更改混合颜色,也可以更改不透明度级别。我的设计的XAML如下:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="MainWindow"
Title="Blurred Opacity" Height="623" Width="752"
Background="#727A7A7A"
AllowsTransparency="True"
WindowStyle="None"
BorderThickness="1"
WindowStartupLocation="CenterScreen"
Loaded="Window_Loaded" MouseLeftButtonDown="Window_MouseDown" Topmost="True" BorderBrush="#FF1E9EC5">
<Grid>
<Rectangle Fill="#FF0143A4" Height="130" VerticalAlignment="Top"/>
<Rectangle Fill="White" Margin="0,130,0,0" HorizontalAlignment="Right" Width="375"/>
<StackPanel HorizontalAlignment="Left" Margin="0,130,0,0" Width="375">
<TextBlock x:Name="textBlock" Height="50" TextWrapping="Wrap" Text="Category 1" d:LayoutOverrides="LeftPosition, RightPosition" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6"/>
<TextBlock x:Name="textBlock_Copy" Height="50" TextWrapping="Wrap" Text="Category 2" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
<TextBlock x:Name="textBlock_Copy1" Height="50" TextWrapping="Wrap" Text="Category 3" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
<TextBlock x:Name="textBlock_Copy2" Height="50" TextWrapping="Wrap" Text="Category 4" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
<TextBlock x:Name="textBlock_Copy3" Height="50" TextWrapping="Wrap" Text="Category 5" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
<TextBlock x:Name="textBlock_Copy4" Height="50" TextWrapping="Wrap" Text="Category 6" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
<TextBlock x:Name="textBlock_Copy5" Height="50" TextWrapping="Wrap" Text="Category 7" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
<TextBlock x:Name="textBlock_Copy6" Height="50" TextWrapping="Wrap" Text="Category 8" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
</StackPanel>
<TextBlock x:Name="textBlock_Copy7" Height="90" TextWrapping="Wrap" Text="Example" FontSize="65" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" Margin="222.5,23,152.5,0" VerticalAlignment="Top" Foreground="White"/>
<Path Data="M705,27.333333 L735.66667,10" Fill="White" HorizontalAlignment="Right" Height="24" Margin="0,7,21,0" Stretch="Fill" VerticalAlignment="Top" Width="24" StrokeThickness="3" Stroke="White"/>
<Path Data="M705,27.333333 L735.66667,10" Fill="White" HorizontalAlignment="Right" Height="24.083" Margin="0,6.833,20.333,0" Stretch="Fill" VerticalAlignment="Top" Width="24.167" StrokeThickness="3" Stroke="White" RenderTransformOrigin="0.5,0.5">
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="-1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Path.RenderTransform>
</Path>
<StackPanel HorizontalAlignment="Right" Margin="0,130,0,0" Width="375">
<TextBlock x:Name="textBlock1" Height="50" TextWrapping="Wrap" Text="Item 1" d:LayoutOverrides="LeftPosition, RightPosition" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6"/>
<TextBlock x:Name="textBlock_Copy8" Height="50" TextWrapping="Wrap" Text="Item 2" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
<TextBlock x:Name="textBlock_Copy9" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="3"/><LineBreak/><Run Text="3"/></TextBlock>
<TextBlock x:Name="textBlock_Copy10" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="4"/></TextBlock>
<TextBlock x:Name="textBlock_Copy11" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="5"/></TextBlock>
<TextBlock x:Name="textBlock_Copy12" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="6"/></TextBlock>
<TextBlock x:Name="textBlock_Copy13" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="7"/></TextBlock>
<TextBlock x:Name="textBlock_Copy14" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="8"/></TextBlock>
<TextBlock x:Name="textBlock_Copy15" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="9"/></TextBlock>
</StackPanel>
</Grid>
我希望这有帮助!