0

it's hard to explane my problem with words, so I will try with words and images :)

I have context menu control in my win form app (ms visual studio project). It doesn't disappear fully, a part from it stays over my panel control, it's custom panel class (with bordercolor property). The problem occurs only on Windows XP, not on Windows 7.

enter image description here

enter image description here 2. source code:

public class MyPanel : Panel 
{
    private System.Drawing.Color colorBorder = System.Drawing.Color.Transparent;

    public MyPanel()
        : base()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
        this.BorderStyle = BorderStyle.None;
    }

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

    protected override void OnResize(EventArgs e)
    {
        Invalidate();  
    }

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

How to resolve this problem? I can add Invalidate() for the panel (to redraws it) when context menu closed event occurs, but I would like to know why exactly this problem occurs, is it some .NET Framework bug?

1 Answer 1

1
    e.Graphics.DrawRectangle(new System.Drawing.Pen(
        new System.Drawing.SolidBrush(colorBorder), 2), e.ClipRectangle);

Your code in fact asked the Graphics class to draw that rectangle. You used the ClipRectangle property, it represents the bounding rectangle around the parts of the window that need to be repainted. Which was just that intersection between the panel and the context menu. What you meant to draw was a rectangle around the entire panel. Or just a line that separates the panel from the toolstrip, it isn't clear. Guessing at the line as the desired outcome:

protected override void OnPaint(PaintEventArgs e) {
    base.OnPaint(e);
    using (var pen = new Pen(colorBorder, 2)) {
        e.Graphics.DrawLine(pen, Point.Empty, new Point(this.ClientSize.Width, 0));
    }
}
3
  • you just draw a line on the top of the panel, nothing more... I need panel with border
    – vinsa
    Oct 31, 2013 at 15:51
  • Then use new Rectangle(0, 0, this.ClientSize.Width-2, this.ClientSize.Height-2); Please don't hesitate to use answers here to learn how to write correct code, we don't really want to do the job for you :) Hopefully you understood why using ClipRectangle was wrong, that was the point. Oct 31, 2013 at 15:53
  • yes, the problem was ClipRectangle, so I use now new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height) instead and it's working beautifully, thank you.
    – vinsa
    Oct 31, 2013 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.