调整大小时,OnPaint会离开路径

时间:2012-07-17 23:20:23

标签: c# winforms

我正在创建一个带自定义边框的表单。 我想使用onPaint覆盖来绘制自己的边框。 不幸的是,当我使用下面的代码时,我得到了图像的痕迹。

有谁知道如何解决这个问题? 是的,我已将DoubleBuffered设置为true。

protected override void OnPaint(PaintEventArgs e)
{
  Graphics g = e.Graphics;

  // Background
  g.FillRectangle(new SolidBrush(ColorTranslator.FromHtml("#EFEFF7")), new Rectangle(0, 0, this.Width - 1, this.Height - 1));

  // Outer Borders
  g.DrawImage(Resources.Resource.OuterBorderTopLeft, new Rectangle(0, 0, 13, 13));
  g.DrawImage(Resources.Resource.OuterBorderTopLeft1, new Rectangle(13, 0, 221, 13));
  g.DrawImage(Resources.Resource.OuterBorderTopLeft2, new Rectangle(0, 13, 13, 221));
  g.DrawImage(Resources.Resource.OuterBorderTopRight, new Rectangle(this.Size.Width - 13, 0, 13, 13));
  g.DrawImage(Resources.Resource.OuterBorderTopRight1, new Rectangle(this.Size.Width - 13, 13, 13, 221));
  g.DrawImage(Resources.Resource.OuterBorderTopRight2, new Rectangle(this.Size.Width - 234, 0, 221, 13));
  g.DrawImage(Resources.Resource.OuterBorderBottomLeft, new Rectangle(0, this.Size.Height - 13, 13, 13));
  g.DrawImage(Resources.Resource.OuterBorderBottomLeft1, new Rectangle(0, this.Size.Height - 234, 13, 221));
  g.DrawImage(Resources.Resource.OuterBorderBottomLeft2, new Rectangle(13, this.Size.Height - 13, 221, 13));
  g.DrawImage(Resources.Resource.OuterBorderBottomRight, new Rectangle(this.Size.Width - 13, this.Size.Height - 13, 13, 13));
  g.DrawImage(Resources.Resource.OuterBorderBottomRight1, new Rectangle(this.Size.Width - 234, this.Size.Height - 13, 221, 13));
  g.DrawImage(Resources.Resource.OuterBorderBottomRight2, new Rectangle(this.Size.Width - 13, this.Size.Height - 234, 13, 221));
  base.OnPaint(e);
}

1 个答案:

答案 0 :(得分:9)

尝试覆盖resize方法:

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

或在构造函数中,您可以添加另一个设置:

public Form1() {
  InitializeComponent();
  this.DoubleBuffered = true;
  this.ResizeRedraw = true;
}