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

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

相关推荐
t198751281 分钟前
基于多尺度特征融合与自适应权重优化的水下图像对比度与边缘增强MATLAB方法
开发语言·matlab
chilavert31836 分钟前
程序员面试经典问题解答:java篇-2
开发语言·python
senijusene38 分钟前
TCP并发服务器:poll和epoll的多路复用
开发语言·php
浅碎时光8071 小时前
Qt (按钮/显示/输入/容器类控件 布局管理器)
开发语言·qt
bubiyoushang8881 小时前
OFDM系统信道估计MATLAB实现(LS、MMSE、DCT、LRMMSE方法)
开发语言·网络·matlab
Felven1 小时前
C. Dora and Search
c语言·开发语言
唐青枫1 小时前
C#.NET Span 深入解析:零拷贝内存切片与高性能实战
c#·.net
John Song3 小时前
Python创建虚拟环境的方式对比与区别?
开发语言·python
搞程序的心海3 小时前
Python面试题(一):5个最常见的Python基础问题
开发语言·python
Nuopiane9 小时前
关于C#/Unity中单例的探讨
java·jvm·c#