在 `ChatDataModel.cs` 中添加 `Account` 类以存储临时账户信息。 修改 `TrayIconManager.cs` 的窗口关闭事件处理,移除通知显示并优化设置逻辑。 更新 `chatapi.cs` 中的服务器 URL 和 IP 地址。 调整 `LoginWindow.xaml` 的尺寸并添加 `Loaded` 事件处理,确保用户名和密码输入框正确绑定。 在 `LoginWindow.xaml.cs` 中实现读取和保存临时登录信息的功能。 修改 `MainWindow.xaml` 的选项卡定义,确保用户信息和设置选项卡正确显示。 更新 `MainWindow.xaml.cs` 中的登录和注册处理逻辑,确保正确处理服务器返回消息并清理资源。
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["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)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|