在form里定义两个控件,如picturebox1和picturebox2
我要让它们同时运动,每个有每个的运动方式(如picturebox1:x=sin t,y=t;picturebox2:x=t,y=480-t)
并用button1来控制picturebox1的停止运动,用button2来控制picturebox2的停止运动
请问大佬们该如何实现

解决方案 »

  1.   

    首先这个控件不支持动态图像,除非你自己绘制坐标系和函数图像,重写它的onpaint方法或是用w p f
      

  2.   

    不是,这里的x、y为left与top
      

  3.   

    像flash一样动
      

  4.   

    在线程里面加控制标记,button控制标记来暂停或什么的,轨迹自行设置,这里随意设的using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    namespace Demo_1
    {
        public partial class Form1 : Form
        {
            public class PanelEx : Panel
            {
                public PanelEx()
                {
                    SetStyle(
                        ControlStyles.AllPaintingInWmPaint |
                        ControlStyles.OptimizedDoubleBuffer |
                        ControlStyles.ResizeRedraw |
                        ControlStyles.SupportsTransparentBackColor |
                        ControlStyles.UserPaint,
                        true
                        );
                    UpdateStyles();
                }
            }
     
            public abstract class DraggableObject
            {
                public abstract Rectangle Region { get; set; }
                public abstract Bitmap Setimage { get; set; }
                public abstract void OnPaint(PaintEventArgs e);
            }
     
            public class Draggable : DraggableObject
            {
                private Rectangle m_Region;
                private Bitmap m_SetImage;
     
                public Draggable(string id, Rectangle regin, Bitmap image)
                {
                    m_Region = regin;
                    m_SetImage = image;
                }
                public override Rectangle Region { get => m_Region; set => m_Region = value; }
                public override Bitmap Setimage { get => m_SetImage; set => m_SetImage = value; }
                public override void OnPaint(PaintEventArgs e)
                {
                    e.Graphics.DrawImage(m_SetImage, m_Region);
                }
            }
            public List<DraggableObject> DesignObjects = new List<DraggableObject>();
     
            public PanelEx panelContent;
     
            public Form1()
            {
                InitializeComponent();
     
                panelContent = new PanelEx()
                {
                    Dock = DockStyle.Fill,
                    BackgroundImageLayout = ImageLayout.Stretch,
                    BackgroundImage = Image.FromFile("back.jpg")
                };
                panelContent.Paint += PanelContent_Paint;
                this.Controls.Add(panelContent);
     
                string[] files = new string[] { "01.png", "02.png", "03.png" };
                for (int i = 0; i < 3; i++)
                {
                    Bitmap bmp = Image.FromFile(files[i]) as Bitmap;
                    Draggable draggableBlock = new Draggable(DateTime.Now.ToString(), new Rectangle(0, 0, bmp.Width, bmp.Height), bmp);
                    DesignObjects.Add(draggableBlock);
                }
                Task.Run(async () =>
                {
                    int set_x = 0;
                    while (true)
                    {
                        DesignObjects[0].Region = new Rectangle(set_x, 100, DesignObjects[0].Region.Width, DesignObjects[0].Region.Height);
                        panelContent.Invalidate();
                        await Task.Delay(5);
                        if (set_x < this.Width) set_x += 1;
                        else set_x = 0;
                    }
                });
                Task.Run(async () =>
                {
                    int set_y = 0;
                    while (true)
                    {
                        DesignObjects[1].Region = new Rectangle(100, set_y, DesignObjects[1].Region.Width, DesignObjects[1].Region.Height);
                        panelContent.Invalidate();
                        await Task.Delay(5);
                        if (set_y < this.Height) set_y += 1;
                        else set_y = 0;
                    }
                });
                Task.Run(async () =>
                {
                    int set_x = 0, set_y = 0;
                    while (true)
                    {
                        DesignObjects[2].Region = new Rectangle(set_x, set_y, DesignObjects[2].Region.Width, DesignObjects[2].Region.Height);
                        panelContent.Invalidate();
                        await Task.Delay(5);
                        if ((set_y + 1 + DesignObjects[2].Region.Height) < this.Height)
                        {
                            set_x += 1;
                            set_y += 1;
                        }
                        else
                        {
                            set_x = 0;
                            set_y = 0;
                        }
                    }
                });
            }
     
            private void PanelContent_Paint(object sender, PaintEventArgs e)
            {
                foreach (DraggableObject item in DesignObjects)
                {
                    item.OnPaint(e);
                }
            }
     
        }
    }
      

  5.   

    自己绘制坐标系和函数图像,重写它的onpaint方法或是用w p f
      

  6.   

    添加一个timer控件,在函数体里对button控件的.Location属性(x,y)同时增减x,y的值,x,y到最大或最小时改变一下,反向操作(最好创建两个全局int变量来控制增减多少,反向时乘以-1方向就可以改变方向),创建两个全局bool变量IsMove1和IsMove2在改变button控件时用来判断是否增减值;同时如果是true就把变量变false,反之则异然。