阴影中的WinForms控件? [英] Drop shadow in Winforms Controls?
问题描述
有没有办法来添加阴影的控件?
is there a way to add a drop shadow to controls?
是否有任何控制在那里与此功能?
are there any controls out there with this feature?
推荐答案
这个问题已经存在了6年,需要一个答案。我希望任何人谁需要这样做可以推断从我的解决方案设置的任何控制一个答案。我有一个面板,并想画每一个孩子的控制之下的阴影 - 在这种情况下一个或多个面板(但解决的办法应该把握好与一些小的代码更改其他控制类型)
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).
作为对照的阴影了我们,加入到容器的Paint()事件的函数启动该控件的容器的表面上进行绘制。
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.
Container.Paint += dropShadow;
阴影效果()看起来是这样的:
dropShadow() looks like this:
private void dropShadow(object sender, PaintEventArgs e)
{
Panel panel = (Panel)sender;
Color[] shadow = new Color[3];
shadow[0] = Color.FromArgb(181, 181, 181);
shadow[1] = Color.FromArgb(195, 195, 195);
shadow[2] = Color.FromArgb(211, 211, 211);
Pen pen = new Pen(shadow[0]);
using (pen)
{
foreach (Panel p in panel.Controls.OfType<Panel>())
{
Point pt = p.Location;
pt.Y += p.Height;
for (var sp = 0; sp < 3; sp++)
{
pen.Color = shadow[sp];
e.Graphics.DrawLine(pen, pt.X, pt.Y, pt.X + p.Width - 1, pt.Y);
pt.Y++;
}
}
}
}
显然,你可以挑选从容器的集合不同的控制类型,你可以改变一些小的调整阴影的颜色和深度。
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.
这篇关于阴影中的WinForms控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!