当前位置: 首页 > 新闻动态 > 软件编程

C# 无边框窗体之窗体移动实现代码

作者:用户投稿 浏览: 发布日期:2026-01-16
[导读]:这篇文章介绍了C# 无边框窗体之窗体移动实现代码,有需要的朋友可以参考一下

点击窗体任意位置移动窗体:

需要添加命名空间:

using System.Runtime.InteropServices;
复制代码 代码如下:
private const int WM_NCLBUTTONDOWN = 0x00A1;
private  const int HTCAPTION = 2;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern bool ReleaseCapture();

protected override void OnMouseDown( MouseEventArgs e )
{
    base.OnMouseDown( e );
    if (e.Button == MouseButtons.Left)  // 按下的是鼠标左键             
    {
        ReleaseCapture();   // 释放捕获                
        SendMessage(this.Handle, WM_NCLBUTTONDOWN, (IntPtr)HTCAPTION, IntPtr.Zero);    // 拖动窗体             
    }
}

免责声明:转载请注明出处:http://m.lexweb.cn/news/212932.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!