C# winform 无边框窗口 移动

给本身留个笔记, 在用wke作界面的时候. 每每须要把winform窗口设置成无边框this

可是WebUI也须要移动窗口, 因此才把之前在易语言中用的方法翻译过来使用spa

第零步: 设置无边框窗口翻译

form属性设置 FormBorderStyle = None

第一步: 引用code

using System.Runtime.InteropServices;

第二步: 声明DLL调用方法orm

//窗口是否最大化, 最大化返回true 不然false
[DllImport("user32.dll")]
public static extern bool IsZoomed(IntPtr hwnd);
//释放捕获
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
//发送消息
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

第三步: 为”鼠标按下(MouseDown)”事件添加功能代码blog

private void Login_MouseDown(object sender, MouseEventArgs e)
{
    //鼠标左键按下
    if (e.Button == MouseButtons.Left)
    {
        if (IsZoomed(this.Handle) == false)
            {
                ReleaseCapture(); 
                SendMessage(this.Handle, 161, 2, 0); 
            }
    }  
}

这里写图片描述
这样功能就实现辣事件