| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2620 人关注过本帖
标题:求label左右来回运动代码
只看楼主 加入收藏
a6681316
Rank: 2
等 级:论坛游民
帖 子:21
专家分:27
注 册:2010-12-17
结帖率:71.43%
收藏
 问题点数:0 回复次数:10 
求label左右来回运动代码
在线等待求解!呵呵
搜索更多相关主题的帖子: label 代码 运动 
2010-12-21 18:30
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
label左右来回运动
控件 timer label
算法 timer事件 label Y坐标不变 X坐标增加或减少

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-12-21 18:40
a6681316
Rank: 2
等 级:论坛游民
帖 子:21
专家分:27
注 册:2010-12-17
收藏
得分:0 
timer1下代码我label1.Left++; 标签一直往左!如何使它当到了软件边缘时候自动执行label1.Left--; ??求解
2010-12-21 18:46
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:0 
那你就判断一下label1.Left > form1.Right ? ...
2010-12-21 18:52
a6681316
Rank: 2
等 级:论坛游民
帖 子:21
专家分:27
注 册:2010-12-17
收藏
得分:0 
程序代码:
if ("label1.Left > form1.Right")
            {
                label5.Left++;
            }
            else
                label5.Left--;   
这样也不对啊!我晕!求助
2010-12-21 18:57
a6681316
Rank: 2
等 级:论坛游民
帖 子:21
专家分:27
注 册:2010-12-17
收藏
得分:0 
要转换!求代码
2010-12-21 18:57
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
请问楼主你自己有一点思路吗

本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2010-12-21 22:08
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:0 
我承认。我晚上闲了....
寂寞的夜晚。唉~~

写了一个类,自己用用哈。我没有用Timer,用Timer更容易。没事看看代码,对自己还是有帮助的~~
程序代码:
/*

 * 文件名: 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

    }
}

2010-12-21 22:40
cccool
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:268
专家分:555
注 册:2007-5-1
收藏
得分:0 
不得不承认!

[fly]让心情飞一会[/fly]
">Email to Me     
2010-12-21 22:44
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:0 
回复 8楼 zhoufeng1988
呵呵 研究研究zhoufeng1988兄的代码风格

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-12-22 09:18
快速回复:求label左右来回运动代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029387 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved