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,
}
}
自写控件:滑动呈现控件(实现了两个以上控件间的切换)我写的
最新推荐文章于 2022-11-18 11:18:17 发布