using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Dongke.WinForm.Controls
{
///
/// 状态栏控件
///
[ToolboxBitmap(typeof(StatusStrip))]
public partial class SsrAsyncStatusStrip : SsrStatusStrip
{
#region 常量
///
/// 通信中
///
private const string COMMTIME_TEXT = " {0}({1:F0}){2}";
///
/// 通信时长
///
private const string COMMTIMES_TEXT = "耗时{0:F0}秒 ";
///
/// 通信中
///
private static readonly string[] COMMTEXTS = new string[] { " ", ". ", ".. ", "..." };
#endregion
#region 成员变量
///
/// 是否通信状态
///
private bool _communicate = false;
///
/// 是否显示时间
///
private bool _showTimer = true;
///
/// 是否显示通信时长
///
private bool _showElapsedTimes = false;
///
/// 是否显示通信状态
///
private bool _showCommText = true;
///
/// 通信时文本
///
private string _commText = "通信中";
///
/// 通信显示文本索引
///
private int _commTextsIndex = 0;
///
/// 通信开始时间
///
private DateTime _commBeginTime;
///
///
///
private TimeSpan _commTimes = TimeSpan.Zero;
#endregion
#region 构造函数
///
/// 扩展的状态栏控件
///
public SsrAsyncStatusStrip()
{
this.InitializeComponent();
this.tmrDateTime.Interval = 1000;
this.tmrCommText.Interval = 1000;
this.timer_Tick(null, null);
this.tmrDateTime.Start();
this.tslblCommLable.Click += tslblCommLable_Click;
}
void tslblCommLable_Click(object sender, EventArgs e)
{
if(!this._communicate)
{
this.ClearCommTimes();
}
}
#endregion
#region 属性
///
/// 获取或设置控件的通信状态
///
[Description("获取或设置控件的通信状态。"), Category("CustomerEx")]
[DefaultValue(false)]
public bool Communicate
{
get
{
return this._communicate;
}
set
{
if (this._communicate != value)
{
this._communicate = value;
if (this._communicate)
{
this.StartCommunicate();
}
else
{
this.StopCommunicate();
}
}
}
}
///
/// 获取或设置控件是否显示本地时间。
///
[Description("获取或设置控件是否显示本地时间。"), Category("CustomerEx")]
[DefaultValue(true)]
public bool ShowTimer
{
get
{
return this._showTimer;
}
set
{
if (this._showTimer != value)
{
this._showTimer = value;
if (this._showTimer && this.Visible)
{
this.timer_Tick(null, null);
this.tmrDateTime.Start();
}
else if (this.tmrDateTime.Enabled)
{
this.tmrDateTime.Stop();
this.tslblDateTime.Text = string.Empty;
}
}
}
}
///
/// 获取或设置控件是否显示通信时长。
///
[Description("获取或设置控件是否显示通信时长。"), Category("CustomerEx")]
[DefaultValue(false)]
public bool ShowElapsedTimes
{
get
{
return this._showElapsedTimes;
}
set
{
if (this._showElapsedTimes != value)
{
this._showElapsedTimes = value;
if (this._showElapsedTimes && this._commTimes > TimeSpan.Zero)
{
this.tslblCommLable.Text = string.Format(COMMTIMES_TEXT, this._commTimes.TotalSeconds);
}
else
{
this.tslblCommLable.Text = string.Empty;
}
}
}
}
///
/// 获取或设置控件是否显示通信状态。
///
[Description("获取或设置控件是否显示通信状态。"), Category("CustomerEx")]
[DefaultValue(true)]
public bool ShowCommText
{
get
{
return this._showCommText;
}
set
{
if (this._showCommText != value)
{
this._showCommText = value;
}
}
}
///
/// 获取或设置通信状态的文本。
///
[Description("获取或设置通信状态的文本。"), Category("CustomerEx")]
[DefaultValue("通信中")]
public string CommText
{
get
{
return this._commText;
}
set
{
this._commText = value;
}
}
///
/// 获取或设置状态栏的文本。
///
[Description("获取或设置状态栏的文本。"), Category("CustomerEx")]
[DefaultValue("")]
public string StripText
{
get
{
return this.tslblStripText.Text;
}
set
{
this.tslblStripText.Text = value;
}
}
#endregion
#region 重写事件
///
///
///
///
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (this._showTimer && this.Visible)
{
this.timer_Tick(null, null);
this.tmrDateTime.Start();
}
else if (this.tmrDateTime.Enabled)
{
this.tmrDateTime.Stop();
this.tslblDateTime.Text = string.Empty;
}
}
#endregion
#region 事件处理
///
/// 刷新时间
///
///
///
private void timer_Tick(object sender, EventArgs e)
{
this.tslblDateTime.Text = DateTime.Now.ToString(Constant.DATETIME_FORMAT_YYYYMMDDHHMM);
}
///
/// 刷新时间
///
///
///
private void cTimer_Tick(object sender, EventArgs e)
{
TimeSpan td = DateTime.Now - this._commBeginTime;
this._commTextsIndex = this._commTextsIndex % COMMTEXTS.Length;
if (this._showCommText)
{
this.tslblCommLable.Text = string.Format(COMMTIME_TEXT, this._commText, td.TotalSeconds, COMMTEXTS[this._commTextsIndex++]);
}
else
{
this.tslblCommLable.Text = string.Empty;
}
}
#endregion
#region 公有方法
///
/// 清除通信时长显示。
///
public void ClearCommTimes()
{
this.tslblCommLable.Text = string.Empty;
}
#endregion
#region 私有方法
///
/// 开始通讯
///
private void StartCommunicate()
{
// 通信中
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate()
{
this.StartCommunicate();
}
));
}
else
{
this._commTextsIndex = 0;
this._commBeginTime = DateTime.Now;
this._commTimes = TimeSpan.Zero;
this.cTimer_Tick(null, null);
this.tmrCommText.Start();
this.tslblCommImage.Image = Properties.Resources.Status_Communicating_00;
}
}
///
/// 结束通讯
///
private void StopCommunicate()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate()
{
this.StopCommunicate();
}
));
}
else
{
this._commTimes = DateTime.Now - this._commBeginTime;
this.tmrCommText.Stop();
this._commTextsIndex = 0;
this.tslblCommImage.Image = null;
if (this._showElapsedTimes)
{
this.tslblCommLable.Text = string.Format(COMMTIMES_TEXT, this._commTimes.TotalSeconds);
}
else
{
this.tslblCommLable.Text = string.Empty;
}
this._commTimes = TimeSpan.Zero;
}
}
///
/// 设置消息内容
///
///
private void SetStripText(string text)
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate()
{
SetStripText(text);
}
));
}
else
{
this.tslblStripText.Text = text;
}
}
#endregion
}
}