diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs index 6b8a5333..abf40c56 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/WindowAttach.cs @@ -109,6 +109,42 @@ namespace HandyControl.Controls => element.SetValue(ShowInTaskManagerProperty, ValueBoxes.BooleanBox(value)); public static bool GetShowInTaskManager(DependencyObject element) - => (bool) element.GetValue(ShowInTaskManagerProperty); + => (bool)element.GetValue(ShowInTaskManagerProperty); + + /// + /// 隐藏窗口而不是关闭,在使用 时很有用,这时点击主窗口的关闭按钮通常是隐藏窗口而不是退出程序 + /// + public static readonly DependencyProperty HiddenInsteadCloseProperty = + DependencyProperty.RegisterAttached("HiddenInsteadClose", typeof(bool), typeof(WindowAttach), new PropertyMetadata(false, HiddenInsteadClosePropertyChanged)); + + public static bool GetHiddenInsteadClose(DependencyObject obj) + { + return (bool)obj.GetValue(HiddenInsteadCloseProperty); + } + + public static void SetHiddenInsteadClose(DependencyObject obj, bool value) + { + obj.SetValue(HiddenInsteadCloseProperty, value); + } + + private static void HiddenInsteadClosePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is System.Windows.Window window && e.NewValue is bool enabled) + { + if (enabled) + window.Closing += Window_Closing; + else + window.Closing -= Window_Closing; + } + } + + private static void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + if (sender is System.Windows.Window window) + { + window.Hide(); + e.Cancel = true; + } + } } } \ No newline at end of file diff --git a/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs b/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs index 8b55c343..338babd8 100644 --- a/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs +++ b/src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs @@ -387,6 +387,38 @@ namespace HandyControl.Controls } } + public static readonly DependencyProperty ShowMainWindowOnClickProperty = + DependencyProperty.RegisterAttached("ShowMainWindowOnClick", typeof(bool), typeof(NotifyIcon), + new PropertyMetadata(false, OnShowMainWindowOnClickChanged)); + + private static void OnShowMainWindowOnClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is NotifyIcon notifyIcon && e.NewValue is bool enabled) + { + if (enabled) + notifyIcon.Click += NotifyIcon_Click; + else + notifyIcon.Click -= NotifyIcon_Click; + } + } + + private static void NotifyIcon_Click(object sender, RoutedEventArgs e) + { + if (sender is NotifyIcon) + { + Application.Current.MainWindow.Show(); + Application.Current.MainWindow.Activate(); + } + } + + /// + /// 设置或获取点击托盘图标时是否显示主窗口 + /// + public bool ShowMainWindowOnClick { + get => (bool)GetValue(ShowMainWindowOnClickProperty); + set => SetValue(ShowMainWindowOnClickProperty, value); + } + //referenced from http://www.cnblogs.com/sczmzx/p/5158127.html private IntPtr FindTrayToolbarWindow() {