• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

【06年博文搬家】用C#实现仿MSN提示框

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

(1)首先新建一个类库项目,命名为MessageNotify。
(2)在项目中添加一个窗体,命名为MessageNotifyForm,设置如下属性:
    ShowInTaskbar:Fasle;
    TopMost:True;
    FormBorderStyle:FixedToolWindow;
    增加三个Timer控件,分别命名为TimerUp,TimerStay,TimerDown,将其Interval设为10。
    在窗体上增加一个label控件,命名为lblContent。

(3)MessageNotifyForm的代码如下:

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace KoalaStudio.ModuleLibrary
{
    
internal partial class MessageNotifyForm : Form
    {               
        
//提示框高度
        private int _HeightMax;    
        
/// 提示框高度
        public int HeightMax
        {
            
set { _HeightMax = value; }           
        }

        
/// 提示框停留时间(单位:秒)
        public int StayTime
        {
            
set { this.TimerStay.Interval = value ; }
        } 


        
/// 显示窗体
        public void ShowMessage(string text)
        {                     
            
this.lblContent.Text = text;
            
this.TimerUp.Enabled = true;                    
            
this.Height = 0;
            
this.Show();            
        }
        
/// 显示窗体

        
public void ShowMessage(string text, string caption)


        {            
            
this.Text = caption  ;
            
this.lblContent.Text = text;
            
this.TimerUp.Enabled = true;
            
this.Height = 0;
            
this.Show();
        }     

        
/// 向上移动窗体
        private void MoveUp()
        {
            
if (this.Height < _HeightMax)
            {
                
this.Height += 3;
                
this.Location = new Point(Location.X, Location.Y - 3);
            }
            
else
            {
                
this.TimerUp.Enabled = false;
                
this.TimerStay.Enabled = true;
            }
        }

        
/// 向下移动窗体
        private void MoveDown()
        {
            
if (this.Height > 0)
            {
                
this.Height -= 3;
                
this.Location = new Point(Location.X, Location.Y + 3);
            }
            
else
            {
                
this.TimerDown.Enabled = false;
                
this.Close();                
            }
        }
        
public MessageNotifyForm()
        {
            InitializeComponent();           
        }   
        
private void MessageNotify_Load(object sender, EventArgs e)
        {
            
//根据不同的屏幕分辨率来确定窗体的初始位置
            Screen[] screens = Screen.AllScreens;
            Screen screen 
= screens[0];
            
this.Location = new Point(screen.WorkingArea.Width - this.Width - 2, screen.WorkingArea.Height - 30);
        }

        
private void TimerUp_Tick(object sender, EventArgs e)
        {
            
this.MoveUp();
        }

        
private void TimerStay_Tick(object sender, EventArgs e)
        {
            
this.TimerStay.Enabled = false;
            
this.TimerDown.Enabled = true;
        }

        
private void TimerDown_Tick(object sender, EventArgs e)
        {
            
this.MoveDown();
        }
    }
}

 

 

(4)添加一个类文件,命名为MessageNotify,代码如下:

using System;
using System.Collections.Generic;
using System.Text;
namespace KoalaStudio.ModuleLibrary
{
    
public  class MessageNotify
    {  
        
//当前显示的提示框的数量
        private static int _Count = 0;
        
//提示框宽度
        private static int _WidthMax = 180;
        
//提示框高度
        private static int _HeightMax = 120;
        
//提示框停留时间(单位:毫秒)
        private static int _StayTime = 5000;
        
/// 提示框宽度
        public static int WidthMax
        {
            
set { _WidthMax = value; }
            
get { return _WidthMax; }
        }
        
/// 提示框高度
        public static int HeightMax
        {
            
set { _HeightMax = value; }
            
get { return _HeightMax; }
        }
        
/// 提示框停留时间(单位:秒)
        public static int StayTime
        {
            
set { _StayTime = value; }
            
get { return _StayTime / 1000; }
        }
        
public static void Show(string text)
        {            
            MessageNotifyForm messageNotifyForm 
= new MessageNotifyForm();
            messageNotifyForm.Width 
= _WidthMax;
            messageNotifyForm.HeightMax 
= _HeightMax;
            messageNotifyForm.StayTime 
= _StayTime;
            messageNotifyForm.ShowMessage(text);
        }
        
public static void Show(string text, string caption)
        {
            MessageNotifyForm messageNotifyForm 
= new MessageNotifyForm();
            messageNotifyForm.Width 
= _WidthMax;
            messageNotifyForm.HeightMax 
= _HeightMax;
            messageNotifyForm.StayTime 
= _StayTime;
            messageNotifyForm.ShowMessage(text, caption);
        }
    }
}

 

(5)将项目生成为dll文件,在应用中引用后即可直接调用,格式如下:    
    MessageNotify.Show("在窗体中显示的提示信息");        
    MessageNotify.Show("在窗体中显示的提示信息","在标题栏中显示的标题");    

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#委托初探发布时间:2022-07-14
下一篇:
C#设计模式(8)-BuilderPattern发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap