Control Study -> 实现:用鼠标拖动图片

本文介绍如何在ASP.NET WebForm中实现鼠标拖动图片的功能,通过按住鼠标左键拖动,图片会跟随鼠标移动。示例代码可供下载,并提供相关文章链接以供进一步学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一).说明

    功能:用鼠标实现拖动图片.
    用法:将鼠标指向一图片,按住鼠标左键拖动. 图片会随鼠标一起移动.

(二).图片显示效果

(三).代码

      说明:您可以将此功能用作其它用途,也可以用作设计其它的游戏.

  1  using  System;
  2  using  System.Drawing;
  3  using  System.Collections;
  4  using  System.ComponentModel;
  5  using  System.Windows.Forms;
  6  using  System.Data;
  7  using  System.Threading;
  8 
  9  namespace  拖动图片
 10  {
 11    ///   <summary>
 12    ///  Form1 的摘要说明。
 13    ///   </summary>
 14    public   class  Form1 : System.Windows.Forms.Form
 15   {
 16     public  System.Windows.Forms.PictureBox[] pictureBox = new  PictureBox[ 2 ];
 17     // private System.Windows.Forms.PictureBox pictureBox2;
 18     ///   <summary>
 19     ///  必需的设计器变量。
 20     ///   </summary>
 21     private  System.ComponentModel.Container components  =   null ;
 22 
 23     public  Form1()
 24    {
 25      //
 26      //  Windows 窗体设计器支持所必需的
 27      //
 28     InitializeComponent();
 29      for ( int  i = 0 ;i < 2 ;i ++ )
 30     {
 31       this .pictureBox[i].MouseUp  +=   new  System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseUp);
 32       this .pictureBox[i].MouseMove  +=   new  System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseMove);
 33       this .pictureBox[i].MouseDown  +=   new  System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseDown);
 34     }   
 35              
 36      //
 37      //  TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 38      //
 39    }
 40 
 41     ///   <summary>
 42     ///  清理所有正在使用的资源。
 43     ///   </summary>
 44     protected   override   void  Dispose(  bool  disposing )
 45    {
 46      if ( disposing )
 47     {
 48       if  (components  !=   null
 49      {
 50       components.Dispose();
 51      }
 52     }
 53      base .Dispose( disposing );
 54    }
 55 
 56     #region  Windows Form Designer generated code
 57     ///   <summary>
 58     ///  设计器支持所需的方法 - 不要使用代码编辑器修改
 59     ///  此方法的内容。
 60     ///   </summary>
 61     private   void  InitializeComponent()
 62    {
 63     System.Resources.ResourceManager resources  =   new  System.Resources.ResourceManager( typeof (Form1));
 64      this .SuspendLayout();
 65      this .pictureBox[ 0 =   new  System.Windows.Forms.PictureBox();
 66      this .pictureBox[ 1 =   new  System.Windows.Forms.PictureBox();
 67     
 68      //  
 69      //  pictureBox1
 70      //  
 71      this .pictureBox[ 0 ].BackColor  =  System.Drawing.Color.Transparent;
 72      this .pictureBox[ 0 ].Image  =  ((System.Drawing.Image)(resources.GetObject( " pictureBox1.Image " )));
 73      this .pictureBox[ 0 ].Location  =   new  System.Drawing.Point( 200 192 );
 74      this .pictureBox[ 0 ].Name  =   " pictureBox1 " ;
 75      this .pictureBox[ 0 ].Size  =   new  System.Drawing.Size( 88 72 );
 76      this .pictureBox[ 0 ].SizeMode  =  System.Windows.Forms.PictureBoxSizeMode.StretchImage;
 77      this .pictureBox[ 0 ].TabIndex  =   0 ;
 78      this .pictureBox[ 0 ].TabStop  =   false ;
 79      /* this.pictureBox[1].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
 80     this.pictureBox[1].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
 81     this.pictureBox[1].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown); */
 82      //  
 83      //  pictureBox2
 84      //  
 85      // this.Opacity=0.8;
 86      this .pictureBox[ 1 ].BackColor  =  System.Drawing.Color.DodgerBlue;
 87      this .pictureBox[ 1 ].Image  =  ((System.Drawing.Image)(resources.GetObject( " pictureBox2.Image " )));
 88      this .pictureBox[ 1 ].Location  =   new  System.Drawing.Point( 200 72 );
 89      this .pictureBox[ 1 ].Name  =   " pictureBox2 " ;
 90      this .pictureBox[ 1 ].Size  =   new  System.Drawing.Size( 88 64 );
 91      this .pictureBox[ 1 ].SizeMode  =  System.Windows.Forms.PictureBoxSizeMode.StretchImage;
 92      this .pictureBox[ 1 ].TabIndex  =   1 ;
 93      this .pictureBox[ 1 ].TabStop  =   false ;
 94      /* this.pictureBox[2].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
 95     this.pictureBox[2].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
 96     this.pictureBox[2].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
 97      */
 98     
 99      //  
100      //  Form1
101      //  
102      this .AutoScaleBaseSize  =   new  System.Drawing.Size( 6 14 );
103      this .ClientSize  =   new  System.Drawing.Size( 432 348 );
104      this .Controls.Add( this .pictureBox[ 0 ]);
105      this .Controls.Add( this .pictureBox[ 1 ]);
106      this .Name  =   " Form1 " ;
107      this .Text  =   " Form1 " ;   
108      this .ResumeLayout( false );
109 
110    }
111     #endregion
112 
113     ///   <summary>
114     ///  应用程序的主入口点。
115     ///   </summary>
116    [STAThread]
117     static   void  Main() 
118    {
119     Application.Run( new  Form1());   
120    }  
121 
122                   bool  whetherSelected = false ;
123    Point p = new  Point();
124 
125     #region  事件代码
126     private   void  pictureBox1_MouseDown( object  sender, System.Windows.Forms.MouseEventArgs e)
127    {
128                          whetherSelected = true ;   
129     p.X = Cursor.Position.X;
130     p.Y = Cursor.Position.Y;    
131    }
132 
133     private   void  pictureBox1_MouseUp( object  sender, System.Windows.Forms.MouseEventArgs e)
134    {
135     whetherSelected = false ;
136    }
137 
138     private   void  pictureBox1_MouseMove( object  sender, System.Windows.Forms.MouseEventArgs e)
139    {
140      if (whetherSelected == true
141     {    
142       // 加入一个函数,得到是哪个pictureBox
143       int  index =- 1 ;
144       for ( int  i = 0 ;i < 2 ;i ++ )
145        if ( this .pictureBox[i].Capture)
146        index = i;
147       this .pictureBox[index].Left = this .pictureBox[index].Left + (Cursor.Position.X - p.X);
148       this .pictureBox[index].Top = this .pictureBox[index].Top + (Cursor.Position.Y - p.Y);
149      
150                   this .pictureBox[ 1 - index].SendToBack();
151 
152      p.X = Cursor.Position.X;
153      p.Y = Cursor.Position.Y;  
154     }
155    }
156 
157    
158     #endregion
159 
160    
161   }
162  }
163 
164 

(四).示例代码下载

     http://www.cnblogs.com/Files/ChengKing/拖动图片.rar

 

 

(五). 相关文章

          1. 在WebForm(Asp.net页面)中实现拖动效果, 请看:

               http://blog.csdn.net/ChengKing/archive/2006/08/30/1142605.aspx

          2. 使用键盘模拟鼠标, 请看:

               http://blog.csdn.net/chengking/archive/2005/10/07/496715.aspx


评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值