日期:2014-05-18  浏览次数:20810 次

如何控制控件移动
如何控制button1在Panel中反复运动

------解决方案--------------------
C# code


 private void Form1_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(DoMoveButton));
            t.IsBackground = true;
            t.Start();
        }

        private void DoMoveButton()
        {
            int x = 0;
            MoveControl dele = SafeRemove;
            while (true)
            {
                Point buttonPosition = button1.Location;
                if (buttonPosition.X <= 0)
                {
                    x = panel1.Location.X + panel1.Width;
                }
                else
                {
                    x = buttonPosition.X - 2;
                }
                Point p = new Point(x, buttonPosition.Y);
                button1.Invoke(dele, p);
                Thread.Sleep(100);
            }
        }

        public delegate void MoveControl(Point p);

        private void SafeRemove(Point p)
        {
            button1.Location = p;
        }