0

I created Panel class "GpanelBorder" which draw border in custom panel with code:

namespace GetterControlsLibary
{
    public class GpanelBorder : Panel
    {
        private Color colorBorder;
        public GpanelBorder()
        {
            SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawRectangle(
                new Pen(
                    new SolidBrush(colorBorder), 8),
                    e.ClipRectangle);
        }

        public Color BorderColor
        {
            get
            {
                return colorBorder;
            }
            set
            {
                colorBorder = value;
            }
        }
    }
}

Works fine but when i in design mode, mouse click inside panel and move mouse or drag other control over this panel, artifacts are created (picture below)

enter image description here

How to fix it?

aybe
  • 15,516
  • 9
  • 57
  • 105
  • See if any of the flags play bad with designer, if so then detect design mode and set flags accordingly : https://stackoverflow.com/questions/1166226/detecting-design-mode-from-a-controls-constructor – aybe Jul 06 '20 at 19:11

2 Answers2

0

The .ClipRectangle parameter does NOT necessarily represent the entire area of your control to be painted within. It may represent a SMALLER portion of your control indicating just that portion needs to be repainted. You can use the "clip rectangle" to redraw only a portion of your control in situations where it would be too costly to compute and repaint the entire control. If that situation doesn't apply to you, then use the ClientRectangle to get the bounds of your entire control and use that to draw your border. Also, you are LEAKING a PEN and a SOLIDBRUSH. You need to .Dispose() of those resources when you're done with them. This is best done with a using block:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    using (SolidBrush sb = new SolidBrush(colorBorder), 8))
    {
        using (Pen p = new Pen(sb))
        {
            e.Graphics.DrawRectangle(p, this.ClientRectangle);
        }
    }
}

You may need to create a new Rectangle based on ClientRectangle and adjust that to your liking before drawing.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

Thanks, Works great!

but corectly code is

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            using (SolidBrush sb = new SolidBrush(colorBorder))
            {
                using (Pen p = new Pen(colorBorder, 2))
                {
                    e.Graphics.DrawRectangle(p, this.ClientRectangle);
                }
            }
        }
  • That's not how you had it originally written (look back at your code!)...but I'm glad you figured it out. ;) If my post below was helpful, then give it an upvote or an accept. – Idle_Mind Jul 07 '20 at 00:06