Win32函数实现鼠标拖动窗口

[DllImport("User32.DLL")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("User32.DLL")]
private static extern bool ReleaseCapture();
private const uint WM_SYSCOMMAND = 0x0112;
private const int WM_SETREDRAW = 11; 
private const int SC_MOVE = 61456;
private const int HTCAPTION = 2;
public static void  MoveWindow(IntPtr handle)
{
      ReleaseCapture();
      SendMessage(handle, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 5);
}

要实现鼠标点击拖动,只需要在鼠标MouseDown事件中 调用MoveWindow方法即可,参数传入窗口句柄。

private void XXXX_MouseDown(object sender, MouseEventArgs e)
{
     MoveWindow(XXXX.Handle);
 }
posted @ 2017-04-12 17:14 LiGreat 阅读(...) 评论(...) 编辑 收藏