我准备将PictureBox中的图片拖放到Panel中,每拖放一下,就在Panel中增加一个新的图片,现在可以实现拖放操作,但Panel中只显示最后一个,其他的怎么能显示出来啊
如果能给放入(DragDrop)的每一张图片编上号,而在Panel的OnPaint事件中设定循环,就应该能显示出所有图片,但不知该采用什么技术,大家帮帮忙吧
用可变数组或索引器能实现么
谢谢,已经实现了,不过不是用你说的方法
private ArrayList pics = new ArrayList();
...
private void panel1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
Point pt = Point.Empty ;
pt.X = e.X;
pt.Y = e.Y;
pt = panel1.PointToClient(pt);
cmpImage = cmpPictureBox.Image;
cmpX = pt.X - cmpImage.Width/2;
cmpY = pt.Y - cmpImage.Height/2;
pics.Add(cmpImage);
pics.Add(cmpX);
pics.Add(cmpY);
panel1.Invalidate();
}
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
for(int i=0; i<pics.Count; i++)
{
cmpImage = (Image)pics[i];
i += 1;
cmpX = (int)pics[i];
i += 1;
cmpY = (int)pics[i];
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(cmpX, cmpY, cmpImage.Width, cmpImage.Height);
g.DrawImage(cmpImage,rect);
}
就是建立一个ArrayList,每三位存储一个图片的3个信息:图片、横坐标、纵坐标,鼠标放下的时候加入ArrayList,Panel刷新的时候读出来,这样图片就能全部显示了
但我想用另一种方法,就是把这3个信息存入数组 Object[3]中,再把这个数组加入ArrayList:
private Object[] Img;
...
Img = new Object[3];
...
Img[0] = cmpImage;
Img[1] = cmpX;
Img[2] = cmpY;
pics.Add(Img);
再读出来
for(int i=0; i< pics.Count; i++)
{
Img = (Object[])pics[i];
cmpImage = (Image)Img[0];
cmpX = (int)Img[1];
cmpY = (int)Img[2];
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(cmpX, cmpY, cmpImage.Width, cmpImage.Height);
g.DrawImage(cmpImage,rect);
}
但这样做又只显示最后一张图片,是什么原因呢?