自定义控件msgdiv

继承label父类

  1  public class MsgDiv
  2     {
  3         private Timer timerLable = new Timer();
  4         /// <summary>
  5         /// 消息回调 委托对象
  6         /// </summary>
  7         private DGMsgDiv dgCallBack = null;
  8 
  9 
 10         #region 计时器
 11         /// <summary>
 12         /// 计时器
 13         /// </summary>
 14         public Timer TimerMsg
 15         {
 16             get { return timerLable; }
 17             set { timerLable = value; }
 18         }
 19         #endregion
 20 
 21         #region  MsgDiv构造函数
 22         /// <summary>
 23         /// MsgDiv构造函数
 24         /// </summary>
 25         public MsgDiv()
 26         {
 27             InitallMsgDiv(7, 7);
 28         }
 29 
 30         /// <summary>
 31         /// MsgDiv构造函数
 32         /// </summary>
 33         /// <param name="x">定位x轴坐标</param>
 34         /// <param name="y">定位y轴坐标</param>
 35         public MsgDiv(int x, int y)
 36         {
 37             InitallMsgDiv(x, y);
 38         }
 39         #endregion
 40 
 41         #region  初始化消息条
 42         /// <summary>
 43         /// 初始化消息条
 44         /// </summary>
 45         private void InitallMsgDiv(int x, int y)
 46         {
 47             this.AutoSize = true;
 48             this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
 49             this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
 50             //this.ContextMenuStrip = this.cmsList;
 51             this.Font = new System.Drawing.Font("宋体", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
 52             this.ForeColor = System.Drawing.Color.Red;
 53             this.Location = new System.Drawing.Point(x, y);
 54             this.MaximumSize = new System.Drawing.Size(980, 525);
 55             this.Name = "msgDIV";
 56             this.Padding = new System.Windows.Forms.Padding(7);
 57             this.Size = new System.Drawing.Size(71, 31);
 58             this.TabIndex = 1;
 59             this.Text = "消息条";
 60             this.Visible = false;
 61             //给委托添加事件
 62             this.DoubleClick += new System.EventHandler(this.msgDIV_DoubleClick);
 63             this.MouseLeave += new System.EventHandler(this.msgDIV_MouseLeave);
 64             this.MouseHover += new System.EventHandler(this.msgDIV_MouseHover);
 65             this.timerLable.Interval = 1000;
 66             this.timerLable.Tick += new System.EventHandler(this.timerLable_Tick);
 67         }
 68         #endregion
 69 
 70         #region 将消息条添加到指定容器上
 71         /// <summary>
 72         /// 将消息条添加到指定容器上Form
 73         /// </summary>
 74         /// <param name="form"></param>
 75         public void AddToControl(Form form)
 76         {
 77             form.Controls.Add(this);
 78         }
 79         /// <summary>
 80         /// 将消息条添加到指定容器上GroupBox
 81         /// </summary>
 82         /// <param name="form"></param>
 83         public void AddToControl(GroupBox groupBox)
 84         {
 85             groupBox.Controls.Add(this);
 86         }
 87         /// <summary>
 88         /// 将消息条添加到指定容器上Panel
 89         /// </summary>
 90         /// <param name="form"></param>
 91         public void AddToControl(Panel panel)
 92         {
 93             panel.Controls.Add(this);
 94         }
 95         #endregion
 96 
 97         //---------------------------------------------------------------------------
 98         #region 消息显示 的相关参数们 hiddenClick,countNumber,constCountNumber
 99         /// <summary>
100         /// 当前显示了多久的秒钟数
101         /// </summary>
102         int hiddenClick = 0;
103         /// <summary>
104         /// 要显示多久的秒钟数 可变参数
105         /// </summary>
106         int countNumber = 3;
107         /// <summary>
108         /// 要显示多久的秒钟数 固定参数
109         /// </summary>
110         int constCountNumber = 3;
111         #endregion
112 
113         #region 计时器 显示countNumber秒钟后自动隐藏div -timerLable_Tick(object sender, EventArgs e)
114         private void timerLable_Tick(object sender, EventArgs e)
115         {
116             if (hiddenClick > countNumber - 2)
117             {
118                 MsgDivHidden();
119             }
120             else
121             {
122                 hiddenClick++;
123                 //RemainCount();
124             }
125         }
126         #endregion
127 
128         #region 隐藏消息框 并停止计时 +void MsgDivHidden()
129         /// <summary>
130         /// 隐藏消息框 并停止计时
131         /// </summary>
132         public void MsgDivHidden()
133         {
134             this.Text = "";
135             this.Visible = false;
136             this.hiddenClick = 0;
137             //this.tslblRemainSecond.Text = "";
138             if (this.timerLable.Enabled == true)
139                 this.timerLable.Stop();
140 
141             //调用 委托 然后清空委托
142             if (dgCallBack != null && dgCallBack.GetInvocationList().Length > 0)
143             {
144                 dgCallBack();
145                 dgCallBack -= dgCallBack;
146             }
147         }
148         #endregion
149 
150         #region 在消息框中显示消息字符串 +void MsgDivShow(string msg)
151         /// <summary>
152         /// 在消息框中显示消息字符串
153         /// </summary>
154         /// <param name="msg">要显示的字符串</param>
155         public void MsgDivShow(string msg)
156         {
157             this.Text = msg;
158             this.Visible = true;
159             this.countNumber = constCountNumber;//默认设置显示秒数为10;
160             this.hiddenClick = 0;//重置倒数描述
161             this.timerLable.Start();
162         }
163         #endregion
164 
165         #region 在消息框中显示消息字符串 并在消息消失时 调用回调函数 +void MsgDivShow(string msg, DGMsgDiv callback)
166         /// <summary>
167         /// 在消息框中显示消息字符串 并在消息消失时 调用回调函数
168         /// </summary>
169         /// <param name="msg">要显示的字符串</param>
170         /// <param name="callback">回调函数</param>
171         public void MsgDivShow(string msg, DGMsgDiv callback)
172         {
173             MsgDivShow(msg);
174             dgCallBack = callback;
175         }
176         #endregion
177 
178         #region 在消息框中显示消息字符串 并在指定时间消息消失时 调用回调函数 +void MsgDivShow(string msg, int seconds, DGMsgDiv callback)
179         /// <summary>
180         /// 在消息框中显示消息字符串 并在消息消失时 调用回调函数
181         /// </summary>
182         /// <param name="msg">要显示的字符串</param>
183         /// <param name="seconds">消息显示时间</param>
184         /// <param name="callback">回调函数</param>
185         public void MsgDivShow(string msg, int seconds, DGMsgDiv callback)
186         {
187             MsgDivShow(msg, seconds);
188             dgCallBack = callback;
189         }
190         #endregion
191 
192         #region 在消息框中显示消息字符串,并指定消息框显示秒数 +void MsgDivShow(string msg, int seconds)
193         /// <summary>
194         /// 在消息框中显示消息字符串,并指定消息框显示秒数
195         /// </summary>
196         /// <param name="msg">要显示的字符串</param>
197         /// <param name="seconds">消息框显示秒数</param>
198         public void MsgDivShow(string msg, int seconds)
199         {
200             this.Text = msg;
201             this.Visible = true;
202             this.countNumber = seconds;
203             this.hiddenClick = 0;//重置倒数描述
204             this.timerLable.Start();
205         }
206         #endregion
207 
208         //---------------------------------------------------------------------------
209         #region 事件们~~~! msgDIV_MouseHover,msgDIV_MouseLeave,msgDIV_DoubleClick
210         //当鼠标停留在div上时 停止计时
211         private void msgDIV_MouseHover(object sender, EventArgs e)
212         {
213             if (this.timerLable.Enabled == true)
214                 this.timerLable.Stop();
215         }
216         //当鼠标从div上移开时 继续及时
217         private void msgDIV_MouseLeave(object sender, EventArgs e)
218         {
219             //当消息框正在显示、回复框没显示、计时器正停止的时候,重新启动计时器
220             if (this.Visible == true && this.timerLable.Enabled == false)
221                 this.timerLable.Start();
222         }
223         //双击消息框时关闭消息框
224         private void msgDIV_DoubleClick(object sender, EventArgs e)
225         {
226             MsgDivHidden();
227         }
228         #endregion
229 
230     }
原文地址:https://www.cnblogs.com/zhanying/p/4097167.html