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

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

s.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
   this.pictureBox[2].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
   this.pictureBox[2].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
   */
   
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(432, 348);
   this.Controls.Add(this.pictureBox[0]);
   this.Controls.Add(this.pictureBox[1]);
   this.Name = "Form1";
   this.Text = "Form1";   
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());   
  }  

                bool whetherSelected=false;
  Point p=new Point();

  #region 事件代码
  private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
                        whetherSelected=true;   
   p.X=Cursor.Position.X;
   p.Y=Cursor.Position.Y;    
  }

  private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   whetherSelected=false;
  }

  private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   if(whetherSelected==true)
   {    
    //加入一个函数,得到是哪个pictureBox
    int index=-1;
    for(int i=0;i<2;i++)
     if(this.pictureBox[i].Capture)
      index=i;
    this.pictureBox[index].Left=this.pictureBox[index].Left+(Cursor.Position.X-p.X);
    this.pictureBox[index].Top=this.pictureBox[index].Top+(Cursor.Position.Y-p.Y);
    
                this.pictureBox[1-index].SendToBack();

    p.X=Cursor.Position.X;
    p.Y=Cursor.Position.Y;  
   }
  }

  
  #endregion

  
 }
}

本文关键:Control Study -> 实现:用鼠标拖动图片
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top