Reputation: 1242
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.
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?
Upvotes: 0
Views: 628
Reputation: 942080
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));
}
}
Upvotes: 1