zoukankan      html  css  js  c++  java
  • C# 令某个窗体可跟着鼠标移动

    /// <summary>
        /// 使窗口的中的指定控件支持运行时移动
        /// TODO:运行时缩放
        /// </summary>
        public class ControlMoveResize
        {
            #region 私有成员
            bool IsMoving = false;
            Point pCtrlLastCoordinate = new Point(0, 0);
            Point pCursorOffset = new Point(0, 0);
            Point pCursorLastCoordinate = new Point(0, 0);
            private Control ctrl = null;
            private ScrollableControl Containe = null;
            #endregion
            #region 私有方法
            /// <summary>
            /// 在鼠标左键按下的状态记录鼠标当前的位置,以及被移动组件的当前位置
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void MouseDown(object sender, MouseEventArgs e)
            {
                if (Containe == null)
                {
                    return;
                }
                if (e.Button == MouseButtons.Left)
                {
                    IsMoving = true;
                    pCtrlLastCoordinate.X = ctrl.Left;
                    pCtrlLastCoordinate.Y = ctrl.Top;
                    pCursorLastCoordinate.X = Cursor.Position.X;
                    pCursorLastCoordinate.Y = Cursor.Position.Y;
                }
            }
            private void MouseMove(object sender, MouseEventArgs e)
            {
                if (Containe == null)
                {
                    return;
                }
    
                if (e.Button == MouseButtons.Left)
                {
                    if (this.IsMoving)
                    {
                        Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);
    
                        pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X;
    
                        pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y;
                        ctrl.Left = pCtrlLastCoordinate.X + pCursorOffset.X;
                        ctrl.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y;
                    }
    
                }
            }
    
            private void MouseUp(object sender, MouseEventArgs e)
            {
                if (Containe == null)
                {
                    return;
                }
                if (this.IsMoving)
                {
                    if (pCursorOffset.X == 0 && pCursorOffset.Y == 0)
                    {
                        return;
                    }
                    if ((pCtrlLastCoordinate.X + pCursorOffset.X + ctrl.Width) > 0)
                    {
                        ctrl.Left = pCtrlLastCoordinate.X + pCursorOffset.X;
                    }
                    else
                    {
                        ctrl.Left = 0;
                    }
                    if ((pCtrlLastCoordinate.Y + pCursorOffset.Y + ctrl.Height) > 0)
                    {
                        ctrl.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y;
                    }
                    else
                    {
                        ctrl.Top = 0;
                    }
                    pCursorOffset.X = 0;
                    pCursorOffset.Y = 0;
                }
            }
            #endregion
            #region 构造函数
            /// <summary>
            /// 获取被移动控件对象和容器对象
            /// </summary>
            /// <param name="c">被设置为可运行时移动的控件</param>
            /// <param name="parentContain">可移动控件的容器</param>
            public ControlMoveResize(Control c, ScrollableControl parentContain)
            {
                ctrl = c;
                this.Containe = parentContain;
                ctrl.MouseDown += new MouseEventHandler(MouseDown);
                ctrl.MouseMove += new MouseEventHandler(MouseMove);
                ctrl.MouseUp += new MouseEventHandler(MouseUp);
            }
            #endregion
        }

    上面是类,以下是调用 的方法

    传入的参数为:控件,控件容器(即窗体)

     ControlMoveResize control = new ControlMoveResize(this.button1,this);
  • 相关阅读:
    ASP.NET Web API 2.0 统一响应格式
    [翻译]ASP.NET Web API 2 中的全局错误处理
    【WPF】UserControl 的 Load事件
    解决MS SQL Server 使用HashBytes函数乱码问题
    实例化MD5CryptoServiceProvider报错:此实现不是 Windows 平台 FIPS 验证的加密算法的一部分
    添加扩展方法,提示编译错误 “缺少编译器要求的成员”
    [调试]VS2013调试时提示“运行时当前拒绝计算表达式的值”
    jquery chosen 插件多选初始化
    Asp.net WebForm 中无法引用App_Code文件夹下的类
    文本非法字符过滤 简体转换繁体
  • 原文地址:https://www.cnblogs.com/chcong/p/4310624.html
Copyright © 2011-2022 走看看