渐变面板显示最小化,然后还原时,红十字会 [英] Gradient Panel shows red cross when minimized and then restored

查看:152
本文介绍了渐变面板显示最小化,然后还原时,红十字会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么发生这种情况,但我创建了下面的代码是一个渐变面板,面板随后停靠在屏幕的左侧。



当表单的尺寸重新调整它显示正确,但是如果你最小化的形式,然后还原它,你得到一个大红色的X,而不是梯度。



任何人都可以?发现错误

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Drawing.Drawing2D;
使用System.Text;使用System.Windows.Forms的
;

公共类GradientPanel:面板
{
私人颜色ColorA = Color.LightBlue;
私人颜色ColorB = Color.Red;
私人LinearGradientMode GradientFillStyle = LinearGradientMode.ForwardDiagonal;
私人刷gradientBrush;

众彩colourStart
{
{返回ColorA; }
集合{ColorA =价值;无效(); }
}
众彩colourEnd
{
{返回ColorB; }
集合{ColorB =价值;无效(); }
}
公共LinearGradientMode colourGradientStyle
{
{返回GradientFillStyle; }
集合{GradientFillStyle =价值;无效(); }
}

公共GradientPanel()
{
handlerGradientChanged =新的EventHandler(GradientChanged);
ResizeRedraw = TRUE;
}

私人事件处理handlerGradientChanged;

保护覆盖无效OnPaintBackground(System.Windows.Forms.PaintEventArgs E)
{
gradientBrush =新一个LinearGradientBrush(ClientRectangle,ColorA,ColorB,GradientFillStyle);

e.Graphics.FillRectangle(gradientBrush,ClientRectangle);
}

保护覆盖无效的Dispose(BOOL处置)
{
如果(处置)
{
如果(gradientBrush!= NULL) gradientBrush.Dispose();
}
base.Dispose(处置);
}

保护覆盖无效onResize受到(EventArgs的EventArgs的)
{
的Invalidate();
//base.OnResize(eventargs);
}
保护覆盖无效OnSizeChanged(EventArgs的发送)
{
的Invalidate();
//base.OnSizeChanged(e);
}
私人无效GradientChanged(对象发件人,EventArgs五)
{
如果(gradientBrush!= NULL)gradientBrush.Dispose();
gradientBrush = NULL;
的Invalidate();
}

}


解决方案

我要避免建立新的一个LinearGradientBrush OnPaintBackground 处理程序。



我的猜测是,你可能会打你的GDI对象限制(您可以在任务管理器中查看),因为我不认为你是正确处理您的画笔做。



移动这构造,或在当颜色和样式属性来更改被称为(并创造新的前一个以前的处理)功能。



下面是同时仍然允许的属性来改变颜色等...



编辑你可以做什么的例子属性:

 众彩colourStart 
{
{返回ColorA; }
集合{ColorA =价值; RefershBrush(); }
}
众彩colourEnd
{
{返回ColorB; }
集合{ColorB =价值; RefershBrush(); }
}
公共LinearGradientMode colourGradientStyle
{
{返回GradientFillStyle; }
集合{GradientFillStyle =价值; RefershBrush(); }
}

添加功能:

 私人无效RefershBrush()
{
//我觉得这IF块应该没有问题的工作,一直与画笔
如果工作一段时间(gradientBrush!= NULL)
{
gradientBrush.Dispose();
}

gradientBrush =新一个LinearGradientBrush(ClientRectangle,ColorA,ColorB,GradientFillStyle);
的Invalidate();
}


I have no idea why this is happening, but I created the below code which is a gradient panel, the panel is then docked to the left of the screen.

When the form is re-sized it displays correctly, however if you minimize the form and then restore it you get a big red X instead of the gradient.

Can anyone spot the error?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

public class GradientPanel : Panel
{
    private Color ColorA = Color.LightBlue;
    private Color ColorB = Color.Red;
    private LinearGradientMode GradientFillStyle = LinearGradientMode.ForwardDiagonal;
    private Brush gradientBrush;

    public Color colourStart
    {
        get { return ColorA; }
        set { ColorA = value; Invalidate(); }
    }
    public Color colourEnd
    {
        get { return ColorB; }
        set { ColorB = value; Invalidate(); }
    }
    public LinearGradientMode colourGradientStyle
    {
        get { return GradientFillStyle; }
        set { GradientFillStyle = value; Invalidate(); }
    }

    public GradientPanel()
    {
        handlerGradientChanged = new EventHandler(GradientChanged);
        ResizeRedraw = true;        
    }

    private EventHandler handlerGradientChanged;

    protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
    {
        gradientBrush = new LinearGradientBrush(ClientRectangle, ColorA, ColorB, GradientFillStyle);

        e.Graphics.FillRectangle(gradientBrush, ClientRectangle);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (gradientBrush != null) gradientBrush.Dispose();
        }
        base.Dispose(disposing);
    }

    protected override void OnResize(EventArgs eventargs)
    {
        Invalidate();
        //base.OnResize(eventargs);
    }
    protected override void OnSizeChanged(EventArgs e)
    {
        Invalidate();
        //base.OnSizeChanged(e);
    }
    private void GradientChanged(object sender, EventArgs e)
    {
        if (gradientBrush != null) gradientBrush.Dispose();
        gradientBrush = null;
        Invalidate();
    }

}

解决方案

I would avoid creating a new LinearGradientBrush in the OnPaintBackground handler.

My guess is you could be hitting your GDI object limit (which you can check in task manager) as I don't think you are disposing your brushes correctly.

Move this to the constructor, or in a function that is called when the colours and styles properties change (and dispose of the previous before creating the new one)

Here is an example of what you can do while still allowing the properties to change the colours etc...

Edit properties:

public Color colourStart
{
    get { return ColorA; }
    set { ColorA = value; RefershBrush(); }
}
public Color colourEnd
{
    get { return ColorB; }
    set { ColorB = value; RefershBrush(); }
}
public LinearGradientMode colourGradientStyle
{
    get { return GradientFillStyle; }
    set { GradientFillStyle = value; RefershBrush(); }
}

Add function:

private void RefershBrush()
{
    //I think this IF block should work with no problems, been a while working with brush
    if(gradientBrush != null)
    {
        gradientBrush.Dispose();
    }

    gradientBrush = new LinearGradientBrush(ClientRectangle, ColorA, ColorB, GradientFillStyle);
    Invalidate();
}

这篇关于渐变面板显示最小化,然后还原时,红十字会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆