Hi,

I wanted to know how do I create the exact polygon(trapezium) in my main form.
I've now created a class "PolygonShape" and inside there is this code(this is a shape of a trapezium):

protected override void OnPaint(PaintEventArgs pe)
{

                  

   // Create pen.
   Pen blackPen = new Pen(Color.Black, 3);
   // Create points that define polygon.
   Point point1 = new Point(0, 0);
   Point point2 = new Point(0, 50);
   Point point3 = new Point(150, 50);
   Point point7 = new Point(70, 0);
   Point[] curvePoints =
   {
      point1,
      point2,
      point3,
      point7
   };
   // Draw polygon to screen.
   pe.Graphics.DrawPolygon(blackPen, curvePoints);
         
 
}

Then in my main, I've initialize as below and I created an instance of it

PolygonShape poliA = new PolygonShape();
poliA.Width = 100; //(this width and height turn out to be a rectangle)
poliA.Height = 50;
poliA.BackColor = Color.Azure;

P/S : i know why it turn out to be rectangle... but i don't know how to make it to follow the design at top when displaying it..


PROBLEM : when i run this code, then it will come out with a rectangle shape.I've alread tried to research.. but i dont get any ways to make the shape become the shape of the trapezium. Any idea?

Dani AI

Generated

Windows Forms controls are rectangular by default, so setting Width/Height will only change the control's bounding box. To make the control actually look and behave like your trapezium, build a GraphicsPath for the polygon, convert it into a Region and assign it to the control. Recreate that Region whenever the control size changes so the shape scales with Width/Height.

A minimal pattern to use (call UpdateRegion() from the constructor and OnResize) is:

private void UpdateRegion()
{
    using (var path = new System.Drawing.Drawing2D.GraphicsPath())
    {
        var pts = new PointF[]
        {
            new PointF(0f, 0f),
            new PointF(0f, Height),
            new PointF(Width, Height),
            new PointF(Width * 0.5f, 0f) // adjust fraction for top inset
        };
        path.AddPolygon(pts);
        this.Region?.Dispose();
        this.Region = new Region(path);
    }
}

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    UpdateRegion();
}

For painting, fill the Region (or redraw the polygon) inside OnPaint. To avoid the rectangular background showing, either let the parent paint through (support transparent background) or explicitly fill only the region you want. Use Region.IsVisible(point) for precise hit-testing if needed, and dispose GraphicsPath/old Region properly to avoid leaks.

This ties together 's idea of supplying points and 's hint about GraphicsPath. For reference see the MSDN docs on GraphicsPath and the Control.Region property (assigning a region is how Windows clips the control). Consider double-buffering (control styles) to reduce flicker and, for heavy vector UI work, evaluate WPF which handles non-rectangular vector shapes more naturally.

Recommended Answers

All 4 Replies

For easier to understand what i'm going to do.

I wanted to make

x.Width
x.Height

to be a shape of trapezium... I'm wondering whether I'm able to put the shape into it or not (for the instance of my object)

Hi,

My advice is that you in the constructor of the class PoligonShape should recieve an array of points and a Pen. Add a method called Paint that method shoud recive a Form
and you assign the grafic object like this:

Grafic g = form.CreateGrafic();//form is the parameter of the Paint method

Regards,
Camilo

Hi, i think GraphicsPath class may also helpful

Thanks !

GraphicPath really help it ! :twisted:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.