在 `chatclient.sln` 中添加 `chatserver` 项目,配置调试和发布设置。更新 `App.config` 中 `log4net` 的配置路径。修改 `TrayIconManager.cs` 中的分隔符样式引用。更新 `chatapi.cs` 中的服务器地址和相关数据结构,增加 `userid` 和 `token` 字段。优化 `LoginWindow.xaml` 和 `MainWindow.xaml.cs` 的布局和逻辑,确保用户 ID 正确处理。更新 `log4net.config` 日志格式,添加控制台输出。配置 `chatserver.csproj` 的依赖项,添加服务器基本逻辑和消息类型枚举。更新 `launchSettings.json` 启动配置。
124 lines
4.1 KiB
C#
124 lines
4.1 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();
|
|
}
|
|
|
|
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["Separator"] });
|
|
//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)
|
|
{
|
|
RestoreApplication();
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
|
if (mainWindow != null)
|
|
{
|
|
mainWindow.TabControl.SelectedItem = mainWindow.Settings;
|
|
}
|
|
});
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|