Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 卡在OnPaint循环中,带有从面板派生的自定义控件_Winforms_C# 4.0_User Controls_Panel_Onpaint - Fatal编程技术网

Winforms 卡在OnPaint循环中,带有从面板派生的自定义控件

Winforms 卡在OnPaint循环中,带有从面板派生的自定义控件,winforms,c#-4.0,user-controls,panel,onpaint,Winforms,C# 4.0,User Controls,Panel,Onpaint,我有一个定制面板,可以显示纯色背景、渐变背景或图片 我已覆盖BackgroundImage属性和OnPaint方法,如下所示: protected override void OnPaint(PaintEventArgs pe){ Rectangle rc = new Rectangle(0, 0, this.Width, this.Height); if (this.DrawStyle != DrawStyle.Picture){ base.Background

我有一个定制面板,可以显示纯色背景、渐变背景或图片

我已覆盖BackgroundImage属性和OnPaint方法,如下所示:

protected override void OnPaint(PaintEventArgs pe){
    Rectangle rc = new Rectangle(0, 0, this.Width, this.Height);
    if (this.DrawStyle != DrawStyle.Picture){
        base.BackgroundImage = null;

            if (this.DrawStyle == DrawStyle.Solid){
                using (SolidBrush brush = new SolidBrush(this.PrimaryColor))
                    pe.Graphics.FillRectangle(brush, rc);
            }
            else if (this.DrawStyle == DrawStyle.Gradient){
                using (LinearGradientBrush brush = 
                       new LinearGradientBrush(
                           rc, this.PrimaryColor, this.SecondaryColor, this.Angle))
                pe.Graphics.FillRectangle(brush, rc);
            }
        }
    else if (this._BGImage != null)
        base.BackgroundImage = this._BGImage;
}

public override Image BackgroundImage{
    get { return this._BGImage; }
    set { this._BGImage = value; }
}
原因是控件必须足够灵活,以便在运行时更改背景类型(实心、渐变或图片),因此控件保留设置的背景图像,并在必要时显示

然而,我遇到了一个问题

如果我不设置背景图像,就没有问题。OnPaint大约被调用了5次,然后就可以正常运行了

然而,当我设置背景图像时,它似乎变得疯狂,一遍又一遍地调用绘画

这显然与覆盖背景图像有关,这是一个问题,因为它挂起了,在我更改面板外观之前,面板上没有任何内容得到更新

所以我想我的问题是,当我设置背景图像时,为什么它会卡在这个OnPaint循环中?

请注意:

// base.BackgroundImage = this._BGImage;
它导致它递归地绘制自己。决不应在绘制例程中设置属性

此外,重写BackgroundImage并不能完成任何事情,如果确实重写了属性,则应该只有在该位置指定base.BackgroundImage值。考虑删除它。

我将您的代码修改为:

protected override void OnPaintBackground(PaintEventArgs e) {
  if (this.DrawStyle == DrawStyle.Picture) {
     base.OnPaintBackground(e);
  }
}

protected override void OnPaint(PaintEventArgs e) {
  Rectangle rc = this.ClientRectangle;
  if (this.DrawStyle == DrawStyle.Solid) {
    using (SolidBrush brush = new SolidBrush(this.PrimaryColor))
      e.Graphics.FillRectangle(brush, rc);
  } else if (this.DrawStyle == DrawStyle.Gradient) {
    using (LinearGradientBrush brush =
           new LinearGradientBrush(
               rc, this.PrimaryColor, this.SecondaryColor, this.Angle))
      e.Graphics.FillRectangle(brush, rc);
  }
  base.OnPaint(e);
}

确保添加
this.DoubleBuffered=true
this.ResizeRedraw=true以避免不必要的闪烁。

好的,谢谢。我确实找到了解决办法。编辑以反映更改。@Will我不会这样做
base.BackgroundImage=null在你的画图通话中。你能告诉那个家伙在他的属性设置器中指定base.BackgroundImage吗?@LarsTech我执行了你的建议。谢谢。由于最新更新删除了实际问题,所以我将帖子回滚到原始问题。