返回

美化你的无边框窗体:从移动到缩放再到自定义标题栏

后端

1. 美化窗体的必要性

在Winform中,无边框窗体通常具有更现代的外观,可以为您的应用程序提供更时尚的感觉。但,Winform中简单的无边框窗体可能有一些缺点:

  • 移动困难。 由于没有标题栏,用户无法点击并拖动窗口来移动它。
  • 调整大小困难。 同样,由于没有标题栏,用户无法点击并拖动窗口的边框来调整其大小。
  • 没有标题栏。 无边框窗体没有标题栏,这意味着您无法在其中放置应用程序名称、图标或其他信息。

2. 创建无边框窗体

要创建无边框窗体,您需要将FormBorderStyle属性设置为None。这将禁用窗口的标题栏,并使其看起来像一个没有边框的矩形。

this.FormBorderStyle = FormBorderStyle.None;

3. 允许移动窗体

要允许用户移动窗口,您需要在窗体上添加一个鼠标按下事件处理程序。当用户单击窗口时,该事件处理程序将触发,您可以在其中设置窗口的Location属性以将其移动到新位置。

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        this.Location = new Point(
            this.Location.X + e.X,
            this.Location.Y + e.Y);
    }
}

4. 允许调整窗体大小

要允许用户调整窗口大小,您需要在窗体的四个边框上添加鼠标移动事件处理程序。当用户将鼠标悬停在窗口的边框上时,这些事件处理程序将触发,您可以在其中设置窗口的Size属性以调整其大小。

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.X < this.Width / 2)
    {
        // 左边框
        this.Cursor = Cursors.SizeWE;
    }
    else if (e.X > this.Width - 5)
    {
        // 右边框
        this.Cursor = Cursors.SizeWE;
    }
    else if (e.Y < this.Height / 2)
    {
        // 顶部边框
        this.Cursor = Cursors.SizeNS;
    }
    else if (e.Y > this.Height - 5)
    {
        // 底部边框
        this.Cursor = Cursors.SizeNS;
    }
    else
    {
        this.Cursor = Cursors.Default;
    }
}

private void Form1_Resize(object sender, EventArgs e)
{
    if (this.Cursor == Cursors.SizeWE)
    {
        // 左边框或右边框
        this.Width = e.X;
    }
    else if (this.Cursor == Cursors.SizeNS)
    {
        // 顶部边框或底部边框
        this.Height = e.Y;
    }
}

5. 添加自定义标题栏

要添加自定义标题栏,您需要创建一个新的Panel控件并将其添加到窗体顶部。然后,您可以在Panel控件中添加任何您想要的元素,例如,应用程序名称、图标或按钮。

private void Form1_Load(object sender, EventArgs e)
{
    // 创建新的Panel控件
    Panel titleBar = new Panel();
    titleBar.Dock = DockStyle.Top;
    titleBar.Height = 30;
    titleBar.BackColor = Color.DodgerBlue;

    // 在Panel控件中添加应用程序名称
    Label appName = new Label();
    appName.Text = "My Application";
    appName.Font = new Font("Arial", 12, FontStyle.Bold);
    appName.Location = new Point(10, 10);
    titleBar.Controls.Add(appName);

    // 在Panel控件中添加图标
    PictureBox icon = new PictureBox();
    icon.Image = Image.FromFile("icon.ico");
    icon.Size = new Size(16, 16);
    icon.Location = new Point(5, 5);
    titleBar.Controls.Add(icon);

    // 在Panel控件中添加按钮
    Button minimizeButton = new Button();
    minimizeButton.Text = "_";
    minimizeButton.Size = new Size(20, 20);
    minimizeButton.Location = new Point(this.Width - 35, 5);
    minimizeButton.Click += new EventHandler(minimizeButton_Click);
    titleBar.Controls.Add(minimizeButton);

    Button maximizeButton = new Button();
    maximizeButton.Text = "[]";
    maximizeButton.Size = new Size(20, 20);
    maximizeButton.Location = new Point(this.Width - 55, 5);
    maximizeButton.Click += new EventHandler(maximizeButton_Click);
    titleBar.Controls.Add(maximizeButton);

    Button closeButton = new Button();
    closeButton.Text = "X";
    closeButton.Size = new Size(20, 20);
    closeButton.Location = new Point(this.Width - 75, 5);
    closeButton.Click += new EventHandler(closeButton_Click);
    titleBar.Controls.Add(closeButton);

    // 将Panel控件添加到窗体
    this.Controls.Add(titleBar);
}

private void minimizeButton_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Minimized;
}

private void maximizeButton_Click(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Maximized)
    {
        this.WindowState = FormWindowState.Normal;
    }
    else
    {
        this.WindowState = FormWindowState.Maximized;
    }
}

private void closeButton_Click(object sender, EventArgs e)
{
    this.Close();
}

6. 总结

通过本文,您已经学习了如何创建美观的无边框窗体,以及如何允许用户移动、调整大小和关闭窗口。通过使用这些技术,您可以创建出更现代、更时尚的应用程序。