zhangyu028

导航

用C#写的一个winForm的短消息提示程序

    最近用C#+WebService写了一个winForm的短消息提示程序

关键提示窗口代码如下:

frm_Message.cs
   
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace MessageClient
{
    
/// <summary>
    
/// frm_Message 的摘要说明。
    
/// </summary>

    public class frm_Message : System.Windows.Forms.Form
    
{
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;
        
private System.Windows.Forms.PictureBox btn_cancel;
        
private System.Windows.Forms.PictureBox btn_search;
        
private System.Windows.Forms.Label label1;
        Thread t;
        
string msgLabel;
        System.Windows.Forms.NotifyIcon notifyicon;
        System.Windows.Forms.Timer timer;

        
public frm_Message(string number,string mlabel,System.Windows.Forms.NotifyIcon ni,System.Windows.Forms.Timer tm)
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            this.Opacity=0;
            InitializeComponent();
            
this.label1.Text="你有新消息:"+number+"";
            
this.msgLabel=mlabel;
            notifyicon
=ni;
            timer
=tm;            
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if(components != null)
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
private void frm_Message_Load(object sender, System.EventArgs e)
        
{
            t
= new Thread(new ThreadStart(ShowForm));
            t.Start();            
        }


        
private void ShowForm()
        
{            
            
int Tag = this.Height; 
            Height 
= 5
            
//定位到屏幕右下角
            Screen [] screens = Screen.AllScreens;                        
            
            Top 
= screens[0].WorkingArea.Height-5
            Left 
= screens[0].WorkingArea.Width - Width - 2
            
            
this.Opacity=100;
            
//从屏幕右下角逐渐弹出
            while (Height<Tag)
            
{
                Height 
= Height + 5
                Top 
= Top - 5
                Thread.Sleep(
100); 
            }

            Height 
= Tag; 
            Tag 
= 0
            Thread.Sleep(
5000); 
            HideForm();
        }


        
private void HideForm()
        
{    
            
//往屏幕右下角逐渐隐去            
            Screen [] screens = Screen.AllScreens;    
            
int _top=screens[0].WorkingArea.Height-5;
            
while (Top<_top)
            
{
                Height 
= Height - 5
                Top 
= Top + 5;                                 
                Thread.Sleep(
100); 
            }

            Height 
=0;             
            
this.Close();
            
this.Dispose();
        }


        
private void pictureBox1_Click(object sender, System.EventArgs e)
        
{
            
this.Close();
        }


        
private void btn_search_Click(object sender, System.EventArgs e)
        
{
            frm_showmessage frmshowmessage
=new frm_showmessage(msgLabel,notifyicon,timer);
            frmshowmessage.Show();
        }
        

        
private void frm_Message_MouseLeave(object sender, System.EventArgs e)
        
{
            
this.t.Resume();
        }


        
private void frm_Message_MouseEnter(object sender, System.EventArgs e)
        
{
            
this.t.Suspend();
        }


        
private void label1_MouseHover(object sender, System.EventArgs e)
        
{
            
this.t.Suspend();
        }


        
private void frm_Message_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        
{
            
this.t.Abort();
        }


        
    }

}

 

说明:

调用方法:

frm_Message frmmessage=new frm_Message(um.Length.ToString(),userID,this.notifyIcon1,this.timer2);
frmmessage.Show();

um是我能过WebService取到的消息结构数组
userID是用户名,也就是消息接收人
notifyIcon1是托盘图标
timer2是时钟,用于在有短消息时在两个不同的图标之间进行切换,产生图标闪动的效果。


第一次写这个东东,而且读书时语文成绩就是拖我总分的科,肯定写的不咋样,要不然我早去当记者了:)。
有什么问题,大家可以问我。

posted on 2005-12-14 10:33  张 宇  阅读(2748)  评论(3)    收藏  举报