我正在创建一个自定义形状的 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
课程从这里开始。
生成的阴影就像形状是完美的矩形一样投射,这在右下角最为明显。如何正确绘制阴影?
编辑:我不敢相信我从 2004 年(8 年前)开始在网上找到关于此的讨论,但仍然没有找到解决方案。