{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int SendMessage(
IntPtr hWnd,
[param: MarshalAs(UnmanagedType.U4)]
uint Msg,
[param: MarshalAs(UnmanagedType.U4)]
uint wParam,
[param: MarshalAs(UnmanagedType.I4)]
int lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReleaseCapture();
public const uint WM_NCLBUTTONDOWN = 0xA1; // 161
public const uint HTCAPTION = 2;
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
}
public static void MoveObject(IntPtr hWnd)
{
SafeNativeMethods.ReleaseCapture();
SafeNativeMethods.SendMessage(hWnd, SafeNativeMethods.WM_NCLBUTTONDOWN, SafeNativeMethods.HTCAPTION, 0);
// SafeNativeMethods.SendMessage(hWnd, SafeNativeMethods.WM_SYSCOMMAND, SafeNativeMethods.SC_MOVE + SafeNativeMethods.HTCAPTION, 0);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
MoveObject((IntPtr)(new WindowInteropHelper(this).Handle.ToInt32()));
}
拖动
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0xA3://拦截鼠标非客户区左键双击消息,决定窗体是否最大化显示
if (this.MaximizeBox)
{
base.WndProc(ref m);
}
return;
case 0x84:
base.WndProc(ref m);
Point lpint = new Point((int)m.LParam);
lpint.Offset(-this.Left, -this.Top);
Point lpint1 = new Point((int)m.LParam);
lpint1.Offset(-this.Right, -this.Bottom);
// 设置可托动的区域
Rectangle Client = new Rectangle(0, 3, this.Width, 25);
if (Client.Contains(lpint))
{
m.Result = (IntPtr)0x2;
return;
}
//上
Rectangle topboder = new Rectangle(5, 0, this.Width - 10, 5);
if (topboder.Contains(lpint))
{
m.Result = (IntPtr)0xC;//上边框HTTOP,上方的窗体缩放
return;
}
//下
Rectangle Downboder = new Rectangle(-this.Width, -3, this.Width, 3);
if (Downboder.Contains(lpint1))
{
m.Result = (IntPtr)0xF;//下边框HTBOTTOM,下方的窗体缩放
return;
}
//左
Rectangle leftboder = new Rectangle(0, 5, 5, this.Height - 10);
if (leftboder.Contains(lpint))
{
m.Result = (IntPtr)0xA;//左边框HTLEFT,左方的窗体缩放
return;
}
//右
Rectangle rightboder = new Rectangle(-3, -this.Height, 3, this.Height);
if (rightboder.Contains(lpint1))
{
m.Result = (IntPtr)0xB;//右边框HTRIGHT,右方的窗体缩放
return;
}
//下right角
Rectangle rightDownboder = new Rectangle(-6, -6, 30, 10);
if (rightDownboder.Contains(lpint1))
{
m.Result = (IntPtr)0x11;//下right角HTBOTTOMRIGHT,下right角窗体缩放
return;
}
////左下
Rectangle LeftDownboder = new Rectangle(0, -10, 30, 10);
if (LeftDownboder.Contains(lpint1))
{
m.Result = (IntPtr)0xA;//下right角HTBOTTOMRIGHT,下right角窗体缩放
return;
}
////左上
Rectangle LeftUpboder = new Rectangle(0, 0, 30, 10);
if (LeftUpboder.Contains(lpint1))
{
m.Result = (IntPtr)0xD;//下right角HTBOTTOMRIGHT,下right角窗体缩放
return;
}
//右上
Rectangle RightUpboder = new Rectangle(-6, -this.Height, 30, 10);
if (RightUpboder.Contains(lpint1))
{
m.Result = (IntPtr)0xE;//下right角HTBOTTOMRIGHT,下right角窗体缩放
return;
}
return;
}
base.WndProc(ref m);
}