可拖动的窗体栏

问题描述

我正在制作一个 windows 窗体应用程序只是为了测试我的设计技巧,但我发现轮廓很难看,所以我制作了自己的最小化和关闭按钮,但我不确定如何制作一个面板可以拖来拖去。有人可以帮我吗?

顺便说一句,代码是C#。

解决方法

使用事件,我们可以在左键单击 (MouseDown) 和 MouseMove 时获取当前位置,我们获取当前窗口位置减去我们之前所在的位置,并加上我们拖动鼠标的距离。

public partial class Form1 : Form 
{
    private Point windowLocation;

    public Form1()
    {
        InitializeComponent();
    }

    
    private void Form1_MouseDown(object sender,MouseEventArgs e)
    {
        this.windowLocation = e.Location;

    }

    private void Form1_MouseMove(object sender,MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            // Refers to the Form location (or whatever you trigger the event on)
            this.Location = new Point(
                (this.Location.X - windowLocation.X) + e.X,(this.Location.Y - windowLocation.Y) + e.Y
            );

            this.Update();
        }
    }



}