zoukankan      html  css  js  c++  java
  • 鼠标拖拽效果的实现

    鼠标拖拽效果的实现

     

    我所说的“鼠标拖拽效果”是指,鼠标在某个图标上按下,然后拖动,图标随着鼠标移动;这个效果的实现依赖控件的三个事件:Mouse_Down,Mouse_Move,Mouse_Up

    1. 首先在Mouse_Down事件设置”拖动”标志,表明鼠标已经按下,将要移动;同时记录鼠标的起始位置
    2. 然后在Mouse_Move事件计算鼠标的新位置,将图标设置到鼠标的位置,重新绘图
    3. 最后在Mouse_Up事件关闭”拖动”标志,标志鼠标已经弹起

    实现如下:

    首先,在Visual Studio 2010创建一个“Windows窗体应用程序”项目,在Form上任意拖入一个控件(比如Button,Label等,为了看得明白,我这里拖入的是PictureBox控件),为PictureBox的Image属性设置图片,效果如下:

     

     

    然后修改Form对应的代码:

    首先为Form增加两个实例变量,分别为拖动标识和PictureBox控件的位置

    View Code
    #region Fields
    
    private Boolean m_bDragFlag = false;
    private Point m_oStartLocation;
    
    #endregion
    复制代码
    #region Fields
    
    private Boolean m_bDragFlag = false;
    private Point m_oStartLocation;
    
    #endregion
    复制代码

    然后为PictureBox控件实现Mouse_Down,Mouse_Move,Mouse_Up事件

    View Code
    #region Events
    
            //PictureBox.Click事件
            private void pbxDrag_Click(object sender, EventArgs e)
            {
                //0.取控件实例
                var pbxDrag = sender as PictureBox;
    
                //1.用边框模拟选中状态
                pbxDrag.BorderStyle = BorderStyle.FixedSingle;
            }
    
            //PictrueBox.MouseDown事件(鼠标在控件上按下左键触发)
            private void pbxDrag_MouseDown(object sender, MouseEventArgs e)
            {
                //0.取控件实例
                var pbxDrag = sender as PictureBox;
    
                //1.设置"拖动"标识
                this.m_bDragFlag = true;
    
                //2.设置鼠标形状
                this.Cursor = Cursors.SizeAll;
    
                //3.设置初始位置
                this.m_oStartLocation = pbxDrag.Location;
            }
            //PictureBox.MouseMove事件(鼠标在控件上移动后触发)
            private void pbxDrag_MouseMove(object sender, MouseEventArgs e)
            {
                //0.取控件实例
                var pbxDrag = sender as PictureBox;
    
                if (this.m_bDragFlag)
                { 
                    //1.设置鼠标形状
                    this.Cursor = Cursors.Default;
    
                    //2.设置控件新位置
                    pbxDrag.Location = new Point(this.m_oStartLocation.X + e.X, this.m_oStartLocation.Y + e.Y);
    
                    //3.重新绘制图形
                    this.Invalidate();
                    this.Update();
    
                    //4.修正位置
                    this.m_oStartLocation = pbxDrag.Location;
                }
            }
            //PictureBox.MouseUp事件(鼠标在控件上弹起时触发)
            private void pbxDrag_MouseUp(object sender, MouseEventArgs e)
            {
                //0.设置鼠标形状
                this.Cursor = Cursors.Default;
    
                //1.设置"拖动"标识
                this.m_bDragFlag = false;
            }
    
            #endregion
    复制代码
    #region Events
    
            //PictureBox.Click事件
            private void pbxDrag_Click(object sender, EventArgs e)
            {
                //0.取控件实例
                var pbxDrag = sender as PictureBox;
    
                //1.用边框模拟选中状态
                pbxDrag.BorderStyle = BorderStyle.FixedSingle;
            }
    
            //PictrueBox.MouseDown事件(鼠标在控件上按下左键触发)
            private void pbxDrag_MouseDown(object sender, MouseEventArgs e)
            {
                //0.取控件实例
                var pbxDrag = sender as PictureBox;
    
                //1.设置"拖动"标识
                this.m_bDragFlag = true;
    
                //2.设置鼠标形状
                this.Cursor = Cursors.SizeAll;
    
                //3.设置初始位置
                this.m_oStartLocation = pbxDrag.Location;
            }
            //PictureBox.MouseMove事件(鼠标在控件上移动后触发)
            private void pbxDrag_MouseMove(object sender, MouseEventArgs e)
            {
                //0.取控件实例
                var pbxDrag = sender as PictureBox;
    
                if (this.m_bDragFlag)
                { 
                    //1.设置鼠标形状
                    this.Cursor = Cursors.Default;
    
                    //2.设置控件新位置
                    pbxDrag.Location = new Point(this.m_oStartLocation.X + e.X, this.m_oStartLocation.Y + e.Y);
    
                    //3.重新绘制图形
                    this.Invalidate();
                    this.Update();
    
                    //4.修正位置
                    this.m_oStartLocation = pbxDrag.Location;
                }
            }
            //PictureBox.MouseUp事件(鼠标在控件上弹起时触发)
            private void pbxDrag_MouseUp(object sender, MouseEventArgs e)
            {
                //0.设置鼠标形状
                this.Cursor = Cursors.Default;
    
                //1.设置"拖动"标识
                this.m_bDragFlag = false;
            }
    
            #endregion
    复制代码

     

     
     
    分类: WinForm
  • 相关阅读:
    面向中后台复杂场景的低代码实践思路
    树莓派使用raspivid实时预览视频
    grep rn无法匹配文件中的字符串
    C++有关std::sort和std::bind那些事
    C++有关unordered_map::erase的奇怪bug
    ssh与tar的奇妙组合
    git指定仓库使用特定用户名提交
    在win7下搭建php+apache+mysql环境
    神乎其神的技艺
    好书推荐——《启动大脑》
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2979333.html
Copyright © 2011-2022 走看看