在 `ChatDataModel.cs` 中添加了控件引用并重命名消息类型属性。 在 `TrayIconManager.cs` 中将菜单项文本翻译为中文并注释掉部分代码。 调整 `LoginWindow.xaml` 的窗口尺寸。 在 `MainWindow.xaml` 中添加了 `x:Name` 属性和数据触发器,改善了用户体验。 在 `MainWindow.xaml.cs` 中添加了用户登录时的欢迎消息逻辑,并统一了消息类型属性名称。
122 lines
3.9 KiB
C#
122 lines
3.9 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("打开程序", PackIconKind.WindowRestore, OpenApp_Click));
|
|
contextMenu.Items.Add(CreateMenuItem("设置", 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("退出", 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();
|
|
}
|
|
}
|
|
}
|