Question

I have a Windows Forms app where I add different figures(rectangles, circles, etc.) to the main form. The figure is a UserControl and it's shape I define with GraphicsPath. Method for adding new figure:

 void AddElement(ShapeType shape, string guid)
    {
        Shape newShape = new Shape();
        newShape.Name = guid;
        newShape.Size = new Size(100, 100);           
        newShape.Type = shape;
        newShape.Location = new Point(100, 100);

        newShape.MouseDown += new MouseEventHandler(Shape_MouseDown);
        newShape.MouseMove += new MouseEventHandler(Shape_MouseMove);
        newShape.MouseUp += new MouseEventHandler(Shape_MouseUp);
        newShape.BackColor = this.BackColor;

        this.Controls.Add(newShape);
    }

In Shape (Figure) class:

 private ShapeType shape;
 private GraphicsPath path = null;
 public ShapeType Type
    {
        get { return shape; }
        set
        {
            shape = value;
            DrawElement();
        }
    } 

 private void DrawElement()
     {
        path = new GraphicsPath();
        switch (shape)
        {
            case ShapeType.Rectangle:
                path.AddRectangle(this.ClientRectangle);
                break;

            case ShapeType.Circle:
                path.AddEllipse(this.ClientRectangle);
                break;

            case ShapeType.Line:
                path.AddLine(10,10,20,20);                   
                break;
        }
        this.Region = new Region(path);
    }

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        if (path != null)
        {              
            e.Graphics.DrawPath(new Pen(Color.Black, 4), path);
        }
    }

When resizing the figure, I redraw It:

 protected override void OnResize(System.EventArgs e)
    {
        DrawElement();
        this.Invalidate();
    }

Everything works fine when I add shapes like rectangle and circle. But when I choose Line, nothing appears on my form. The breakpoint shows that the programs steps in all the methods and this.Controls.Add(newShape); as well.

I do not understand why this is not working. I'd appreciate any advice.

Was it helpful?

Solution

You can draw an open GraphicsPath with a thin or a thick Pen. But a region must be set from a closed shape or else there is no place where your pixels could show up. This will help to keep your region intact; but you need to know, just what you want it to be:

if (shape != ShapeType.Line)   this.Region = new Region(path);

If you want it to be something like a thick line you must create a polygon or a series of lines to outline the shape you want. And if you want your line to be inside that region you will need two paths: one closed polygon path to set the region and one open line path to draw the line inside the region.

Edit: The best way to create the closed path is probably to use the Widen() method with the Pen you are using like this:

GraphicsPath path2 = path.Widen(yourPen);

This would get the thickness right as well as the line caps and also work for more complicated polylines; I haven't tried it though..

OTHER TIPS

Maybe it's because the line has no area. Try to replace it with a very thin shape having a positive area. For instance:

const int thickness = 1;
path.AddLines(new[]
    {
        new Point(10, 10),
        new Point(20, 20),
        new Point(20 + thickness, 20 + thickness),
        new Point(10 + thickness, 10 + thickness)
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top