最小化屏幕时的C#LinearGradientBrush毛刺 [英] C# LinearGradientBrush glitch when minimizing screen

查看:60
本文介绍了最小化屏幕时的C#LinearGradientBrush毛刺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在Winform上创建混合背景:

I have the following code to create a blended background on my winform:

public partial class Aging : Form
{
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        using (var brush = new LinearGradientBrush(this.ClientRectangle,
        Color.Transparent, Color.Transparent, LinearGradientMode.Vertical))
        {
            var blend = new ColorBlend();
            blend.Positions = new[] { 0, 3 / 10f, 1 };
            blend.Colors = new[] { Color.WhiteSmoke, Color.LightSteelBlue, Color.LightSteelBlue };
            brush.InterpolationColors = blend;
            e.Graphics.FillRectangle(brush, this.ClientRectangle);
        }
    }

结果是彩色背景从LightSteelBlue逐渐淡化为WhiteSmoke:

The result is a color background that fades from LightSteelBlue to WhiteSmoke:

问题是,如果我最小化屏幕然后最大化,则应用程序将不再显示背景:

The problem is that if I minimize the screen and then, maximize, the application no longer shows the background:

这是我收到的异常消息:

This is the exception message I'm getting:

System.ArgumentException: Rectangle '{X=0,Y=0,Width=0,Height=0}' cannot have a width or height equal to 0.
at System.Drawing.Drawing2D.LinearGradientBrush..ctor(Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
at AgingStatusDb.Aging.OnPaintBackground(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmEraseBkgnd(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,  
IntPtr wparam, IntPtr lparam)

我不那么精明,也无法弄清故障的根源.任何帮助将不胜感激.

I'm not that savvy and I'm not been able to figure out the source of the glitch. Any help would be appreciated.

推荐答案

要解决该异常,只需遵循异常消息中的内容:

To resolve the exception, just follow what the exception message said:

矩形'{X = 0,Y = 0,Width = 0,Height = 0}'不能具有宽度或高度 等于0.

Rectangle '{X=0,Y=0,Width=0,Height=0}' cannot have a width or height equal to 0.

因此您可以简单地检查ClientRectangle.Width==0ClientRectangle.Height==0然后什么也不做,然后返回即可.

So you can simply check if ClientRectangle.Width==0 or ClientRectangle.Height==0 then do nothing and just return.

但是修复错误后,最小化并还原后,背景将变为黑色.

如果要绘制表单背景,则上面的代码需要进行一些更正:

If you want to draw background of a form, above code needs some corrections:

  • 您需要设置控件以在调整大小时重绘自身.为此,您应该在构造函数中设置this.SetStyle(ControlStyles.ResizeRedraw, true);.

您需要启用双重缓冲以防止闪烁.因此,在构造函数中设置this.DoubleBuffered = true;.

You need to enable double buffering to prevent flicker. So in constructor set this.DoubleBuffered = true;.

代码

public Form1()
{
    InitializeComponent();
    this.DoubleBuffered = true;
    this.SetStyle(ControlStyles.ResizeRedraw, true);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
    if (ClientRectangle.Width == 0 || ClientRectangle.Height == 0) return;
    using (var brush = new LinearGradientBrush(this.ClientRectangle,
        Color.Transparent, Color.Transparent, LinearGradientMode.Vertical))
    {
        var b = new ColorBlend();
        b.Positions = new[] { 0, 3 / 10f, 1 };
        b.Colors = new[] { Color.WhiteSmoke, Color.LightSteelBlue, Color.LightSteelBlue };
        brush.InterpolationColors = b;
        e.Graphics.FillRectangle(brush, this.ClientRectangle);
    }
}

这篇关于最小化屏幕时的C#LinearGradientBrush毛刺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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