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

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

相关推荐
棉花糖超人4 分钟前
【操作系统】三、线程
java·开发语言·操作系统
liuyao_xianhui10 分钟前
优选算法_判断字符是否唯一_C++
java·开发语言·数据结构·c++·算法·链表
no_work15 分钟前
python-深度学习快速入门实战-数据集和源码
开发语言·人工智能·python·深度学习·神经网络·cnn
Yupureki17 分钟前
《C++实战项目-高并发内存池》7.大块内存的申请与释放
服务器·c语言·开发语言·c++·算法·哈希算法
2301_8035545219 分钟前
c++中的CAS是什么
java·开发语言·c++
河西石头21 分钟前
powerconfig告别繁琐配置读写---为C#提供了一个快捷的读写配置文件的API
开发语言·c#·高效读写配置文件·c#配置文件·xml读写
IOT-Power23 分钟前
QT 事件驱动架构
开发语言·qt·架构
2401_8898846624 分钟前
模板代码模块化设计
开发语言·c++·算法
qq_2461000531 分钟前
CSDN risk probe 1773588273
开发语言·javascript·ecmascript
2401_8980751234 分钟前
代码生成器优化策略
开发语言·c++·算法