简体   繁体   English

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

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

I have a code were a form with no border changes it size depending on a string.我有一个代码是一个没有边框的表单,它的大小取决于一个字符串。

It's like a notification form,这就像一个通知表,

Some times the form does not resize correctly, and part of it looks transparent.有时表单无法正确调整大小,并且其中一部分看起来是透明的。

I know its transparent because that transparent part calls all the events, like click or mouse wheel even when I am seeing the program on the background of it.我知道它是透明的,因为即使我在它的背景上看到程序,透明部分也会调用所有事件,例如单击或鼠标滚轮。 And have checked the Width property of the form and its OK, its greater than the part its showing.并检查了表单的 Width 属性及其确定,它大于其显示的部分。

Here is the code where the form changes its size, it's the only method where it changes color or size.这是表单改变大小的代码,它是改变颜色或大小的唯一方法。

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

I send the exact same text and some times it changes its size correctly.我发送完全相同的文本,有时它会正确更改其大小。

Example Fails:示例失败:

示例失败

Example OK:示例确定:

示例确定

Just let the Label AutoSize to the contents of the String, then resize your form based on that.只需将 Label AutoSize设置为字符串的内容,然后根据此调整表单的大小。 It works with multi-line strings as well.它也适用于多行字符串。 Why all the measuring?....为什么要测量?......

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

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

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