WPF初探——利用Winform库中的NotifyIcon实现托盘小程序

       前天晚上睡觉前萌生实现一个小软件的想法,界面准备采用类似Demon tools的NotifyIcon界面。今天游玩归来准备动手写的时候,在工具栏里面却发现没找到托盘控件,囧。既然没有,就自己实现下吧,参考了MSDN上给出的代码样板,于是写了如下一个,也算是为下一个小软件做下铺垫吧~

      运行界面如下所示:

                 

       图1                                             图2

 

       代码很少,如下所示:       


using System;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;

namespace Royen
{
    
public partial class SysTray : Window
    {
        
private NotifyIcon notifyIcon=null;

        
public SysTray()
        {
            InitializeComponent();

            InitialTray();
        }

        
private void InitialTray()
        {
            
//隐藏主窗体
            this.Visibility = Visibility.Hidden;
                
            
//设置托盘的各个属性
            notifyIcon = new NotifyIcon();
            notifyIcon.BalloonTipText 
= "systray runnning...";         
            notifyIcon.Text 
= "systray";
            notifyIcon.Icon 
= new System.Drawing.Icon("http://www.cnblogs.com/res/spring.ico");
            notifyIcon.Visible 
= true;
            notifyIcon.ShowBalloonTip(
2000);            
            notifyIcon.MouseClick 
+= new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
            
            
//设置菜单项
            MenuItem setting1 = new MenuItem("setting1");
            MenuItem setting2 
= new MenuItem("setting2");
            MenuItem setting 
= new MenuItem("setting"new MenuItem[]{setting1,setting2});

            
//帮助选项
            MenuItem help = new MenuItem("help");

            
//关于选项
            MenuItem about = new MenuItem("about");

            
//退出菜单项
            MenuItem exit = new MenuItem("exit");
            exit.Click 
+= new EventHandler(exit_Click);

            
//关联托盘控件
            MenuItem[] childen = new MenuItem[] {setting,help,about,exit};
            notifyIcon.ContextMenu 
= new ContextMenu(childen);

            
//窗体状态改变时候触发
            this.StateChanged += new EventHandler(SysTray_StateChanged);
        }
      
        
/// <summary>
        
/// 鼠标单击
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            
//如果鼠标左键单击
            if (e.Button == MouseButtons.Left)
            {
                
if (this.Visibility == Visibility.Visible)
                {
                    
this.Visibility = Visibility.Hidden;
                }
                
else
                {
                    
this.Visibility = Visibility.Visible;
                    
this.Activate();
                }
            }
        }

        
/// <summary>
        
/// 窗体状态改变时候触发
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void SysTray_StateChanged(object sender, EventArgs e)
        {
            
if (this.WindowState == WindowState.Minimized)
            {
                
this.Visibility = Visibility.Hidden;
            }
        }          


        
/// <summary>
        
/// 退出选项
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void exit_Click(object sender, EventArgs e)
        {
            
if (System.Windows.MessageBox.Show("sure to exit?",
                                               
"application",
                                                MessageBoxButton.YesNo,
                                                MessageBoxImage.Question,
                                                MessageBoxResult.No) 
== MessageBoxResult.Yes)
            {
                System.Windows.Application.Current.Shutdown();
            }
        }
    }
}

 

 

     工程源码下载:/Files/royenhome/Royen.rar 。真诚的希望和大家学习交流~

 

 

 

 

 

 

 

 

 

 

 


 

 

posted @ 2010-02-02 21:52  royen  阅读(9336)  评论(13编辑  收藏  举报