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

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

相关推荐
葬爱家族小阿杰2 分钟前
python执行测试用例,allure报乱码且未成功生成报告
开发语言·python·测试用例
酷爱码5 分钟前
Python实现简单音频数据压缩与解压算法
开发语言·python
keepquietl13 分钟前
MQTT示例体验(C)
c语言·开发语言
newxtc18 分钟前
【JJ斗地主-注册安全分析报告】
开发语言·javascript·人工智能·安全
程序猿小D28 分钟前
第22节 Node.js JXcore 打包
开发语言·人工智能·vscode·node.js·c#
小猫咪怎么会有坏心思呢35 分钟前
华为OD机试-最短木板长度-二分法(A卷,100分)
java·开发语言·华为od
wo32586614541 分钟前
浪潮交换机配置track检测实现高速公路收费网络主备切换NQA
开发语言·网络·php
知识中的海王1 小时前
Python html 库用法详解
开发语言·python
獨枭1 小时前
配置 macOS 上的 Ruby 开发环境
开发语言·macos·ruby
飞由于度1 小时前
C#中清空DataGridView的方法
开发语言·c#