C#无标题栏窗体拖动

要实现C#无标题栏窗体的拖动功能,可以通过以下步骤实现:

  1. 在窗体的构造函数中添加以下代码,以去掉标题栏:

    this.FormBorderStyle = FormBorderStyle.None;

  2. 然后,添加以下代码以处理鼠标按下事件:

    private const int WM_NCHITTEST = 0x0084;
    private const int HT_CAPTION = 0x0002;

    protected override void WndProc(ref Message m)
    {
    switch (m.Msg)
    {
    case WM_NCHITTEST:
    base.WndProc(ref m);
    if ((int)m.Result == HT_CLIENT)
    m.Result = (IntPtr)HT_CAPTION;
    return;
    }
    base.WndProc(ref m);
    }

  3. 最后,通过处理鼠标按下和移动事件来实现拖动功能:

    private bool isDragging = false;
    private Point lastCursor;
    private Point lastForm;

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
    isDragging = true;
    lastCursor = Cursor.Position;
    lastForm = this.Location;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
    if (isDragging)
    {
    int x = lastForm.X - (lastCursor.X - Cursor.Position.X);
    int y = lastForm.Y - (lastCursor.Y - Cursor.Position.Y);
    this.Location = new Point(x, y);
    }
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
    isDragging = false;
    }

将上述代码添加到你的窗体上即可实现无标题栏窗体的拖动功能。

相关推荐
一颗青果8 分钟前
auto | 尾置返回类型 | decltype | using | typedef
java·开发语言·算法
江君是实在人30 分钟前
java jvm 调优
java·开发语言·jvm
kylezhao201941 分钟前
C# 中实现自定义的窗口最大化、最小化和关闭按钮
开发语言·c#
一苓二肆1 小时前
PUMA机械臂matlab仿真正逆解与路径规划
开发语言·matlab
Frank_refuel1 小时前
C++之继承
开发语言·c++
sunfove1 小时前
Python 自动化实战:从识图点击、模拟真人轨迹到封装 EXE 全流程教学
开发语言·python·自动化
傻啦嘿哟1 小时前
Python网页自动化操作全攻略:从入门到实战
开发语言·python·自动化
筱歌儿1 小时前
TinyMCE-----word表格图片进阶版
开发语言·javascript·word
黎雁·泠崖2 小时前
Java面向对象:对象数组进阶实战
java·开发语言
%xiao Q2 小时前
GESP C++四级-216
java·开发语言·c++