我正在创建自定义形状的Windows Form
(public class Window : Form
)并需要放置适当的阴影。表格是带圆角的矩形,我正在定义一个合适的GraphicsPath
来获得表格的形状。
以下是应该执行此操作的部分代码:
private const int CS_DROPSHADOW = 0x00020000;
public Window()
{
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
SetStyle(ControlStyles.Opaque | ControlStyles.OptimizedDoubleBuffer, true);
TransparencyKey = Color.FromArgb(0xFF, 0, 0, 0);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
Pen ps = new Pen(Color.FromArgb(0xFF, 0, 0, 0));
e.Graphics.FillRectangle(ps.Brush, this.ClientRectangle);
Pen p1 = new Pen(Color.FromArgb(0xFA, 0xFA, 0xFA));
Pen p2 = new Pen(Color.FromArgb(0xCD, 0xCD, 0xCD));
GraphicsPath border =
RoundedRectangle.Create(new Rectangle(0, 0, Width - 1, Height - 1), 7);
e.Graphics.SetClip(border); // I'd prefer if this came after the following two
// lines, but doing that results in another problem
e.Graphics.FillPath(p1.Brush, border);
e.Graphics.DrawPath(p2, border);
base.OnPaint(e);
e.Graphics.ResetClip();
}
RoundedRectangle
课程来自here。
生成的阴影就像表格是完全矩形一样,最好在右下角看到。如何正确绘制阴影?
编辑:我无法相信我在2004年(8年前)在网上发现了关于此问题的讨论,但仍无法找到解决方案。