winform窗体无边框拖动

1:引用命名空间
using System.Runtime.InteropServices;
2:想要拖动窗体的控件绑定MouseDown事件

点击查看代码
//窗体移动
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();

[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

private void _MouseDown(object sender, MouseEventArgs e)
{
    if (e.Clicks == 1)
    {
        //窗体移动
        if (e.Button == MouseButtons.Left)
        {
            //释放鼠标捕捉
            ReleaseCapture(); 

            //发送左键点击的消息至该窗体(标题栏)
            SendMessage(this.Handle, 0xA1, 0x02, 0);
        }
    }
}
posted @ 2024-12-07 14:21  小卡拉咪  阅读(39)  评论(0)    收藏  举报