通过winform中的Region属性删除表单设计

问题描述 投票:0回答:1

我使用region属性定制了winform-design,如下所示,

Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, varPassedInConstructor * 9, Height, 10, 10));

这里通过以下代码在新线程中调用winform

new Thread(new ThreadStart(() => {
            toast toast = new toast(message);
            toast.Show(nativeWindow);
            toast.Refresh();

            Thread.Sleep(3000);

            while (toast.Opacity > 0)
            {
                toast.Opacity -= 0.04;
                Thread.Sleep(100);
            }

            toast.Close();
            toast.Dispose();
        })).Start();

一切顺利,表格初始显示正确,但在突然关闭之前,通过Region应用的更改消失,表单看起来像是在设计时的那个。

图像一,最初形式显示时,enter image description here

图片二,就在形式关闭之前,enter image description here

我尝试了许多不同的事情,我没有得到究竟问题是什么,所以所有的帮助将不胜感激。

winforms region
1个回答
0
投票

最后,我得到了修复,而不是使用CreateRoundRectRgnGDI32使用GraphicsPath方法如下,

private void SetRegion()
{
    var GP = RoundedRect(this.ClientRectangle, 5);
    this.Region = new Region(GP);
}

这里是RoundRect函数的代码(Credit转到https://stackoverflow.com/a/33853557/3531672),

public static GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
    int diameter = radius * 2;
    Size size = new Size(diameter, diameter);
    Rectangle arc = new Rectangle(bounds.Location, size);
    GraphicsPath path = new GraphicsPath();

    if (radius == 0)
    {
        path.AddRectangle(bounds);
        return path;
    }

    // top left arc  
    path.AddArc(arc, 180, 90);

    // top right arc  
    arc.X = bounds.Right - diameter;
    path.AddArc(arc, 270, 90);

    // bottom right arc  
    arc.Y = bounds.Bottom - diameter;
    path.AddArc(arc, 0, 90);

    // bottom left arc 
    arc.X = bounds.Left;
    path.AddArc(arc, 90, 90);

    path.CloseFigure();
    return path;
}

然后在构造函数中只是设置了表单本身的大小并调用了上面定义的SetRegion,

this.Width = toastMessage.Length * 9;
SetRegion();

另外请注意,我建议覆盖OnSizeChanged并简单地在其中调用SetRegion

© www.soinside.com 2019 - 2024. All rights reserved.