首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

托盘程序闪动图标有关问题

2012-02-26 
托盘程序闪动图标问题!我想做的是一个新闻提醒小程序,如果有新的新闻托盘图标就闪动!为什么第一次闪动正常

托盘程序闪动图标问题!
我想做的是一个新闻提醒小程序,如果有新的新闻托盘图标就闪动!为什么第一次闪动正常第二就不正常了闪动图标非常快,第三次有正常闪了!不知道为什么!希望高手帮我看下代码!或提供一些这方面的例子!using   System;
using   System.IO;
using   System.Net;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Text;
using   System.Collections.Generic;
using   System.Windows.Forms;


namespace   WindowsApplication1
{
        public   partial   class   Form1   :   Form
        {
                private   bool   m_bFlag   =   true;                
                //根据Url地址得到网页的HTML源码
                private   string   GetWebContent(string   Url)
                {
                        string   strResult   =   " ";
                        try
                        {
                                HttpWebRequest   request   =   (HttpWebRequest)WebRequest.Create(Url);
                                //声明一个HttpWebRequest请求
                                request.Timeout   =   30000;
                                //设置连接超时时间
                                request.Headers.Set( "Pragma ",   "no-cache ");
                                HttpWebResponse   response   =   (HttpWebResponse)request.GetResponse();
                                Stream   streamReceive   =   response.GetResponseStream();
                                Encoding   encoding   =   Encoding.GetEncoding( "gb2312 ");
                                StreamReader   streamReader   =   new   StreamReader(streamReceive,   encoding);
                                strResult   =   streamReader.ReadToEnd();
                                streamReader.Close();
                        }
                        catch
                        {
                                MessageBox.Show( "出错 ");


                        }
                        return   strResult;
                }
               
                public   Form1()
                {
                        InitializeComponent();
                        this.notifyIcon1.Icon   =   new   Icon( "1.ico ");
                        this.SizeChanged   +=   new   System.EventHandler(this.Form1_SizeChanged);
                        this.timer2.Enabled   =   true;
                        this.timer2.Tick   +=   new   System.EventHandler(this.timer2_Tick);
                }
                private   void   Form1_SizeChanged(object   sender,   System.EventArgs   e)
                {
                        if   (this.WindowState   ==   FormWindowState.Minimized)
                        {
                                this.Hide();
                        }
                }

                //关闭
                private   void   toolStripMenuItem4_Click(object   sender,   System.EventArgs   e)
                {
                        this.Close();
                }
                //显示
                private   void   toolStripMenuItem3_Click(object   sender,   EventArgs   e)
                {
                        if   (this.WindowState   ==   FormWindowState.Minimized)
                        {
                                this.Show();
                                this.WindowState   =   FormWindowState.Normal;
                                this.Activate();
                                this.timer1.Stop();
                                this.notifyIcon1.Icon   =   new   Icon( "1.ico ");


                        }
                }

                //抓
                private   void   timer2_Tick(object   sender,   System.EventArgs   e)
                {
                        //抓取数据
                        string   Url   =   "http://192.168.2.27:91/admin/Admin_User.asp ";
                        string   strWebContent   =   GetWebContent(Url);
                        FileStream   fs   =   new   FileStream(@ "1.txt ",   FileMode.Open,   FileAccess.Read);
                        StreamReader   sr   =   new   StreamReader(fs);
                        string   strData   =   sr.ReadToEnd();
                        sr.Close();
                        fs.Close();
                        if   (strWebContent   !=   strData)
                        {
                                this.webBrowser1.Refresh();
                                this.timer1.Start();
                                this.timer1.Tick   +=   new   EventHandler(this.timer1_Tick);
                                StreamWriter   sw   =   new   StreamWriter( "1.txt ");
                                sw.Write(strWebContent);
                                sw.Close();
                        }
                }
                //闪
                private   void   timer1_Tick(object   sender,   System.EventArgs   e)
                {
                        if   (m_bFlag)
                        {
                                this.notifyIcon1.Icon   =   new   Icon( "2.ico ");
                                m_bFlag   =   false;


                        }
                        else
                        {
                                this.notifyIcon1.Icon   =   new   Icon( "1.ico ");
                                m_bFlag   =   true;
                        }
                }
        }
}

[解决办法]
是不是timer1的interver设的太小了?
[解决办法]
有新的新闻就调用

private void NotifyUser()
{
int counter = 0;
while(++counter <=3)
{
NotifyIcon.Icon = "新值 ";
Thread.Sleep(100);
NotifyIcon.Icon = "旧值 ";
Thread.Sleep(100);
}
}
[解决办法]
public Form1()
{
InitializeComponent();
this.notifyIcon1.Icon = new Icon( "1.ico ");
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.timer2.Enabled = true;
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//这句放这里
this.timer1.Tick += new EventHandler(this.timer1_Tick);

}

热点排行