c# – Windows窗体:图片框手动拖动操作图像解决方案?

嗨,我写了一个代码,将在winform中拖动图片框这是我的代码:

   private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
   {

       pictureBox1.Top=(56 * ((pictureBox1.Top + (e.Y - firsty)) / 56) + 3); //this for the correction of location of picture box
       isdragging[0] = false;

    }
     private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
   {
       isdragging[0] = true;
       firstx = e.X;
       firsty = e.Y;
       changeclickingstatus(pictureBox1);//this about my program
       for (sbyte i = 0; i < (sbyte)20; i++)//this about my program
       {
           clickindex[i] = (sbyte)1;//this about my program
       }
       clickindex[0] = (sbyte)0;//this about my program
       dragtop = (sbyte)(pictureBox1.Top/56);//this about my program
       }



        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
       {
       if (isdragging[0])
       {

           if (pictureBox1.Location.Y + (e.Y - firsty) < 0)
           {
               pictureBox1.Top = 0;//these are the limits of dragging

           }
           else if (pictureBox1.Location.Y + (e.Y - firsty) > 290)
           {
               pictureBox1.Top = 290;//these are the limits of dragging

           }
           else
           {



               pictureBox1.Top = pictureBox1.Top + (e.Y - firsty);

           }


           if (pictureBox1.Location.X + (e.X - firstx) < 6)
           {
               pictureBox1.Left = 6;//these are the limits of dragging
           }
           else if (pictureBox1.Location.X + (e.X - firstx) > 280)
           {
               pictureBox1.Left = 280;//these are the limits of dragging
           }
           else
           {
               pictureBox1.Left = pictureBox1.Left + (e.X - firstx);

           }

       }
   }

我有相同的图片框2代码,我的问题是:
当我将我的第一张图片拖到第二张图片时,它会越过它并且代码正常工作,但是当我将第二张图片框拖到第一张图片框时,第二张图片框会在第一张图片框下面!有这个属性吗?

解决方法:

您可以使用BringToFront()方法:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    pictureBox1.BringToFront();
    /**/
}

private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
    pictureBox2.BringToFront();
    /**/
}
上一篇:javascript – d3.js:使用带有强制布局的缩放时禁用拖动


下一篇:c# – ItemsControl中的WPF中的可拖动对象?