【问题标题】:DrawParentBackground / DrawThemeParentBackground on a top level form顶级表单上的 DrawParentBackground / DrawThemeParentBackground
【发布时间】:2015-04-08 05:41:57
【问题描述】:

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

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

我真正想要做的是类似于DrawThemeParentBackground 函数的行为(方便地以DrawParentBackground 包装在.NET 中),但是这似乎不适用于顶级表单(仅控件)。

我尝试使用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 下方绘制内容?

【问题讨论】:

    标签: c# winforms gdi+ gdi


    【解决方案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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2017-02-03
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      相关资源
      最近更新 更多