Winforms 控件中的阴影?
有没有办法给控件添加阴影?
有没有具有此功能的控件?
is there a way to add a drop shadow to controls?
are there any controls out there with this feature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须像这样覆盖
CreateParams
属性:You have to overwrite the
CreateParams
property like this:这个问题已经存在了6年了,需要一个答案。我希望任何需要这样做的人都可以从我的解决方案中推断出任何控制集的答案。我有一个面板,想要在每个子控件下方绘制阴影 - 在本例中是一个或多个面板(但该解决方案应该适用于其他控件类型,只需进行一些小的代码更改)。
由于控件的阴影必须绘制在该控件容器的表面上,因此我们首先向容器的 Paint() 事件添加一个函数。
dropShadow() 看起来像这样:
显然,您可以从容器的集合中选择不同的控件类型,并且可以通过一些细微的调整来改变阴影的颜色和深度。
This question has been around for 6 years and needs an answer. I hope that anyone who needs to do this can extrapolate an answer for any control set from my solution. I had a panel and wanted to draw a drop shadow underneath every child control - in this instance one or more panels (but the solution should hold good for other control types with some minor code changes).
As the drop shadow for a control has to be drawn on the surface of that control's container we start by adding a function to the container's Paint() event.
dropShadow() looks like this:
Clearly you can pick a different control type from the container's collection and you can vary the colour and depth of the shadow with some minor tweaks.
最上面的答案实际上确实生成了阴影,但我个人对它并不满意,原因有几个:
因此,由于所有这些事情,我最终为我的项目编写了自己的内容,我想我应该在这里分享它:
在此实现中,添加到
shadowControls
的所有控件都将用平滑的颜色绘制阴影。您应该能够对非矩形形状实现此功能,因为生成阴影的主函数采用GraphicsPath
。请注意,在将阴影绘制到表单之前,将阴影绘制到另一个位图非常重要,因为 main 函数需要SourceCopy
的合成模式才能工作,这意味着如果您不将其绘制到另一个表面首先,阴影后面的任何东西都将被完全替换,并且透明度方面毫无用处。我正在回答 10 年前的问题,但希望这对某人有帮助!The top answer does in fact generate a shadow, but I personally wasn't satisfied with it for a few reasons:
So, because of all these things, I ended up writing my own for my project and I thought I'd share it here:
In this implementation, all Controls added to the
shadowControls
will be painted with a smooth shadow. You should be able to implement this for non-rectangular shapes because the main function to generate the shadows takes aGraphicsPath
. Please note that it's important you draw the shadow to another bitmap before drawing it to the form because the main function requires a compositing mode ofSourceCopy
to work, which means if you don't draw it to another surface first anything behind the shadow will be completely replaced and the transparency aspect is useless. I'm on a roll of answering 10-year-old questions, but hopefully, this helps someone!如果您可以使用 WPF 中的替代方案,我不认为 Windows 窗体中存在替代方案,因为 GDI+ 的功能有限。
There is in WPF if you can stretch to using that instead, I don't believe there is an alternative in Windows Forms due to the limited capabilities of GDI+.
一种方法是将控件放入具有以下属性的表格布局中:
这将使您可以通过调整外部列和行的大小来灵活调整大小。下面是一个快速草图,可直观地看到设计器中表格布局的外观:
表格布局草图
表格布局草图 2
应用程序中最终结果的实际屏幕截图:
截图
One way would be to put your control in a table layout with these properties:
This will give you flexibility over sizing by sizing the outer columns and rows. Here is a quick sketch to visualize what the table layout would look like in the designer:
Table Layout Sketch
Table Layout Sketch 2
Actual screenshot of the end result in an application:
Screenshot
这是一个有争议的观点,你不用代码就可以做到。
将主面板边框样式设置为“固定单一”。
在其下方创建 3 个面板,每个方向各大 1 像素。
3 个面板均采用浅灰色。
不完美但便宜又简单。
带有伪阴影的面板
Here's a controversial opinion, you do it without code.
Set your main panel Border Style to Fixed Single.
Create 3 panels below it, each 1 pixel larger in every direction.
Each of the 3 panels is of a lighter shade of gray.
Not perfect but cheap and easy.
panel with pseudo-shadow