顶级窗体上的 DrawParentBackground / DrawThemeParentBackground

DrawParentBackground / DrawThemeParentBackground on a top level form

考虑以下示例。这是通过设置 TransparencyKey 属性:

public Form()
{
    this.InitializeComponent();
    this.BackColor = Color.Fuscia;
    this.TransparencyKey = this.BackColor;
}

我真正想要做的是类似于 DrawThemeParentBackground function (conveniently wrapped up in .NET as DrawParentBackground) 的行为,但这似乎不适用于顶级表单(仅控件)。

我尝试使用 TransparencyKey 行为并覆盖 OnPaint 方法:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 255, 0, 0)), this.ClientRectangle);
}

结果:

问题:

如何在顶级表单的 ClientRectangle 下面绘制内容?

这是你想要的效果吗?

如果是这样,您可以使用两种不同的形式。你在第二个里画画。

public partial class Form1 : Form
{
    private Form2 form2;

    public Form1()
    {
        InitializeComponent();
        this.BackColor = Color.White;
        this.TransparencyKey = Color.White;
        this.StartPosition = FormStartPosition.Manual;
        this.Location = new Point(200, 200);
        form2 = new Form2();
        form2.StartPosition = this.StartPosition;
        form2.Location = this.Location;
        form2.Show();

        this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
        this.LocationChanged += new EventHandler(Form1_LocationChanged);
        this.SizeChanged += new EventHandler(Form1_SizeChanged);
    }

    void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        form2.Close();
    }

    void Form1_LocationChanged(object sender, EventArgs e)
    {
        form2.Location = this.Location;
    }

    void Form1_SizeChanged(object sender, EventArgs e)
    {
        form2.Size = this.Size;
    }
}

 public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        ShowInTaskbar = false;
        this.Opacity = 0.8;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(255, 0, 0)), this.ClientRectangle);
    }
}