好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

鼠标拖拽效果的实现

鼠标拖拽效果的实现

鼠标拖拽效果的实现

 

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

首先在Mouse_Down事件设置”拖动”标志,表明鼠标已经按下,将要移动;同时记录鼠标的起始位置 然后在Mouse_Move事件计算鼠标的新位置,将图标设置到鼠标的位置,重新绘图 最后在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

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于鼠标拖拽效果的实现的详细内容...

  阅读:45次