#region 属性
private bool _designAnimotion = false;
[Description("设置和获取设计模式下开启动画预览"), Category("IDSkinSwitchButton属性")]
public bool DesignAnimotion
{
get { return _designAnimotion; }
set { _designAnimotion = value; }
}
private StrockState _strockState = StrockState.Close;
/// <summary>
/// 设置和获取按钮状态(Open或Close)
/// </summary>
[Description("设置和获取按钮状态(Open或Close)"), Category("IDSkinSwitchButton属性")]
public StrockState StrockState
{
get { return _strockState; }
set
{
_strockState = value;
SetStyle();
if (_strockState == StrockState.Open)
{
if (Orientation == Orientation.Horizontal)
{
//如果是横向模式,则启用横向Open动画
int left = this.Width - StrockButton.Width - SwitchViewStrockWidth;
if (DesignMode && !DesignAnimotion)
{
StrockButton.Left = left;
return;
}
StrockButton.DoEffect(StrockButton.Left, left, StrockSpeed, "Left", (a) => { });
}
else if (Orientation == Orientation.Vertical)
{
//如果是纵向模式,则启用纵向Open动画
int top = this.Height - StrockButton.Height - SwitchViewStrockWidth;
if (DesignMode && !DesignAnimotion)
{
StrockButton.Top = top;
return;
}
StrockButton.DoEffect(StrockButton.Top, top, StrockSpeed, "Top", (a) => { });
}
}
else if (_strockState == StrockState.Close)
{
if (Orientation == Orientation.Horizontal)
{
//如果是横向模式,则启用横向Close动画
int left = SwitchViewStrockWidth;
if (DesignMode && !DesignAnimotion)
{
StrockButton.Left = left;
return;
}
StrockButton.DoEffect(StrockButton.Left, left, StrockSpeed, "Left", (a) => { });
}
else if (Orientation == Orientation.Vertical)
{
//如果是纵向模式,则启用纵向Close动画
int top = SwitchViewStrockWidth;
if (DesignMode && !DesignAnimotion)
{
StrockButton.Top = top;
return;
}
StrockButton.DoEffect(StrockButton.Top, top, StrockSpeed, "Top", (a) => { });
}
}
}
}
private int _strockSpeed = 200;
/// <summary>
/// 设置和获取滑块滑动的速度。(单位毫秒,如200,滑块将在200毫秒内完成动画,数值越小动画越快)
/// </summary>
[Description("设置和获取滑块滑动的速度。(单位毫秒,如200,滑块将在200毫秒内完成动画,数值越小动画越快)"), Category("IDSkinSwitchButton属性")]
public int StrockSpeed
{
get { return _strockSpeed; }
set { _strockSpeed = value; }
}
private DuiButton _strockButton = null;
/// <summary>
/// 滑块按钮
/// </summary>
[Description("滑块按钮"), Category("IDSkinSwitchButton属性"), Browsable(false)]
public DuiButton StrockButton
{
get
{
if (_strockButton == null || _strockButton.IsDisposed)
{
_strockButton = new DuiButton();
_strockButton.Text = "";
_strockButton.IsPureColor = true;
_strockButton.BaseColor = SwitchViewStrockColor;
_strockButton.DesignModeCanMove = false;
_strockButton.DesignModeCanSelect = false;
_strockButton.DesignModeCanResize = false;
_strockButton.ButtonBorderColor = Color.Transparent;
_strockButton.MouseClick += (s, e) =>
{
StrockState = StrockState == StrockState.Open ? StrockState.Close : StrockState.Open;
OnStrockStateChanged(e);
};
}
return _strockButton;
}
}
/// <summary>
/// 滑块显示模式。(Horizontal:横向;Vertical:纵向)
/// </summary>
[Description("滑块显示模式。(Horizontal:横向;Vertical:纵向)"), Category("IDSkinSwitchButton属性")]
private Orientation _orientation = Orientation.Horizontal;
public Orientation Orientation
{
get { return _orientation; }
set
{
_orientation = value;
SetRadius();
}
}
private Color _switchViewStrockColor = Color.White;
/// <summary>
/// 滑块背景色
/// </summary>
[Description("滑块背景色"), Category("IDSkinSwitchButton属性")]
public Color SwitchViewStrockColor
{
get { return _switchViewStrockColor; }
set
{
_switchViewStrockColor = value;
SetStyle();
}
}
private Color _switchViewStrockHoverColor = Color.White;
/// <summary>
/// 滑块鼠标移入时的背景色
/// </summary>
[Description("滑块鼠标移入时的背景色"), Category("IDSkinSwitchButton属性")]
public Color SwitchViewStrockHoverColor
{
get { return _switchViewStrockHoverColor; }
set
{
_switchViewStrockHoverColor = value;
SetStyle();
}
}
private Color _switchViewStrockPressColor = Color.White;
/// <summary>
/// 滑块鼠标移入时的背景色
/// </summary>
[Description("滑块鼠标点击时的背景色"), Category("IDSkinSwitchButton属性")]
public Color SwitchViewStrockPressColor
{
get { return _switchViewStrockPressColor; }
set
{
_switchViewStrockPressColor = value;
SetStyle();
}
}
private Color _switchViewStrockOpenColor = Color.FromArgb(26, 172, 25);
/// <summary>
/// 获取和设置打开状态时的背景色
/// </summary>
[Description("获取和设置打开状态时的背景色"), Category("IDSkinSwitchButton属性")]
public Color SwitchViewStrockOpenColor
{
get { return _switchViewStrockOpenColor; }
set
{
_switchViewStrockOpenColor = value;
SetStyle();
}
}
private Color _switchViewStrockCloseColor = Color.FromArgb(153, 153, 153);
/// <summary>
/// 获取和设置关闭状态时的背景色
/// </summary>
[Description("获取和设置关闭状态时的背景色"), Category("IDSkinSwitchButton属性")]
public Color SwitchViewStrockCloseColor
{
get { return _switchViewStrockCloseColor; }
set
{
_switchViewStrockCloseColor = value;
SetStyle();
}
}
private int _switchViewStrockWidth = 2;
/// <summary>
/// 获取和设置滑块与控件之间的间隙
/// </summary>
[Description("获取和设置滑块与控件之间的间隙"), Category("IDSkinSwitchButton属性")]
public int SwitchViewStrockWidth
{
get { return _switchViewStrockWidth; }
set
{
_switchViewStrockWidth = value;
SetRadius();
}
}
#endregion
#region 方法
/// <summary>
/// 设置滑快圆角样式和控件圆角样式
/// </summary>
public void SetRadius()
{
if (Orientation == Orientation.Horizontal)
{
this.Radius = this.Height;
StrockButton.Size = new Size(this.Height - SwitchViewStrockWidth*2,
this.Height - SwitchViewStrockWidth*2);
StrockButton.Radius = StrockButton.Width;
StrockButton.Location = StrockState == StrockState.Close
? new Point(SwitchViewStrockWidth, SwitchViewStrockWidth)
: new Point(this.Width - StrockButton.Width - SwitchViewStrockWidth, SwitchViewStrockWidth);
}
else if (Orientation == Orientation.Vertical)
{
this.Radius = this.Width;
StrockButton.Size = new Size(this.Width - SwitchViewStrockWidth*2, this.Width - SwitchViewStrockWidth*2);
StrockButton.Radius = StrockButton.Width;
StrockButton.Location = StrockState == StrockState.Close
? new Point(SwitchViewStrockWidth, SwitchViewStrockWidth)
: new Point(SwitchViewStrockWidth, this.Height - StrockButton.Height - SwitchViewStrockWidth);
}
}
/// <summary>
/// 设置滑块和控件颜色样式
/// </summary>
public void SetStyle()
{
this.HoverColor =
this.PressColor =
this.BaseColor =
StrockState == StrockState.Open ? SwitchViewStrockOpenColor : SwitchViewStrockCloseColor;
StrockButton.BaseColor = _switchViewStrockColor;
StrockButton.HoverColor = _switchViewStrockHoverColor;
StrockButton.PressColor = _switchViewStrockPressColor;
}
#endregion