C# 无边框窗体之窗体移动实现代码
程序员文章站
2023-12-22 08:49:22
点击窗体任意位置移动窗体:
需要添加命名空间:
using system.runtime.interopservices;复制代码 代码如下:private const...
点击窗体任意位置移动窗体:
需要添加命名空间:
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); // 拖动窗体
}
}