首页 > 解决方案 > 可拖动的窗体栏

问题描述

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

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

标签: windows-forms-designer

解决方案


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

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



}

推荐阅读