Search code examples
winformsdrawrectangle

Graphics remove the top line of a rectangle


On a winform, is there a way to draw a rectangle like this (without top) using the DrawRectangle method or a workaround?

Rectangle without top


Solution

  • You can try using the SetClip method:

    private void DrawTopless(Graphics g, Rectangle r) {
      g.SetClip(new Rectangle(r.Left, r.Top, r.Width + 1, 10), CombineMode.Exclude);
      g.DrawRectangle(Pens.Red, r);
      g.ResetClip();
    }