| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
-
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Dongke.WinForm.Controls
- {
- /// <summary>
- /// 状态栏控件
- /// </summary>
- [ToolboxBitmap(typeof(StatusStrip))]
- public partial class SsrAsyncStatusStrip : SsrStatusStrip
- {
- #region 常量
- /// <summary>
- /// 通信中
- /// </summary>
- private const string COMMTIME_TEXT = " {0}({1:F0}){2}";
- /// <summary>
- /// 通信时长
- /// </summary>
- private const string COMMTIMES_TEXT = "耗时{0:F0}秒 ";
- /// <summary>
- /// 通信中
- /// </summary>
- private static readonly string[] COMMTEXTS = new string[] { " ", ". ", ".. ", "..." };
- #endregion
- #region 成员变量
- /// <summary>
- /// 是否通信状态
- /// </summary>
- private bool _communicate = false;
- /// <summary>
- /// 是否显示时间
- /// </summary>
- private bool _showTimer = true;
- /// <summary>
- /// 是否显示通信时长
- /// </summary>
- private bool _showElapsedTimes = false;
- /// <summary>
- /// 是否显示通信状态
- /// </summary>
- private bool _showCommText = true;
- /// <summary>
- /// 通信时文本
- /// </summary>
- private string _commText = "通信中";
- /// <summary>
- /// 通信显示文本索引
- /// </summary>
- private int _commTextsIndex = 0;
- /// <summary>
- /// 通信开始时间
- /// </summary>
- private DateTime _commBeginTime;
- /// <summary>
- ///
- /// </summary>
- private TimeSpan _commTimes = TimeSpan.Zero;
- #endregion
- #region 构造函数
- /// <summary>
- /// 扩展的状态栏控件
- /// </summary>
- 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 属性
- /// <summary>
- /// 获取或设置控件的通信状态
- /// </summary>
- [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();
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否显示本地时间。
- /// </summary>
- [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;
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否显示通信时长。
- /// </summary>
- [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;
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置控件是否显示通信状态。
- /// </summary>
- [Description("获取或设置控件是否显示通信状态。"), Category("CustomerEx")]
- [DefaultValue(true)]
- public bool ShowCommText
- {
- get
- {
- return this._showCommText;
- }
- set
- {
- if (this._showCommText != value)
- {
- this._showCommText = value;
- }
- }
- }
- /// <summary>
- /// 获取或设置通信状态的文本。
- /// </summary>
- [Description("获取或设置通信状态的文本。"), Category("CustomerEx")]
- [DefaultValue("通信中")]
- public string CommText
- {
- get
- {
- return this._commText;
- }
- set
- {
- this._commText = value;
- }
- }
- /// <summary>
- /// 获取或设置状态栏的文本。
- /// </summary>
- [Description("获取或设置状态栏的文本。"), Category("CustomerEx")]
- [DefaultValue("")]
- public string StripText
- {
- get
- {
- return this.tslblStripText.Text;
- }
- set
- {
- this.tslblStripText.Text = value;
- }
- }
- #endregion
- #region 重写事件
- /// <summary>
- ///
- /// </summary>
- /// <param name="e"></param>
- 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 事件处理
- /// <summary>
- /// 刷新时间
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void timer_Tick(object sender, EventArgs e)
- {
- this.tslblDateTime.Text = DateTime.Now.ToString(Constant.DATETIME_FORMAT_YYYYMMDDHHMM);
- }
- /// <summary>
- /// 刷新时间
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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 公有方法
- /// <summary>
- /// 清除通信时长显示。
- /// </summary>
- public void ClearCommTimes()
- {
- this.tslblCommLable.Text = string.Empty;
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 开始通讯
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 结束通讯
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 设置消息内容
- /// </summary>
- /// <param name="text"></param>
- private void SetStripText(string text)
- {
- if (this.InvokeRequired)
- {
- this.Invoke(new MethodInvoker(delegate()
- {
- SetStripText(text);
- }
- ));
- }
- else
- {
- this.tslblStripText.Text = text;
- }
- }
- #endregion
- }
- }
|