您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
适用于无边框窗体,自定义标题栏
之前在网上找的,如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
- {
- /// <summary>
- /// <para>说明:窗体拖动类,通过这个类提供的方法实现窗体上任意控件可辅助拖动窗体</para>
- /// </summary>
- public class DragFormClass
- {
- private static bool isMouseDown = false;
- private static Point mouseOffset;
- private static Form _form;
- /// <summary>
- /// 在窗体上增加拖拽事件
- /// </summary>
- /// <param name="control">控件对象</param>
- 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);
- }
- /// <summary>
- /// 鼠标按下之时,保存鼠标相对于窗体的位置
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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;
- }
- }
- /// <summary>
- /// 移动鼠标的时候改变窗体位置
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 松开鼠标的时候,重设事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private static void control_MouseUp(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- isMouseDown = false;
- }
- }
- }
- }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。