123 lines
4.0 KiB
C#
123 lines
4.0 KiB
C#
|
using System.Windows;
|
|||
|
using Hardcodet.Wpf.TaskbarNotification;
|
|||
|
using System.Windows.Controls;
|
|||
|
using MaterialDesignThemes.Wpf;
|
|||
|
using System.Windows.Input;
|
|||
|
using System.Drawing;
|
|||
|
using System.Diagnostics;
|
|||
|
|
|||
|
namespace chatclient.Data
|
|||
|
{
|
|||
|
public class TrayIconManager
|
|||
|
{
|
|||
|
private readonly TaskbarIcon _trayIcon;
|
|||
|
private readonly Window _mainWindow;
|
|||
|
|
|||
|
public TrayIconManager(Window mainWindow)
|
|||
|
{
|
|||
|
_mainWindow = mainWindow;
|
|||
|
|
|||
|
// 创建托盘图标
|
|||
|
_trayIcon = new TaskbarIcon
|
|||
|
{
|
|||
|
Icon = new System.Drawing.Icon("resource/chat.ico"),
|
|||
|
ToolTipText = "Material Design Application",
|
|||
|
ContextMenu = CreateContextMenu()
|
|||
|
};
|
|||
|
|
|||
|
// 注册事件
|
|||
|
_trayIcon.TrayMouseDoubleClick += TrayIcon_TrayMouseDoubleClick;
|
|||
|
_mainWindow.Closing += MainWindow_ClosingHandler;
|
|||
|
}
|
|||
|
|
|||
|
private void MainWindow_ClosingHandler(object? sender, System.ComponentModel.CancelEventArgs e)
|
|||
|
{
|
|||
|
// 取消关闭操作,改为最小化到托盘
|
|||
|
e.Cancel = true;
|
|||
|
_mainWindow.Hide();
|
|||
|
|
|||
|
// 显示通知
|
|||
|
//_trayIcon.ShowBalloonTip("Application minimized",
|
|||
|
// "The application is running in the system tray",
|
|||
|
// BalloonIcon.Info);
|
|||
|
}
|
|||
|
|
|||
|
private ContextMenu CreateContextMenu()
|
|||
|
{
|
|||
|
// 创建Material Design风格的上下文菜单
|
|||
|
var contextMenu = new ContextMenu
|
|||
|
{
|
|||
|
Style = (Style)Application.Current.Resources["MaterialTrayMenu"]
|
|||
|
};
|
|||
|
|
|||
|
// 添加菜单项
|
|||
|
contextMenu.Items.Add(CreateMenuItem("Open Application", PackIconKind.WindowRestore, OpenApp_Click));
|
|||
|
contextMenu.Items.Add(CreateMenuItem("Settings", PackIconKind.Cog, Settings_Click));
|
|||
|
contextMenu.Items.Add(new Separator { Style = (Style)Application.Current.Resources["MaterialDesignLightSeparator"] });
|
|||
|
contextMenu.Items.Add(CreateMenuItem("Check for Updates", PackIconKind.Update, Updates_Click));
|
|||
|
contextMenu.Items.Add(CreateMenuItem("Help", PackIconKind.HelpCircle, Help_Click));
|
|||
|
contextMenu.Items.Add(new Separator { Style = (Style)Application.Current.Resources["MaterialDesignLightSeparator"] });
|
|||
|
contextMenu.Items.Add(CreateMenuItem("Exit", PackIconKind.Power, Exit_Click));
|
|||
|
|
|||
|
return contextMenu;
|
|||
|
}
|
|||
|
|
|||
|
private MenuItem CreateMenuItem(string header, PackIconKind iconKind, RoutedEventHandler clickHandler)
|
|||
|
{
|
|||
|
var menuItem = new MenuItem
|
|||
|
{
|
|||
|
Header = header,
|
|||
|
Style = (Style)Application.Current.Resources["MaterialTrayMenuItem"],
|
|||
|
Icon = new PackIcon { Kind = iconKind, Width = 20, Height = 20 }
|
|||
|
};
|
|||
|
|
|||
|
menuItem.Click += clickHandler;
|
|||
|
return menuItem;
|
|||
|
}
|
|||
|
|
|||
|
private void TrayIcon_TrayMouseDoubleClick(object sender, RoutedEventArgs e)
|
|||
|
{
|
|||
|
RestoreApplication();
|
|||
|
}
|
|||
|
|
|||
|
private void OpenApp_Click(object sender, RoutedEventArgs e)
|
|||
|
{
|
|||
|
RestoreApplication();
|
|||
|
}
|
|||
|
|
|||
|
private void RestoreApplication()
|
|||
|
{
|
|||
|
_mainWindow.Show();
|
|||
|
_mainWindow.WindowState = WindowState.Normal;
|
|||
|
_mainWindow.Activate();
|
|||
|
}
|
|||
|
|
|||
|
private void Settings_Click(object sender, RoutedEventArgs e)
|
|||
|
{
|
|||
|
// 实现设置逻辑
|
|||
|
}
|
|||
|
|
|||
|
private void Updates_Click(object sender, RoutedEventArgs e)
|
|||
|
{
|
|||
|
// 实现更新检查逻辑
|
|||
|
}
|
|||
|
|
|||
|
private void Help_Click(object sender, RoutedEventArgs e)
|
|||
|
{
|
|||
|
// 实现帮助逻辑
|
|||
|
}
|
|||
|
|
|||
|
private void Exit_Click(object sender, RoutedEventArgs e)
|
|||
|
{
|
|||
|
// 释放托盘图标资源
|
|||
|
Dispose();
|
|||
|
Application.Current.Shutdown();
|
|||
|
}
|
|||
|
|
|||
|
public void Dispose()
|
|||
|
{
|
|||
|
_trayIcon.Dispose();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|