12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using System;
- using System.Windows;
- using System.Windows.Forms;
- using Application = System.Windows.Application;
- using MessageBox = System.Windows.MessageBox;
- namespace athosRT
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- private NotifyIcon notifyIcon;
- public MainWindow()
- {
- InitializeComponent();
- DataContext = new MainWindowViewModel();
- this.ShowInTaskbar = false;
- this.WindowState = WindowState.Minimized;
- // 初始化 NotifyIcon
- var iconFromResources = Properties.Resources.RT2;
- notifyIcon = new NotifyIcon
- {
- Icon = iconFromResources, // 设置托盘图标文件路径
- Visible = true,
- Text = "JetEfem RT"
- };
- // 构建 ContextMenu 菜单项
- var contextMenu = new ContextMenuStrip();
- contextMenu.Items.Add("Show", null, (s, e) => ShowWindow());
- contextMenu.Items.Add("Exit", null, (s, e) => ExitApplication());
- notifyIcon.ContextMenuStrip = contextMenu; // 将菜单绑定至 NotifyIcon
- this.StateChanged += OnStateChanged; // 注册状态改变事件监听器
- }
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- MessageBoxResult result = MessageBox.Show("Are you sure quit the system ?", "JetEfem", MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.Cancel);
- System.Console.WriteLine(result);
- if (result == MessageBoxResult.Cancel)
- {
- e.Cancel = true;
- }
- }
- // 主界面完成关闭
- private void Window_Closed(object sender, System.EventArgs e)
- {
- Application.Current.Shutdown(0);
- Environment.Exit(0);
- }
- private void ShowWindow()
- {
- this.Show(); // 展示当前窗口
- this.WindowState = WindowState.Normal; // 恢复正常大小
- }
- private void ExitApplication()
- {
- notifyIcon.Visible = false; // 隐藏托盘图标以防资源泄漏
- notifyIcon.Dispose(); // 清理对象占用内存
- Application.Current.Shutdown(); // 关闭整个应用实例
- }
- private void OnStateChanged(object sender, EventArgs e)
- {
- if (this.WindowState == WindowState.Minimized && !IsMouseCaptured)
- {
- Hide(); // 当前窗口被最小化时自动隐藏它本身
- }
- }
- }
- }
|