尝试将层加载到控件c#

尝试将层加载到控件c#,c#,layer,rectangles,C#,Layer,Rectangles,我试图加载一些矩形作为表单的“层”,我加载了一个图像作为表单的层,但这些矩形的问题是它们“覆盖”了图像,擦除了部分图像,我希望它们被视为图像框来显示信息,我还希望能够将一个矩形叠加到另一个矩形上,而不相互擦除 这是矩形的类 namespace Imagen_capas { class rectangulotransp : UserControl { public Pen pen11; private Rectangle Myrectangle;

我试图加载一些矩形作为表单的“层”,我加载了一个图像作为表单的层,但这些矩形的问题是它们“覆盖”了图像,擦除了部分图像,我希望它们被视为图像框来显示信息,我还希望能够将一个矩形叠加到另一个矩形上,而不相互擦除

这是矩形的类

namespace Imagen_capas
{
   class rectangulotransp : UserControl
   {
      public Pen pen11;
      private Rectangle Myrectangle;



     public rectangulotransp(int x,int y,int alto, int ancho, Rectangle tamacontrol)
      {
          Size = tamacontrol.Size;
          Location = tamacontrol.Location;
          SetStyle(ControlStyles.SupportsTransparentBackColor, true);
          BackColor = Color.Transparent;
          Myrectangle = new Rectangle(x, y, alto, ancho);
          pen11 = nuevopen();


      }  


      private Pen nuevopen()
      {
          Pen mypen1 = new Pen(Color.Red);
          return mypen1;
      }

      protected override void OnPaint(PaintEventArgs e)
      {
          Graphics g = e.Graphics;
          g.DrawRectangle(pen11,Myrectangle);

          base.OnPaint(e);
      }
  }
}

以及表格的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Imagen_capas
{   
    public partial class Form1 : Form
    {
    rectangulotransp rect;
    rectangulotransp rect2;
        public Form1()
        {
            InitializeComponent();
            rect = new rectangulotransp(50, 14, 500, 100,new     Rectangle(0,0,Width+400,Height));
           // this.Controls.Add(rect);
             rect2 = new rectangulotransp(0, 50, 20, 100,new     Rectangle(20,50,Width,Height));
            this.Controls.Add(rect2);
            this.Controls.Add(rect);

        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

我认为问题在于,当你设置透明背景时,矩形使用的是表单背景色,在你看来,这是在擦除另一个矩形。 也许你可以试着把你的透明矩形画成4行而不是一个矩形


希望它能帮助你

我的朋友们,我就是这样解决的

我为矩形创建了一个类,然后为控件创建了另一个类,在这个类中,我绘制了所有的矩形,就像你告诉我的那样,然后是图像,然后我使用控件将其添加为窗体的一个层。添加,这样,我也可以将图像放在它下面,而不会被矩形擦除


谢谢

AFAIK用户控件不支持透明性。您可以将矩形类更改为非控制类,并在pain事件中使用tranparency绘制它们。WinForms不会这样进行分层。透明背景只调用父对象的背景来绘制它,而不是它下面的控件。解决方案是根本不使用控件,只创建一个包含矩形信息的类,并在容器的paint事件中绘制这些对象。