求label左右来回运动代码
在线等待求解!呵呵
label左右来回运动控件 timer label
if ("label1.Left > form1.Right") { label5.Left++; } else label5.Left--;这样也不对啊!我晕!求助
/* * 文件名: ControlMove.cs * 编写:ZhouFeng * 日期:2010/12/21 * * 编程中国(bbs.bccn.net) */ using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Threading; using System.Drawing; namespace LabelMove { /*控件移动实体*/ public class ControlMove { /// <summary> /// 移动方向 /// </summary> public enum MoveDirection { Left, // 向左移动 Right, // 向右移动 Up, // 向上移动 Down // 向下移动 } /// <summary> /// 移动操作委托 /// </summary> /// <param name="offset">偏移像素</param> /// <param name="direction">方向</param> internal delegate void ControlMoveDlgt(int offset, MoveDirection direction); /// <summary> /// 获取控件位置操作委托 /// </summary> /// <returns>控件委托</returns> internal delegate Point GetControlLocationDlgt(); #region 成员变量 private Control cntrl; // 要移动的控件 private Thread moveThread; // 移动控件的线程 private static bool hasCreateInstance; // 是否具有该对象实例 private const string ThreadName = "ThreadMoveControl"; // 线程名称 private Point startLocation; // 开始移动位置 private Point endLocation; // 结束移动位置 private int offset; // 移动像素 private MoveDirection direction; // 初始移动方向 #endregion #region 属性 public Point StartLocation { get { return this.startLocation; } set { this.startLocation = value; } } public Point EndLocation { get { return this.endLocation; } set { this.endLocation = value; } } public int Offset { get { return offset; } set { this.offset = value; } } public MoveDirection ControlMoveDirection { get { return direction; } set { direction = value; } } #endregion #region 构造函数相关 static ControlMove() { hasCreateInstance = false; } private ControlMove(Control pCntrl) : this( pCntrl, MoveDirection.Right, Point.Empty, Point.Empty, 1) { } private ControlMove(Control pCntrl, MoveDirection direction, Point pStartPoint, Point pEndPoint, int pOffset) { Init(); this.cntrl = pCntrl; startLocation = pStartPoint; endLocation = pEndPoint; this.direction = direction; this.offset = pOffset; } ~ControlMove() { try { if (moveThread.ThreadState == ThreadState.Running) { moveThread.Abort(); } } catch (ThreadAbortException ex) { // Eat this exception } } #endregion #region 封装方法 /// <summary> /// 对象初始化 /// </summary> private void Init() { /* * 线程实例初始化 */ moveThread = new Thread( new ThreadStart( ThreadEntry)); moveThread.IsBackground = true; } /// <summary> /// 线程入口 /// </summary> private void ThreadEntry() { /// 最大移动像素 int maxMovePixel = this.cntrl.Parent.Right - this.cntrl.Right; /// 移动委托实例 ControlMoveDlgt moveDlgt = new ControlMoveDlgt( ChangeControlLocation); /// 获取控件位置委托实例 GetControlLocationDlgt getLocationDlgt = new GetControlLocationDlgt( GetControlLocation); /// 临时变量 Point tempPoint = Point.Empty; lock (this) { while (true) { tempPoint = (Point)this.cntrl.Invoke(getLocationDlgt); /// 设置当前移动方向 SetCurrentMoveDirection( tempPoint); this.cntrl.Invoke(moveDlgt, offset, this.direction); } } } /// <summary> /// 获取控件位置 /// </summary> /// <returns>位置</returns> private Point GetControlLocation() { if (this.cntrl != null) { return this.cntrl.Location; } else { return Point.Empty; } } /// <summary> /// 移动控件 /// </summary> /// <param name="offset">移动大小</param> /// <param name="direction">移动方向</param> private void ChangeControlLocation( int offset, MoveDirection direction) { if (this.cntrl != null) { switch (direction) { case MoveDirection.Left: /*向左移动*/ this.cntrl.Location = new Point(this.cntrl.Left - offset, this.cntrl.Top); break; case MoveDirection.Right: /*向右移动*/ this.cntrl.Location = new Point(this.cntrl.Left + offset, this.cntrl.Top); break; case MoveDirection.Up: /*向上移动*/ this.cntrl.Location = new Point(this.cntrl.Left, this.cntrl.Top - offset); break; case MoveDirection.Down: /*向下移动*/ this.cntrl.Location = new Point(this.cntrl.Left, this.cntrl.Top + offset); break; } } // Prompt error. } /// <summary> /// 设置当前移动方向 /// </summary> private void SetCurrentMoveDirection( Point currentPoint) { switch (direction) { case MoveDirection.Right: if (currentPoint.X >= this.endLocation.X) { this.direction = MoveDirection.Left; Thread.Sleep(100); } break; case MoveDirection.Left: if (currentPoint.X <= this.startLocation.X) { this.direction = MoveDirection.Right; Thread.Sleep(100); } break; case MoveDirection.Up: if (currentPoint.Y <= this.startLocation.Y) { this.direction = MoveDirection.Down; Thread.Sleep(100); } break; case MoveDirection.Down: if (currentPoint.Y >= this.endLocation.Y) { this.direction = MoveDirection.Up; Thread.Sleep(100); } break; } } #endregion #region 用户操作接口 /// <summary> /// 创建ControlMove实例 /// </summary> /// <param name="pCntrl">从System.Windows.Forms.Control继承的类</param> /// <returns>ControlMove对象实例</returns> public static ControlMove CreateControlMove( Control pCntrl) { if (hasCreateInstance) { throw new Exception("该对象实例已经被创建!"); } else { hasCreateInstance = true; return new ControlMove( pCntrl); } } /// <summary> /// 设置移动的控件 /// </summary> /// <param name="pCntrl">从System.Windows.Forms.Control继承的类</param> public void SetControl(Control pCntrl) { this.cntrl = pCntrl; } /// <summary> /// 开始移动 /// </summary> public void StartMove() { if (this.moveThread != null) { this.moveThread.Start(); } } #endregion } }