简体   繁体   English

最小化屏幕时的C#LinearGradientBrush故障

[英]C# LinearGradientBrush glitch when minimizing screen

I have the following code to create a blended background on my winform: 我有以下代码在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);
        }
    }

The result is a color background that fades from LightSteelBlue to WhiteSmoke: 结果是颜色背景从LightSteelBlue淡入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: 要解决该异常,只需遵循异常消息中的内容:

Rectangle '{X=0,Y=0,Width=0,Height=0}' cannot have a width or height equal to 0. 矩形'{X = 0,Y = 0,Width = 0,Height = 0}'的宽度或高度不能等于0。

So you can simply check if ClientRectangle.Width==0 or ClientRectangle.Height==0 then do nothing and just return. 因此,您可以简单地检查ClientRectangle.Width==0ClientRectangle.Height==0然后什么也不做就返回。

But after fixing the error you will have a black background after a minimize and restore. 但是,修复错误后,最小化和还原后您将拥有黑色背景。

If you want to draw background of a form, above code needs some corrections: 如果要绘制表单的背景,以上代码需要进行一些更正:

  • You need to set control to redraw itself when resized. 您需要设置控件以在调整大小时重绘自身。 To do so, you should set this.SetStyle(ControlStyles.ResizeRedraw, true); 为此,您应该设置this.SetStyle(ControlStyles.ResizeRedraw, true); in constructor. 在构造函数中。

  • You need to enable double buffering to prevent flicker. 您需要启用双重缓冲以防止闪烁。 So in constructor set this.DoubleBuffered = true; 所以在构造函数中设置this.DoubleBuffered = true; .

Code

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);
    }
}

There is simplest way to do fading background. 有最简单的方法可以使背景变淡。

Create an image with a gradient in a graphics editor or using your code, but save it: 在图形编辑器中或使用您的代码创建带有渐变的图像,但是将其保存:

protected override void OnLoad(EventArgs 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;

        using (var bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))
        {
            var g = Graphics.FromImage(bmp);
            g.FillRectangle(brush, ClientRectangle);
            bmp.Save("background.png", ImageFormat.Png);
        }
    }
}

Run and close the app. 运行并关闭该应用程序。 Then remove that code. 然后删除该代码。

In the end, set the form background image, which was created in the previous step: 最后,设置表单背景图像,该图像是在上一步中创建的:

this.DoubleBuffered = true;
this.BackgroundImageLayout = ImageLayout.Stretch;
this.BackgroundImage = new Bitmap("background.png");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM