繁体   English   中英

通过双击notifyIcon调整表单大小时不显示元素

[英]Elements are not displayed when resizing the form via double click on notifyIcon

我的 Winforms 程序有一个颜色渐变,它通过Linear Gradient Brush用从一种颜色到另一种颜色的渐变绘制背景。 它还有一个notifyIcon ,可以将工具最小化到系统托盘,并在双击图标时再次显示它。 显示部分运行良好,但表单上的所有元素(如标签、按钮等)都用红色交叉。

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

LinearGradientBrush 的代码:

private void Form1_Paint(object sender, PaintEventArgs e)
{
       using (LinearGradientBrush LiGraBrush = new LinearGradientBrush(this.ClientRectangle, Color.LightBlue, Color.MediumBlue, 90F))
       {
               e.Graphics.FillRectangle(LiGraBrush, this.ClientRectangle);
       }
}

调整大小事件的代码:

private void Form1_Resize(object sender, EventArgs e)
{
            if (this.WindowState == FormWindowState.Minimized)
            {
                Hide();
                notifyIcon.Visible = true;
            }
  this.Invalidate();
}

渐变代码来自本教程,我还从中获得了this.Invalidate

我还在this.Invalidate的双击事件中使用了notifyIcon

(使用 Visual Studio Enterprise 2019、.NET 5.0 和 C#)

解决方案:检查ClientRectangle的Width和Height是否大于0,如下:

if (this.ClientRectangle.Width > 0 && this.ClientRectangle.Height > 0)
{
    //paint
}

暂无
暂无

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

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