如何将pictureBox拖入另一个-c# [英] how can I drag pictureBox into another - c#

查看:87
本文介绍了如何将pictureBox拖入另一个-c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的想法是:



我有一个包含地图的大图片框..让我们称之为(MAP)它有5个国家



和5个小图片框每个都有特定的城市图片。



我想拖动每个城市的图片并将其放入地图中的正确国家位置..



如果用户将其拖动到地图中的正确位置..城市的图片将会移动到正确的篮子,否则,到不正确的篮子(我没有怎么做这部分所以我没有在代码中写任何关于它的东西)



我想我应该使用(x,y),但我不能这样做:(

请向我解释我该怎么做?



我正在努力的代码:

* pictureBox12是城市的图片之一*

* pictireBox3是地图*







 私人  void  pictureBox12_MouseDown( object  sender,MouseEventArgs e)
{
CurrentDrag = pictureBox12.AccessibleName;
PictureBox pictureBoxV =(PictureBox)sender;
DoDragDrop(pictureBoxV.Image,DragDropEffects.Copy);
}





  private   void  pictureBox12_MouseMove( object  sender,MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.DoDragDrop(random1.ToString() ,DragDropEffects.Copy);
}
}





  private   void  pictureBox3_DragEnter( object  sender,DragEventArgs e)
{

e.Effect = DragDropEffects.Move;
/ * if(e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
* /
}





  private   void  pictureBox3_DragDrop( object  sender,DragEventArgs e)
{

int x = .PointToClient( new Point(eX,eY))。X;

int y = .PointToClient(点(eX,eY))。Y;

if (x > = pictureBox12.Location.X&& ; x < = pictureBox12.Location.X + pictureBox12.Width&& y > = pictureBox12.Location.Y&& y < = pictureBox12.Location.Y + pictureBox12.Height)
{

string [] files =( string [])e.Data.GetData(DataFormats.FileDrop);

pictureBox1.Image = Image.FromFile(files [ 0 ]);

}

/ * string txt = e.Data。的GetData(DataFormats.Text)的ToString();
int index = pictureBox3.GetCharIndexFromPosition(pictureBox3.PointToClient(Cursor.Position));
pictureBox3.SelectionStart = index;
pictureBox3.SelectionLength = 0;
pictureBox3.SelectedText = txt; * /

}







  private   void  pictureBox3_DragOver( object  sender,DragEventArgs e)
{
/ * int index = pictureBox3.GetCharIndexFromPosition(pictureBox3.PointToClient(Cursor.Position));
pictureBox3.SelectionStart = index;
pictureBox3.SelectionLength = 0; * /

}

解决方案

你可能会也可能不会使用 PictureBox 。您可以使用此控件,但它与此问题几乎没有关系。图片框不是图片,而是一些(冗余)包装器控件。相反,您可以在位图上绘制位图:

https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx [ ^ ]。



只需要使用用于绘制的 System.Drawing.Graphics 的实例图片。这是你得到它的地方: https:/ /msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage%28v=vs.110%29.aspx [ ^ ]。



这就是在图像上绘制图像所需的全部内容,无论这是否是 PictureBox 的图像。



请查看我过去的答案:

在图片框中附加图片 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

如何从旧图纸中清除面板 [ ^ ]。



关于图形渲染的这些答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ]。



-SA

here's the idea:

I have a big picture box contains a map.. let's called it (MAP) and it has 5 country

and 5 small picture boxes each one has a specific city picture on it.

I wanna drag each one of the city's picture and drop it into the right country position in the map..

and if the user drag it to the correct place in the map.. the city's picture will move to the correct basket, otherwise, to the incorrect basket(I didn't how to do this part so I didn't write anything about it in the code)

I think I should use (x,y), but I couldn't do it :(
please explain to me how can I do it?

the code I'm struggle with:
* pictureBox12 is one of city's picture*
* pictireBox3 is the map*



private void pictureBox12_MouseDown(object sender, MouseEventArgs e)
      {
          CurrentDrag = pictureBox12.AccessibleName;
          PictureBox pictureBoxV = (PictureBox)sender;
          DoDragDrop(pictureBoxV.Image, DragDropEffects.Copy);
      }



private void pictureBox12_MouseMove(object sender, MouseEventArgs e)
      {
          if (e.Button == System.Windows.Forms.MouseButtons.Left)
          {
              pictureBox1.DoDragDrop(random1.ToString(), DragDropEffects.Copy);
          }
      }



private void pictureBox3_DragEnter(object sender, DragEventArgs e)
      {

          e.Effect = DragDropEffects.Move;
          /*if (e.Data.GetDataPresent(DataFormats.Text))
              e.Effect = DragDropEffects.All;
          else
              e.Effect = DragDropEffects.None;
      */}



private void pictureBox3_DragDrop(object sender, DragEventArgs e)
       {

           int x = this.PointToClient(new Point(e.X, e.Y)).X;

           int y = this.PointToClient(new Point(e.X, e.Y)).Y;

           if (x >= pictureBox12.Location.X && x <= pictureBox12.Location.X + pictureBox12.Width && y >= pictureBox12.Location.Y && y <= pictureBox12.Location.Y + pictureBox12.Height)
           {

               string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

               pictureBox1.Image = Image.FromFile(files[0]);

           }

          /* string txt = e.Data.GetData(DataFormats.Text).ToString();
           int index = pictureBox3.GetCharIndexFromPosition(pictureBox3.PointToClient(Cursor.Position));
           pictureBox3.SelectionStart = index;
           pictureBox3.SelectionLength = 0;
           pictureBox3.SelectedText = txt;*/
       }




private void pictureBox3_DragOver(object sender, DragEventArgs e)
      {
        /*  int index = pictureBox3.GetCharIndexFromPosition(pictureBox3.PointToClient(Cursor.Position));
          pictureBox3.SelectionStart = index;
          pictureBox3.SelectionLength = 0;*/
      }

解决方案

You may or may not to use PictureBox. You can use this control, but it has little to do with this problem. Picture box is not a picture, but some (redundant) wrapper control. Instead, you can draw bitmap on bitmap:
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].

Only you need to use the instance of System.Drawing.Graphics used to draw on an image. This is where you get it: https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage%28v=vs.110%29.aspx[^].

That's all you need to draw an image on image, no matter if this is an image of a PictureBox or not.

Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

On these answers on graphics rendering:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

—SA


这篇关于如何将pictureBox拖入另一个-c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆