很难用文字解释我的问题,所以我会尝试使用文字和图像:)
我在win form app(ms visual studio项目)中有上下文菜单控件。 它不会完全消失,它的一部分停留在我的面板控件上,它是自定义面板类(带bordercolor属性)。问题仅发生在Windows XP上,而不是Windows 7上。
2.源代码:
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;
}
}
}
如何解决此问题?当上下文菜单关闭事件发生时,我可以为面板添加Invalidate()(重绘它),但我想知道为什么会出现这个问题,是否有一些.NET Framework错误?
答案 0 :(得分:1)
e.Graphics.DrawRectangle(new System.Drawing.Pen(
new System.Drawing.SolidBrush(colorBorder), 2), e.ClipRectangle);
您的代码实际上要求Graphics类绘制该矩形。您使用了ClipRectangle属性,它表示需要重新绘制的窗口部分周围的边界矩形。这只是面板和上下文菜单之间的交集。您想要绘制的是整个面板周围的矩形。或者只是将面板与工具条分开的一条线,目前尚不清楚。在线上猜测是理想的结果:
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));
}
}