这可能有三角形的图片框而不是矩形的吗?角形、矩形、能有、这可

2023-09-07 14:53:22 作者:少年心

这是否可以在 Windows 窗体中使用三角形 PictureBox 控件而不是矩形控件?

Is this possible to have triangular PictureBox control in windows forms instead of the rectangular one?

推荐答案

你有一些选择,例如:

您可以将控制区域设置为三角形.您只能在控件的三角形区域内绘制.

示例 1

在本例中,控制区域仅限于三角形.

In this example, the region of control limited to a triangular shape.

public class TriangularPictureBox:PictureBox
{
    protected override void OnPaint(PaintEventArgs pe)
    {
        using (var p = new GraphicsPath())
        {
            p.AddPolygon(new Point[] {
                new Point(this.Width / 2, 0), 
                new Point(0, Height), 
                new Point(Width, Height) });

            this.Region = new Region(p);
            base.OnPaint(pe);
        }
    }
}

示例 2

在此示例中,仅在控件的三角形区域上进行绘制.

In this example, the painting will be done only on a triangular area of the control.

public class TriangularPictureBox:PictureBox
{
    protected override void OnPaint(PaintEventArgs pe)
    {
        using (var p = new GraphicsPath())
        {
            p.AddPolygon(new Point[] {
                new Point(this.Width / 2, 0), 
                new Point(0, Height), 
                new Point(Width, Height) });

            pe.Graphics.SetClip(p);
            base.OnPaint(pe);
        }
    }
}