现在的位置: 首页 > 综合 > 正文

WPF控件–NotifyIcon

2013年03月07日 ⁄ 综合 ⁄ 共 2464字 ⁄ 字号 评论关闭

1.在什么地方找到NotifyIcon

普通的WPF控件基本上都是在该命名空间下:System.Windows.Controls,该命名空间在C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.dll下。也就是说是.net framework3.0之后才支持的。

那难道在WPF下就不能使用NotifyIcon了吗?

在MSDN上有以下关于通知图标的示例:http://msdn.microsoft.com/zh-cn/library/aa972170(VS.90).aspx

using System;
using System.Windows;

using System.Windows.Forms; // NotifyIcon control
using System.Drawing; // Icon

public partial class MainWindow : Window
{
    NotifyIcon notifyIcon;

    public MainWindow()
    {
        InitializeComponent();
    }

    void click(object sender, RoutedEventArgs e)
    {
        // Configure and show a notification icon in the system tray
        this.notifyIcon = new NotifyIcon();
        this.notifyIcon.BalloonTipText = "Hello, NotifyIcon!";
        this.notifyIcon.Text = "Hello, NotifyIcon!";
        this.notifyIcon.Icon = new System.Drawing.Icon("NotifyIcon.ico");
        this.notifyIcon.Visible = true;
        this.notifyIcon.ShowBalloonTip(1000);
    }
}

其中包含NotifyIcon控件,请注意它的命名空间:System.Windows.Forms; ,该命名空间是在C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll下。所以说此处使用的NotifyIcon控件其实是.net framework 2.0就提供的在winform下面是用的控件。

2.怎么使用NotifyIcon

App.xaml

<Application x:Class="NotifyIconStd.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             ShutdownMode="OnExplicitShutdown" Startup="ApplicationStartup" Exit="ApplicationExit">
    <Application.Resources>
         
    </Application.Resources>
</Application>

App.xaml.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using Application = System.Windows.Application;

namespace NotifyIconStd
{
    public partial class App : Application
    {
        private static NotifyIcon trayIcon;

        private void ApplicationStartup(object sender, StartupEventArgs e)
        {
            RemoveTrayIcon();
            AddTrayIcon();
        }

        private void AddTrayIcon()
        {
            if (trayIcon != null)
            {
                return;
            }
            trayIcon = new NotifyIcon
            {
                Icon = new System.Drawing.Icon("notifyIcon.ico"),
                Text = "NotifyIconStd"
            };
            trayIcon.Visible = true;

            ContextMenu menu = new ContextMenu();

            MenuItem closeItem = new MenuItem();
            closeItem.Text = "Close";
            closeItem.Click += new EventHandler(delegate { this.Shutdown(); });

            MenuItem addItem = new MenuItem();
            addItem.Text = "Menu";

            menu.MenuItems.Add(addItem);
            menu.MenuItems.Add(closeItem);

            trayIcon.ContextMenu = menu;    //设置NotifyIcon的右键弹出菜单
        }

        private void RemoveTrayIcon()
        {
            if (trayIcon != null)
            {
                trayIcon.Visible = false;
                trayIcon.Dispose();
                trayIcon = null;
            }
        }

        private void ApplicationExit(object sender, ExitEventArgs e)
        {
            RemoveTrayIcon();
        }
    }
}

该示例程序程序只是在程序启动时,同时启动了一个NotifyIcon,没有其他主程序界面,可以在MenuItem的事件中添加关于弹出其他窗口的处理。

抱歉!评论已关闭.