删除表单圆角最大化



我正在使用自定义函数来创建圆角表单。我的问题很简单,在最大化窗口时如何删除这些角(又名回到常规矩形形式(?

在调整大小事件中,我正在检查 WindowState 以查看它是否已最大化,并尝试重新绘制表单边框,但它似乎不起作用。

public partial class ClientListForm : Form
{
public ClientListForm()
{
InitializeComponent();
RoundBorderForm(this);
}
private void ClientListForm_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Maximized)
{
SharpBorderForm(this);
} else
{
RoundBorderForm(this);
}
}
}
public static void RoundBorderForm(Form frm)
{
Rectangle Bounds = new Rectangle(0, 0, frm.Width, frm.Height);
int CornerRadius = 18;
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddArc(Bounds.X, Bounds.Y, CornerRadius, CornerRadius, 180, 90);
path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y, CornerRadius, CornerRadius, 270, 90);
path.AddArc(Bounds.X + Bounds.Width - CornerRadius, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
path.AddArc(Bounds.X, Bounds.Y + Bounds.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
path.CloseAllFigures();
frm.Region = new Region(path);
frm.Show();
}
public static void SharpBorderForm(Form frm)
{
frm.Region = new Region(new Rectangle(0, 0, frm.Width, frm.Height));
frm.Show();
}

窗口看起来最大化了,但我仍然有圆角。

你对frm.Show()的调用什么也没做,因为你的frm已经显示出来了。在这种情况下,您需要调用Invalidate方法。无论如何frm构造函数中对frm.Show()的调用很奇怪:\

此外,您的SharpBorderFormRoundBorderForm不需要是静态的:

private void RoundBorderForm()
{
var bounds = new Rectangle(0, 0, Width, Height);
var cornerRadius = 18;
var path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddArc(bounds.X, bounds.Y, cornerRadius, cornerRadius, 180, 90);
path.AddArc(bounds.X + bounds.Width - cornerRadius, bounds.Y, cornerRadius, cornerRadius, 270, 90);
path.AddArc(bounds.X + bounds.Width - cornerRadius, bounds.Y + bounds.Height - cornerRadius, cornerRadius, cornerRadius, 0, 90);
path.AddArc(bounds.X, bounds.Y + bounds.Height - cornerRadius, cornerRadius, cornerRadius, 90, 90);
path.CloseAllFigures();
Region = new Region(path);
Invalidate();
}
private void SharpBorderForm()
{
Region = new Region(new Rectangle(0, 0, Width, Height));
Invalidate();
}

此外,您无需在此处订阅Resize活动。您可以只覆盖OnResize受保护的方法:

protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (WindowState == FormWindowState.Maximized)
SharpBorderForm();
else
RoundBorderForm();
}

最新更新