zoukankan      html  css  js  c++  java
  • 移动鼠标时按钮跟着移动

    class MoveAccessPoint
        {
            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;
    
            #region 私有方法
            /// <summary>
            /// 在鼠标左键按下的状态记录鼠标当前的位置,以及被移动组件的当前位置
            /// </summary>
            private void DeviceMouseDown(object sender, MouseEventArgs e)
            {
                Button currentButton = (Button)sender;
                if (Containe == null)
                {
                    return;
                }
                if (e.Button == MouseButtons.Left)
                {
    
                    IsMoving = true;
                    pCtrlLastCoordinate.X = currentButton.Left;
                    pCtrlLastCoordinate.Y = currentButton.Top;
                    pCursorLastCoordinate.X = Cursor.Position.X;
                    pCursorLastCoordinate.Y = Cursor.Position.Y;
                     
                }
            }
            private void DeviceMouseMove(object sender, MouseEventArgs e)
            {
                Button currentButton = (Button)sender;
                if (Containe == null)
                {
                    return;
                }
    
                if (e.Button == MouseButtons.Left)
                {
                    if (this.IsMoving)
                    {
    
    
                        Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);
                        //Point pCursor = Containe.PointToClient(Cursor.Position);
    
                        pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X;
                        pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y;
    
    
    
                        currentButton.Left = pCtrlLastCoordinate.X + pCursorOffset.X;
                        currentButton.Top = pCtrlLastCoordinate.Y + pCursorOffset.Y;
    
    
                        //修正参数
    
                        if (currentButton.Left < 0)
                            currentButton.Left = 0;
                        if (currentButton.Top < 0)
                            currentButton.Top = 0;
                        if (currentButton.Left + currentButton.Width > this.Containe.HorizontalScroll.Maximum)
                            currentButton.Left = this.Containe.HorizontalScroll.Maximum - currentButton.Width;
                        if (currentButton.Top + currentButton.Height > this.Containe.VerticalScroll.Maximum)
                            currentButton.Top = this.Containe.VerticalScroll.Maximum - currentButton.Height;
                        
                    }
                }
            }
    
            private void DeviceMouseUp(object sender, MouseEventArgs e)
            {
                Button currentButton = (Button)sender;
                if (Containe == null)
                {
                    return;
                }
                if (this.IsMoving)
                {
                    pCursorOffset.X = 0;
                    pCursorOffset.Y = 0;
                    IsMoving = false;
                }
            }
            #endregion
            public void EditMode(Control c, ScrollableControl parentContain)
            {
                ctrl = c;
                this.Containe = parentContain;
    
                ctrl.MouseDown += new MouseEventHandler(DeviceMouseDown);
                ctrl.MouseMove += new MouseEventHandler(DeviceMouseMove);
                ctrl.MouseUp += new MouseEventHandler(DeviceMouseUp);
            }
            public void SaveMode(Control c, ScrollableControl parentContain)
            {
                ctrl = c;
                this.Containe = parentContain;
                ctrl.MouseDown -= new MouseEventHandler(DeviceMouseDown);
                ctrl.MouseMove -= new MouseEventHandler(DeviceMouseMove);
                ctrl.MouseUp -= new MouseEventHandler(DeviceMouseUp);
            }
            
            public void simulateDeviceMouseDown(Point lpPoint)
            {
                Win32.simulateDeviceMouseDown( lpPoint);
            }
        }
  • 相关阅读:
    替换html里面的 及解决记事本中的每个段落只有一行的情形
    关于 'list' object has no attribute 'select'
    python写爬虫的弯路
    将博客搬至CSDN
    转载:如何让spring mvc web应用启动时就执行
    java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)
    解决svn问题:Wrong committed revision number: -1
    生产环境中,数据库升级维护的最佳解决方案flyway
    在JaveWeb项目中配置Spring 匿名访问时,匹配规则的变相实现/*
    nexus的使用
  • 原文地址:https://www.cnblogs.com/zhumeng1582/p/3425815.html
Copyright © 2011-2022 走看看