WPF 无边框窗口的 DropShadow
我有一个 WPF 窗口,WindowStyle 设置为 none。有什么方法可以强制这个窗口留下阴影(就像 WindowStyle 不是 none 时得到的阴影一样)?我不想将AllowTransparency 设置为true,因为它会影响性能。而且我也不想禁用硬件渲染(我在某处读到禁用它时透明度效果更好)。
I have a WPF Window with WindowStyle set to none. Is there some way I can force this window to drop a shadow (like the one you get when WindowStyle is not none)? I don't want to set AllowTransparency to true, because it affects the performance. And I also don't want to disable hardware rendering (I read somewhere that transparency performs better with it disabled).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不直接使用与“窗口”相同的对象创建阴影,但要更大并位于其后面。
或者,如果您需要透明标题栏,则可以将其替换为
编辑:我刚刚注意到 OP 希望将 AllowsTransparency 设置为 False。不过,如果影子不是“真实的”,我就看不到它能起作用。
Why not just create the shadow with the same object as your "window" but bigger and behind it.
Or if you need a transparent title bar, it could be replaced by a
<Border>
Edit: I just noticed OP wants AllowsTransparency set to False. I can't see a shadow to work without it being "True", thouth.
我编写了一个小实用程序类,它能够完全满足您的要求:在无边框
Window
上放置标准阴影,但将AllowsTransparency
设置为false
代码>.您只需调用
DropShadowToWindow(Window window)
方法即可。最好在窗口构造函数的InitializeComponent()
之后进行此调用,但即使您在窗口显示后调用它,它也会起作用。I have written a little utility class that is able to do exactly what you want: drop a standard shadow over a borderless
Window
but havingAllowsTransparency
set tofalse
.You just have to call the
DropShadowToWindow(Window window)
method. It is preferred that you make this call just after the window's constructor'sInitializeComponent()
, but it will work even if you call it after the window is shown.Patrick 的答案非常有效,除非托管 win32 窗口。
发生这种情况时,您会注意到托管窗口被“冲掉”(看起来窗口正在将“玻璃板”效果应用于整个托管窗口)。
在本地定义结构时,这种奇怪的行为是固定的,
例如
Patrick's answer works great, except when a win32 window is hosted.
When that happens, you notice that the hosted window is "washed out" (it looks like windows is applying the 'glass sheet' effect to the entire hosted window).
This odd behavior is fixed when defining the structure locally,
e.g.
如果您允许窗口调整边框大小,通过将
ResizeMode
设置为CanResize
,那么您将获得操作系统投影。然后,您可以将MaxWidth
、MinWidth
、MaxHeight
和MinHeight
设置为阻止调整大小的值。如果您有一个没有样式的无边框窗口,您将必须在自己的可视化树中提供窗口的所有外观,包括阴影,因为这种设置组合相当于说您不想要操作系统的内容提供。
编辑:
从那时起,如果您的窗口大小是固定的,只需添加投影,也许作为
作为像这样:
请注意,第一个
矩形
的Fill
属性是部分透明的,您也可以使用Opacity
来做到这一点矩形
的 code> 属性。您可以使用自己的图形或不同的形状来自定义投影的外观。请注意,这违反了您将
AllowsTransparency
设置为False
的要求,但您别无选择:如果您想要透明度,则必须允许它。If you permit the window to have resize borders, by setting
ResizeMode
toCanResize
, then you will get the OS drop shadow. You can then set theMaxWidth
,MinWidth
,MaxHeight
, andMinHeight
to values which will prevent the resize.If you have a borderless window without a style you will have to provide all the appearance for the window in your own visual tree, including a drop shadow, since this combination of settings is the same as saying that you don't want what the OS provides.
EDIT:
From that point, if your window size is fixed, simply add the dropshadow, perhaps as a
<Rectangle/>
as the first element in the content of a<Canvas/>
something like this:
Note that the
Fill
property of that firstRectangle
is partially transparent, which you could also do with theOpacity
property of theRectangle
. You could use a graphic of your own or a different shape, to customize the appearance of the drop shadow.Note that this violates your requirement to have
AllowsTransparency
beFalse
, but you have no choice: if you want transparency, you have to allow it.