自写控件:滑动呈现控件(实现了两个以上控件间的切换)我写的


namespace SyncCenter.My
{
    public class SlidPanel : System.Windows.Forms.UserControl
    {
        private Dictionary<string, Control> mDic;
        private string mWouldControlName;
        private string mCurrControlName;
        private string mOldControlName;

        private Timer mTimer;
        private DirectionType mDirection;

        public SlidPanel()
        {

        }

        public void Init(string defaultControlName, Dictionary<string, Control> dic)
        {
            this.Controls.Clear();

            this.mCurrControlName = defaultControlName;
            this.mDic = dic;
            this.mTimer = new Timer();
            this.mTimer.Enabled = false;
            this.mTimer.Interval = 10;
            this.mTimer.Tick += mTimer_Tick;

            (this.mDic[this.mCurrControlName] as Control).Location = new System.Drawing.Point(0, 0);
            (this.mDic[this.mCurrControlName] as Control).Size = new System.Drawing.Size(this.Width, this.Height);

            this.Controls.Add((this.mDic[this.mCurrControlName] as Control));
        }

        public void Navigate(string controlName, DirectionType direction)
        {
            if (string.IsNullOrEmpty(controlName))
                return;
            if (this.mWouldControlName != null && this.mWouldControlName.Equals(controlName))
            {
                MessageBox.Show("出错了");
                return;
            }

            this.mDirection = direction;
            if (this.Controls.Count == 2)
            {
                this.mOldControlName = this.mCurrControlName;
                this.mCurrControlName = this.mWouldControlName;
                this.mWouldControlName = controlName;
                this.Controls.Remove(this.mDic[this.mOldControlName] as Control);//移除旧的
            }
            else if (this.Controls.Count == 1)
            {
                if (this.mCurrControlName.Equals(controlName))
                {
                    MessageBox.Show("出错了");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请先初始化");
                return;
            }

            this.mWouldControlName = controlName;


            (this.mDic[this.mWouldControlName] as Control).Size = new System.Drawing.Size(this.Width, this.Height);
            this.Controls.Add((this.mDic[this.mWouldControlName] as Control));

            switch (this.mDirection)
            {
                case DirectionType.Left:
                    (this.mDic[this.mWouldControlName] as Control).Location = new System.Drawing.Point(this.Width, 0);
                    break;
                case DirectionType.Right:
                    (this.mDic[this.mWouldControlName] as Control).Location = new System.Drawing.Point(-this.Width, 0);
                    break;
                default:
                    break;
            }
            this.mTimer.Enabled = true;
        }

        void mTimer_Tick(object sender, EventArgs e)
        {
            switch (this.mDirection)
            {
                case DirectionType.Left:
                    MoveLeft();
                    break;
                case DirectionType.Right:
                    MoveRight();
                    break;
                default:
                    break;
            }
        }

        private void MoveLeft()
        {
            if ((this.mDic[this.mWouldControlName] as Control).Left <= 8)
            {
                this.mTimer.Enabled = false;
                return;
            }
            else if ((this.mDic[this.mWouldControlName] as Control).Left <= 200)
            {
                this.mTimer.Interval += 1;
            }
            else
            {
                if (this.mTimer.Interval > 1)
                {
                    this.mTimer.Interval -= 1;
                }
            }
            (this.mDic[this.mCurrControlName] as Control).Left -= 8;
            (this.mDic[this.mWouldControlName] as Control).Left -= 8;
        }

        private void MoveRight()
        {
            if ((this.mDic[this.mWouldControlName] as Control).Left >= -8)
            {
                this.mTimer.Enabled = false;
                return;
            }
            else if ((this.mDic[this.mWouldControlName] as Control).Left >= -200)
            {
                this.mTimer.Interval += 1;
            }
            else
            {
                if (this.mTimer.Interval > 1)
                {
                    this.mTimer.Interval -= 1;
                }
                this.mTimer.Interval = 8;
            }
            (this.mDic[this.mCurrControlName] as Control).Left += 8;
            (this.mDic[this.mWouldControlName] as Control).Left += 8;
        }
    }
    /// <summary>
    /// 滑动方向
    /// </summary>
    public enum DirectionType
    {
        /// <summary>
        /// 向左
        /// </summary>
        Left,
        /// <summary>
        /// 向右
        /// </summary>
        Right,
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值