让鼠标拖动WinForm窗体

参考网上的代码,写了此文。

先引入API函数ReleaseCapture、SendMessage

  [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 const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息 private const int WM_MOVING = 0x216;//鼠标移动消息 private const int SC_MOVE = 0xF010;//移动信息 private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息 private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区(除了标题栏和边框以外的部分)时发送的消息 private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息 private const int SC_MAXIMIZE = 0xF030;//最大化信息 private const int SC_MINIMIZE = 0xF020;//最小化信息

 

 

再override 一下WindProc函数

protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_MOVING: //如果鼠标移 base.WndProc(ref m);//调用基类的窗口过程——WndProc方法处理这个消息 if (m.Result == (IntPtr)HTCLIENT)//如果返回的是HTCLIENT { m.Result = (IntPtr)HTCAPTION;//把它改为HTCAPTION return;//直接返回退出方法 } break; } base.WndProc(ref m);//如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理 }

 

再override一下OnMouseMove函数

protected override void OnMouseMove(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } base.OnMouseMove(e); }

 

运行程序,可以在窗体任意位置拖动改变窗体的位置,而不仅仅像普通的windows程序那样只能在标题栏用鼠标拖动窗口。

 

外加让textbox内容改变后自动滚动到最底端:

 

private void btnAddText_Click(object sender, EventArgs e) { textBox1.AppendText(DateTime.Now.ToString() + "/r/n"); textBox1.SelectionStart = textBox1.Text.Length; //textBox1.SelectionLength = 0; textBox1.ScrollToCaret(); }

 

实现窗体类似于QQ最小化的动画效果

public const System.Int32 IDANI_OPEN = 1; public const System.Int32 IDANI_CAPTION = 3; [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] struct RECT { public RECT(System.Drawing.Rectangle rectangle) { Left = rectangle.Left; Top = rectangle.Top; Right = rectangle.Right; Bottom = rectangle.Bottom; } public RECT(System.Drawing.Point location, System.Drawing.Size size) { Left = location.X; Top = location.Y; Right = location.X + size.Width; Bottom = location.Y + size.Height; } public System.Int32 Left; public System.Int32 Top; public System.Int32 Right; public System.Int32 Bottom; } [System.Runtime.InteropServices.DllImport("user32.dll")] static extern bool DrawAnimatedRects(System.IntPtr hwnd, int idAni, [System.Runtime.InteropServices.In] ref RECT lprcFrom, [System.Runtime.InteropServices.In] ref RECT lprcTo); [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName); [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] static extern System.IntPtr FindWindowEx(System.IntPtr hwndParent, System.IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [System.Runtime.InteropServices.DllImport("user32.dll")] static extern bool GetWindowRect(System.IntPtr hWnd, out RECT lpRect); ///

/// 动画隐藏/显示窗口 /// /// form窗口 /// true显示、false隐藏 static public void ShowHideAnimated(System.Windows.Forms.Form form, System.Boolean show) { RECT from = new RECT(form.Location, form.Size); RECT to; System.IntPtr hWnd = FindWindowEx(FindWindow("Shell_TrayWnd", null), System.IntPtr.Zero, "TrayNotifyWnd", null); if (hWnd != System.IntPtr.Zero) { GetWindowRect(hWnd, out to); } else { to.Left = System.Windows.Forms.SystemInformation.VirtualScreen.Right - form.Width; to.Top = System.Windows.Forms.SystemInformation.VirtualScreen.Bottom - System.Windows.Forms.SystemInformation.CaptionHeight - (System.Windows.Forms.SystemInformation.FrameBorderSize.Height * 2); to.Right = System.Windows.Forms.SystemInformation.VirtualScreen.Right; to.Bottom = System.Windows.Forms.SystemInformation.VirtualScreen.Bottom; } if (show) { DrawAnimatedRects(form.Handle, IDANI_CAPTION, ref to, ref from); form.Show(); } else { form.Hide(); DrawAnimatedRects(form.Handle, IDANI_CAPTION, ref from, ref to); } }

你可能感兴趣的:(让鼠标拖动WinForm窗体)