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

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

相关推荐
earthzhang20212 小时前
【1028】字符菱形
c语言·开发语言·数据结构·c++·算法·青少年编程
earthzhang20214 小时前
第3讲:Go垃圾回收机制与性能优化
开发语言·jvm·数据结构·后端·性能优化·golang
纵有疾風起5 小时前
C++——类和对象(3)
开发语言·c++·经验分享·开源
Full Stack Developme5 小时前
java.text 包详解
java·开发语言·python
文火冰糖的硅基工坊6 小时前
[嵌入式系统-135]:主流AIOT智能体开发板
开发语言·嵌入式·cpu
刘梦凡呀6 小时前
C#获取钉钉平台考勤记录
java·c#·钉钉
承渊政道6 小时前
动态内存管理
c语言·c++·经验分享·c#·visual studio
yudiandian20146 小时前
02 Oracle JDK 下载及配置(解压缩版)
java·开发语言
future_studio6 小时前
聊聊 Unity(小白专享、C# 小程序 之 播放器)
unity·小程序·c#
要加油哦~7 小时前
JS | 知识点总结 - 原型链
开发语言·javascript·原型模式