C#写的一个msn的消息提示类

来源:互联网 发布:上海租房 知乎 编辑:程序博客网 时间:2024/04/28 12:37
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication1
{
 /// <summary>
 /// 使用方法: 1)修改namespace适合您的工程
 ///      2)MsnAlert alert = new MsnAlert("我要显示的消息");
 ///      或MsnAlert alert = new MsnAlert("我要显示的消息",System.Drawing.ContentAlignment.MiddleCenter);
 /// </summary>
 public class MsnAlert : System.Windows.Forms.Form
 {
  private int heightMax, widthMax;
  public int StayTime = 1000;
  private System.ComponentModel.IContainer components;
  private System.Windows.Forms.Timer timer1;
  private System.Windows.Forms.Timer timer2;
  private System.Windows.Forms.Timer timer3;
  private System.Windows.Forms.Label label1;
  public MsnAlert(string msg)
  {
   InitializeComponent();
   
   this.heightMax = 120;//窗体滚动的高度
   this.widthMax = 148;//窗体滚动的宽度
   this.label1.Text = msg;
   this.ScrollShow();
  }
  public MsnAlert(string msg, System.Drawing.ContentAlignment textAlign)
  {
   InitializeComponent();
   
   this.label1.TextAlign = textAlign;
   this.heightMax = 120;//窗体滚动的高度
   this.widthMax = 148;//窗体滚动的宽度
   this.label1.Text = msg;
   this.ScrollShow();
  }
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   this.timer1 = new System.Windows.Forms.Timer(this.components);
   this.timer2 = new System.Windows.Forms.Timer(this.components);
   this.timer3 = new System.Windows.Forms.Timer(this.components);
   this.label1 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // timer1
   //
   this.timer1.Interval = 10;
   this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
   //
   // timer2
   //
   this.timer2.Interval = 10;
   this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
   //
   // timer3
   //
   this.timer3.Interval = 10;
   this.timer3.Tick += new System.EventHandler(this.timer3_Tick);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(8, 8);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(144, 128);
   this.label1.TabIndex = 0;
   this.label1.Text = "您有新的消息";
   this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   //
   // MsnAlert
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.BackColor = System.Drawing.SystemColors.Desktop;
   this.ClientSize = new System.Drawing.Size(160, 144);
   this.Controls.Add(this.label1);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
   this.Name = "MsnAlert";
   this.ShowInTaskbar = false;
   this.Text = "Form2";
   this.TopMost = true;
   this.Load += new System.EventHandler(this.Form_Load);
   this.ResumeLayout(false);
  }
  #endregion
  public int HeightMax
  {
   set
   {
    heightMax = value; 
   }
   get
   {
    return heightMax;
   }
  }
  public int WidthMax
  {
   set
   {
    widthMax = value;
   }
   get
   {
    return widthMax;
   }
  }

  private void ScrollShow()
  {
   this.Width = widthMax;
   this.Height = 0;
   this.Show();
   this.timer1.Enabled = true;
  }
  private void ScrollUp()
  {
   if(this.Height < heightMax)
   {
    this.Height += 3;
    this.Location = new Point(this.Location.X, this.Location.Y - 3);
   }
   else
   {
    this.timer1.Enabled = false;
    this.timer2.Enabled = true;
   }
  }
  private void ScrollDown()
  {
   if(this.Height > 3)
   {
    this.Height -= 3;
    this.Location = new Point(this.Location.X, this.Location.Y + 3);
   }
   else
   {
    this.timer3.Enabled = false;
    this.Close();
    this.Dispose(true);
   }
  }
  private void Form_Load(object sender, System.EventArgs e)
  {
   Screen[] screens = Screen.AllScreens;
   Screen screen = screens[0];
   this.Location = new Point(screen.WorkingArea.Width - widthMax - 20, screen.WorkingArea.Height - 34);
   this.timer2.Interval = StayTime;
  }
  private void timer1_Tick(object sender, System.EventArgs e)
  {
   ScrollUp();
  }
  private void timer2_Tick(object sender, System.EventArgs e)
  {
   timer2.Enabled = false;
   timer3.Enabled = true;
  } 
  private void timer3_Tick(object sender, System.EventArgs e)
  {
   ScrollDown();
  }
 }
}
原创粉丝点击