繁体   English   中英

尝试将图层加载到控件C#

[英]Trying to load layers to a control c#

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

这是矩形的类

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)
        {

        }
    }

我认为问题在于,当您设置“透明背景”时,该矩形正在使用Form Backcolor,并且您似乎正在擦除另一个矩形。 也许您可以尝试将透明矩形绘制为4行而不是矩形。

希望对您有帮助

我的朋友们,我以这种方式解决了。

我为矩形创建了一个类,然后为控件创建了另一个类,在该类中,我按照您告诉我的方式绘制了所有矩形,然后绘制了图像,然后使用controls.add将其添加为表单的一层。这样,我还可以将图像放置在其下而不会被矩形擦除。

谢谢!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM