[code=XAML]<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
Closing="OnClose" StateChanged="OnStateChanged" IsVisibleChanged="OnIsVisibleChanged">
<Grid> </Grid>
</Window>
[/code]using System;
using System.ComponentModel;
using System.Windows;namespace WpfApplication2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private System.Windows.Forms.NotifyIcon m_notifyIcon; public Window1()
{
InitializeComponent();
// initialise code here
m_notifyIcon = new System.Windows.Forms.NotifyIcon();
m_notifyIcon.BalloonTipText = "The app has been minimised. Click the tray icon to show.";
m_notifyIcon.BalloonTipTitle = "The App";
m_notifyIcon.Text = "The App";
m_notifyIcon.Icon = new System.Drawing.Icon("TheAppIcon.ico");
m_notifyIcon.Click += new EventHandler(m_notifyIcon_Click);
} private void OnClose(object sender, CancelEventArgs args)
{
m_notifyIcon.Dispose();
m_notifyIcon = null;
} private WindowState m_storedWindowState = WindowState.Normal; private void OnStateChanged(object sender, EventArgs args)
{
if (WindowState == WindowState.Minimized)
{
Hide();
if (m_notifyIcon != null)
m_notifyIcon.ShowBalloonTip(2000);
}
else
m_storedWindowState = WindowState;
} private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
{
CheckTrayIcon();
} private void m_notifyIcon_Click(object sender, EventArgs e)
{
Show();
WindowState = m_storedWindowState;
} private void CheckTrayIcon()
{
ShowTrayIcon(!IsVisible);
} private void ShowTrayIcon(bool show)
{
if (m_notifyIcon != null)
m_notifyIcon.Visible = show;
}
}
}
记得添加对System.Windows.Forms和System.Drawing的引用。
添加一个ico文件,并将其属性设置为Embeded Resource
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
Closing="OnClose" StateChanged="OnStateChanged" IsVisibleChanged="OnIsVisibleChanged">
<Grid> </Grid>
</Window>
[/code]using System;
using System.ComponentModel;
using System.Windows;namespace WpfApplication2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private System.Windows.Forms.NotifyIcon m_notifyIcon; public Window1()
{
InitializeComponent();
// initialise code here
m_notifyIcon = new System.Windows.Forms.NotifyIcon();
m_notifyIcon.BalloonTipText = "The app has been minimised. Click the tray icon to show.";
m_notifyIcon.BalloonTipTitle = "The App";
m_notifyIcon.Text = "The App";
m_notifyIcon.Icon = new System.Drawing.Icon("TheAppIcon.ico");
m_notifyIcon.Click += new EventHandler(m_notifyIcon_Click);
} private void OnClose(object sender, CancelEventArgs args)
{
m_notifyIcon.Dispose();
m_notifyIcon = null;
} private WindowState m_storedWindowState = WindowState.Normal; private void OnStateChanged(object sender, EventArgs args)
{
if (WindowState == WindowState.Minimized)
{
Hide();
if (m_notifyIcon != null)
m_notifyIcon.ShowBalloonTip(2000);
}
else
m_storedWindowState = WindowState;
} private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
{
CheckTrayIcon();
} private void m_notifyIcon_Click(object sender, EventArgs e)
{
Show();
WindowState = m_storedWindowState;
} private void CheckTrayIcon()
{
ShowTrayIcon(!IsVisible);
} private void ShowTrayIcon(bool show)
{
if (m_notifyIcon != null)
m_notifyIcon.Visible = show;
}
}
}
记得添加对System.Windows.Forms和System.Drawing的引用。
添加一个ico文件,并将其属性设置为Embeded Resource
http://download.csdn.net/source/1145933