如何解决使我的表单透明的调整大小错误?

How do I solve this resize error that makes my form transparent?

我有一个代码是一个没有边框的表单,它会根据字符串改变大小。

这就像一个通知表格,

有时表单无法正确调整大小,并且部分看起来是透明的。

我知道它是透明的,因为透明部分会调用所有事件,例如单击或鼠标滚轮,即使我在它的背景上看到程序也是如此。 并检查了表格的宽度 属性 没问题,它大于显示的部分。

这是更改表单大小的代码,这是唯一更改颜色或大小的方法。

private void ChangeNotification(string Noti, Color C)
{
    string[] Lines = Noti.Split(new[] { '\r', '\n' });
    string Max = "";
    Lines.ToList().ForEach(s =>
    {
        if (s.Length > Max.Length)
            Max = s;
    });
    using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
    {
        SizeF size = graphics.MeasureString(Max, lblInfo.Font);
        Width = (int)size.Width + scroll.Width + 40;
        Height = Lines.Count() * (int)size.Height;
        Top = Screen.PrimaryScreen.WorkingArea.Height - Height;
        Left = Screen.PrimaryScreen.WorkingArea.Width - Width;
    }
    this.BackColor = C;
    lblInfo.Text = Noti;
}

我发送了完全相同的文本,有时它正确地改变了它的大小。

示例失败:

示例确定:

让标签 AutoSize 成为字符串的内容,然后根据它调整表单的大小。它也适用于多行字符串。为什么要测量?!...

public void ChangeNotification(string Noti, Color C)
{
    lblInfo.Text = Noti;
    this.BackColor = C;
    this.Size = new Size(lblInfo.Size.Width + lblInfo.Location.X * 2, lblInfo.Size.Height + lblInfo.Location.Y * 2);
    this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width, Screen.PrimaryScreen.WorkingArea.Height - Height);
}