考虑以下示例。这是通过设置TransparencyKey
属性:
public Form()
{
this.InitializeComponent();
this.BackColor = Color.Fuscia;
this.TransparencyKey = this.BackColor;
}
我真正想要做的是类似于DrawThemeParentBackground函数的行为(在.NET中方便地包含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下绘制内容?
答案 0 :(得分:1)
这是你想要的效果吗?
如果是这样,您可以使用两种不同的形式。你在第二个中进行绘图。
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);
}
}