WPF程式新增托盤圖示,選單的方法
前段時間做專案,客戶需要程式最小化時到托盤,並新增托盤選單,可以做退出,開啟燈操作。
首先需要定義一個類。托盤選單可用的。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Drawing = System.Drawing;
using Forms = System.Windows.Forms;
namespace 名稱空間
{
[ContentProperty("Text")]
[DefaultEvent("MouseDoubleClick")]
public class NotificationAreaIcon : FrameworkElement
{
Forms.NotifyIcon notifyIcon;
public static readonly RoutedEvent MouseClickEvent = EventManager.RegisterRoutedEvent(
"MouseClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotificationAreaIcon));
public static readonly RoutedEvent MouseDoubleClickEvent = EventManager.RegisterRoutedEvent(
"MouseDoubleClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotificationAreaIcon));
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon", typeof(ImageSource), typeof(NotificationAreaIcon));
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(NotificationAreaIcon));
public static readonly DependencyProperty FormsContextMenuProperty =
DependencyProperty.Register("MenuItems", typeof(List<Forms.MenuItem>), typeof(NotificationAreaIcon), new PropertyMetadata(new List<Forms.MenuItem>()));
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
// Create and initialize the window forms notify icon based
notifyIcon = new Forms.NotifyIcon();
notifyIcon.Text = Text;
if (!DesignerProperties.GetIsInDesignMode(this))
{
notifyIcon.Icon = FromImageSource(Icon);
}
notifyIcon.Visible = FromVisibility(Visibility);
if (this.MenuItems != null && this.MenuItems.Count > 0)
{
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(this.MenuItems.ToArray());
}
notifyIcon.MouseDown += OnMouseDown;
notifyIcon.MouseUp += OnMouseUp;
notifyIcon.MouseClick += OnMouseClick;
notifyIcon.MouseDoubleClick += OnMouseDoubleClick;
Dispatcher.ShutdownStarted += OnDispatcherShutdownStarted;
}
private void OnDispatcherShutdownStarted(object sender, EventArgs e)
{
notifyIcon.Dispose();
}
private void OnMouseDown(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseDownEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
}
private void OnMouseUp(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseUpEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
}
private void OnMouseDoubleClick(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseDoubleClickEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
}
private void OnMouseClick(object sender, Forms.MouseEventArgs e)
{
OnRaiseEvent(MouseClickEvent, new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
}
private void OnRaiseEvent(RoutedEvent handler, MouseButtonEventArgs e)
{
e.RoutedEvent = handler;
RaiseEvent(e);
}
public List<Forms.MenuItem> MenuItems
{
get { return (List<Forms.MenuItem>)GetValue(FormsContextMenuProperty); }
set { SetValue(FormsContextMenuProperty, value); }
}
public ImageSource Icon
{
get { return (ImageSource)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public event MouseButtonEventHandler MouseClick
{
add { AddHandler(MouseClickEvent, value); }
remove { RemoveHandler(MouseClickEvent, value); }
}
public event MouseButtonEventHandler MouseDoubleClick
{
add { AddHandler(MouseDoubleClickEvent, value); }
remove { RemoveHandler(MouseDoubleClickEvent, value); }
}
#region Conversion members
private static Drawing.Icon FromImageSource(ImageSource icon)
{
if (icon == null)
{
return null;
}
Uri iconUri = new Uri(icon.ToString());
return new Drawing.Icon(Application.GetResourceStream(iconUri).Stream);
}
private static bool FromVisibility(Visibility visibility)
{
return visibility == Visibility.Visible;
}
private MouseButton ToMouseButton(Forms.MouseButtons button)
{
switch (button)
{
case Forms.MouseButtons.Left:
return MouseButton.Left;
case Forms.MouseButtons.Right:
return MouseButton.Right;
case Forms.MouseButtons.Middle:
return MouseButton.Middle;
case Forms.MouseButtons.XButton1:
return MouseButton.XButton1;
case Forms.MouseButtons.XButton2:
return MouseButton.XButton2;
}
throw new InvalidOperationException();
}
#endregion Conversion members
}
}
然後在主程式前臺程式碼這裡引用
<Window x:Class="名稱空間.WndMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tp="clr-namespace:名稱空間"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
選單呼叫的地方
<!--下面部分為系統托盤選單-->
<tp:NotificationAreaIcon Text="測試托盤選單" Icon="/hysaleclient;component/Resources/hycf1.ico" MouseDoubleClick="NotificationAreaIcon_MouseDoubleClick">
<tp:NotificationAreaIcon.MenuItems>
<wf:MenuItem Text="關於軟體" Click="MenuItem_Click" ></wf:MenuItem >
<wf:MenuItem Text="開啟" DefaultItem="True" Click="MenuItem_Click_1"></wf:MenuItem>
<wf:MenuItem Text="退出" Click="MenuItem_Click_2" ></wf:MenuItem>
</tp:NotificationAreaIcon.MenuItems>
</tp:NotificationAreaIcon>
後臺程式碼部分
IsMinSized 是個布林變數,初始設定為false,最小化後設置為true,也可以不用,直接用windowstate來做判斷
/// <summary>
/// 滑鼠雙擊
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NotificationAreaIcon_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
OpenFromIcon();
}
}
/// <summary>
/// 點選圖示展開程式
/// </summary>
private void OpenFromIcon()
{
this.Visibility = Visibility.Visible;
this.ShowInTaskbar = true;
this.WindowState = WindowState.Normal;
}
/// <summary>
/// 關於軟體
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("開發單位:*************************\r\n軟體版本:1.0.10\r\n最後更新:2013-07-01", "關於軟體");
}
/// <summary>
/// 開啟程式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Click_1(object sender, EventArgs e)
{
if (IsMinSized)
{
OpenFromIcon();
}
}
/// <summary>
/// 退出程式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuItem_Click_2(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// 視窗尺寸變化事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_StateChanged(object sender, EventArgs e)
{
//如果最小化
if (this.WindowState == WindowState.Minimized)
{
IsMinSized = true;
this.ShowInTaskbar = false;
}
}