Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何显示(透明)背景色不显示的控件';不包括下面的控件?_C#_Winforms_Controls_Transparency_Backcolor - Fatal编程技术网

C# 如何显示(透明)背景色不显示的控件';不包括下面的控件?

C# 如何显示(透明)背景色不显示的控件';不包括下面的控件?,c#,winforms,controls,transparency,backcolor,C#,Winforms,Controls,Transparency,Backcolor,我有一个自定义的控件: public class Temp : Control { public Temp(Color col, int x, int y) { Size = new Size(x + 10, y + 10); this.x = x; this.y = y; SetStyle(ControlStyles.SupportsTransparentBackColor, true); Bac

我有一个自定义的
控件

public class Temp : Control
{
    public Temp(Color col, int x, int y)
    {
        Size = new Size(x + 10, y + 10);
        this.x = x;
        this.y = y;

        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        BackColor = col;
    }

    int x, y;

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        using (var p = new Pen(Color.Black, 3))
        {
            e.Graphics.DrawLine(p, new Point(10, 10), new Point(x, y));
        }
    }

}
从我的
表单的
Load
事件中,我将其中两个控件添加到
面板的
控件中,作为我表单的唯一控件添加:

panel1.Controls.Add(new Temp(Color.Red, 50, 50));
panel1.Controls.Add(new Temp(Color.Violet, 10, 100));
这是输出:

正如您所看到的,第一个控件覆盖了第二个控件,而我只想显示两行,其中控件的背景色是透明的

请注意,使用透明背景色不起作用:

panel1.Controls.Add(new Temp(Color.Transparent, 50, 50));
panel1.Controls.Add(new Temp(Color.Violet, 10, 100));
这是输出:


我怎样才能解决这个问题?也就是说,只显示(完全显示)两条我的线?

在自定义类中创建两个点,而不是从控件继承。 下面给出了示例代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SOWinForm
{
    public partial class Form1 : Form
    {
        List<Line> lines;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            lines = new List<Line>();
            lines.Add(new Line(){ StartPoint = new Point(10,10), EndPoint = new Point(10,100)});
            lines.Add(new Line() { StartPoint = new Point(10, 10), EndPoint = new Point(50, 50) });
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            foreach (var line in lines)
            {
                using (var p = new Pen(Color.Black, 3))
                {
                    e.Graphics.DrawLine(p, line.StartPoint, line.EndPoint);
                }
            }
        }
    }

    public class Line
    {
        public Point StartPoint {get;set;}
        public Point EndPoint { get; set; }

        //Add Custom Properties
    }
}
使用系统;
使用System.Collections.Generic;
使用系统图;
使用System.Windows.Forms;
名称空间通知
{
公共部分类Form1:Form
{
列出行;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
行=新列表();
Add(newline(){StartPoint=newpoint(10,10),EndPoint=newpoint(10100)});
Add(newline(){StartPoint=newpoint(10,10),EndPoint=newpoint(50,50)});
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
foreach(行中的var行)
{
使用(var p=新笔(颜色:黑色,3))
{
e、 图形.绘制线(p,line.StartPoint,line.EndPoint);
}
}
}
}
公共班级线
{
公共点起始点{get;set;}
公共点端点{get;set;}
//添加自定义属性
}
}

当您设置透明背景色时,这并不意味着背景色是透明的,而是其父项的颜色。第一个控件的父控件是面板(灰色),因此控件的颜色也是灰色的。将第一个控件的父控件设置为第二个


瓦尔特

如果只是线条,那么为什么不在
面板上画线条呢?或者控件本身的使用是否重要?@DonBoitnott每个控件都有不同的属性,因此我需要为不同的视觉元素使用不同的控件。那么我怀疑这是否有效。控件是
剪裁
下面的控件。透明将不会帮助您,因为已发生剪切。这是一个设计问题……你需要一个不同的方法。@DonBoitnott谢谢,有什么建议吗?这不是讨论这种问题的论坛。你可以试试