//系统托盘制作(引用命名空间System.Windows.Forms.NotifyIcon)
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;
}
}
}
///窗体自动隐藏
private bool _IsHidded = false;
private const int BORDER = 3;
private const int AUTOHIDETIME = 50;
private Location location = Location.None;
private DispatcherTimer autoHideTimer = null;
private WindowState winState = WindowState.Normal;
public MainWindow()
{
InitializeComponent();
//自动隐藏窗体
this.autoHideTimer = new DispatcherTimer();
this.autoHideTimer.Interval = TimeSpan.FromMilliseconds(AUTOHIDETIME);
this.autoHideTimer.Tick += new EventHandler(AutoHideTimer_Tick);
}
/// <summary>
/// 创建一个计时器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AutoHideTimer_Tick(object sender, EventArgs e)
{
POINT p;
if (!GetCursorPos(out p))
{
return;
}
if (p.x >= this.Left && p.x <= (this.Left + this.ActualWidth)
&& p.y >= this.Top && p.y <= (this.Top + this.ActualHeight))
{
this.ShowWindow();
}
else
{
this.HideWindow();
}
}
/// <summary>
/// 当窗体位置改变时触发事件
/// </summary>
/// <param name="e"></param>
protected override void OnLocationChanged(EventArgs e)
{
if (!this._IsHidded)
{
if (this.Top <= 0 && this.Left <= 0)
{
this.location = Location.LeftTop;
this.HideWindow();
}
else if (this.Top <= 0 && this.Left >= SystemParameters.VirtualScreenWidth - this.ActualWidth)
{
this.location = Location.RightTop;
this.HideWindow();
}
else if (this.Top <= 0)
{
this.location = Location.Top;
this.HideWindow();
}
else
{
this.location = Location.None;
}
}
base.OnLocationChanged(e);
}
enum Location
{
None,
Top,
LeftTop,
RightTop
}
/// <summary>
/// 隐藏窗体
/// </summary>
private void ShowWindow()
{
if (this._IsHidded)
{
switch (this.location)
{
case Location.Top:
case Location.LeftTop:
case Location.RightTop:
this.Top = 0;
this.Topmost = false;
this._IsHidded = false;
this.UpdateLayout();
break;
case Location.None:
break;
}
}
}
/// <summary>
/// 显示窗体
/// </summary>
private void HideWindow()
{
if (!this._IsHidded)
{
switch (this.location)
{
case Location.Top:
this.Top = BORDER - this.ActualHeight;
this.Topmost = true;
this._IsHidded = true;
this.autoHideTimer.Start();
break;
case Location.LeftTop:
this.Left = 0;
this.Top = BORDER - this.ActualHeight;
this.Topmost = true;
this._IsHidded = true;
this.autoHideTimer.Start();
break;
case Location.RightTop:
this.Left = SystemParameters.VirtualScreenWidth - this.ActualWidth;
this.Top = BORDER - this.ActualHeight;
this.Topmost = true;
this._IsHidded = true;
this.autoHideTimer.Start();
break;
case Location.None:
break;
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(out POINT pt);