Question

I beleive it's possible but aint see nothing like that. I would like to have rectangles (some horizontal and some vertical) acting like this : take the first one, place it side the second one and when line touch, create a "L" without the inside lines. It will be a kinda basic CAD drawing.

The project will be created with visual studio 2010 ultimate in c# with framework 4.

If anyone have clues or path to tutorials, i'll appreciate.

Thanks !

EDIT: there is my try.

System.Drawing.Rectangle rectangle1 = new System.Drawing.Rectangle(30, 40, 50, 200);          System.Drawing.Rectangle rectangle2 = new System.Drawing.Rectangle(30, 190, 200, 50);
e.Graphics.DrawRectangle(Pens.Blue, rectangle1);
e.Graphics.DrawRectangle(Pens.GreenYellow, rectangle2);
System.Drawing.Rectangle rectangle3 = System.Drawing.Rectangle.Intersect(rectangle1, rectangle2);
e.Graphics.DrawRectangle(Pens.White, rectangle3);
Was it helpful?

Solution

Add your Rectangles to a GraphicsPath, then use the GdipWindingModeOutline() API to convert it to just the outline as in this SO question.

L-Shaped Outline from GraphicsPath

Specifically, with your example:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    [DllImport(@"gdiplus.dll")]
    public static extern int GdipWindingModeOutline(HandleRef path, IntPtr matrix, float flatness);

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Rectangle rectangle1 = new Rectangle(30, 40, 50, 200); 
        Rectangle rectangle2 = new Rectangle(30, 190, 200, 50);

        GraphicsPath gp = new GraphicsPath();
        gp.AddRectangle(rectangle1);
        gp.AddRectangle(rectangle2);

        HandleRef handle = new HandleRef(gp, (IntPtr)gp.GetType().GetField("nativePath", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(gp));
        GdipWindingModeOutline(handle, IntPtr.Zero, 0.25F);

        e.Graphics.DrawPath(Pens.Blue, gp);
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top