这可能有三角形的 PictureBox 而不是矩形的吗?

viv*_*ain -1 .net c# user-controls picturebox winforms

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

Rez*_*aei 5

您有一些选择,例如:

  • 您可以将控制区域设置为三角形。
  • 您只能在控件的三角形区域中绘画。

示例 1

在此示例中,控制区域仅限于三角形。

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

示例 2

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

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明