通过控件拖动窗体
适用于无边框窗体,自定义标题栏
普兰店网站建设公司创新互联建站,普兰店网站设计制作,有大型网站制作公司丰富经验。已为普兰店上千多家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的普兰店做网站的公司定做!
之前在网上找的,如http://www.cnblogs.com/yuxuan/archive/2010/09/25/1834346.html这篇博文,开始移动时会瞬移几个像素.....
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing;
- namespace EW.XIS.Common
- {
- ///
- ///
说明:窗体拖动类,通过这个类提供的方法实现窗体上任意控件可辅助拖动窗体 - ///
- public class DragFormClass
- {
- private static bool isMouseDown = false;
- private static Point mouseOffset;
- private static Form _form;
- ///
- /// 在窗体上增加拖拽事件
- ///
- /// 控件对象
- public static void bindControl(Control control)
- {
- //如果控件为空
- if (control == null)
- {
- return;
- }
- _form = control.FindForm();
- //增加鼠标拖动窗体移动事件
- control.MouseMove += new MouseEventHandler(control_MouseMove);
- control.MouseDown += new MouseEventHandler(control_MouseDown);
- control.MouseUp += new MouseEventHandler(control_MouseUp);
- }
- ///
- /// 鼠标按下之时,保存鼠标相对于窗体的位置
- ///
- ///
- ///
- private static void control_MouseDown(object sender, MouseEventArgs e)
- {
- if (Control.MouseButtons == MouseButtons.Left)
- {
- Point p=_form.PointToClient(Control.MousePosition);
- mouseOffset = new Point(-p.X, -p.Y);
- isMouseDown = true;
- }
- }
- ///
- /// 移动鼠标的时候改变窗体位置
- ///
- ///
- ///
- private static void control_MouseMove(object sender, MouseEventArgs e)
- {
- if (Control.MouseButtons == MouseButtons.Left)
- {
- if (isMouseDown)
- {
- Point mouse = Control.MousePosition;
- mouse.Offset(mouseOffset.X, mouseOffset.Y);
- _form.Location = new Point(mouse.X, mouse.Y);
- }
- }
- }
- ///
- /// 松开鼠标的时候,重设事件
- ///
- ///
- ///
- private static void control_MouseUp(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- isMouseDown = false;
- }
- }
- }
- }
当前文章:通过控件拖动窗体
网页URL:http://pwwzsj.com/article/gpehpe.html