Add attached property "WindowAttach.HiddenInsteadClose".

Add property "ShowMainWindowOnClick" for "NotifyIcon".
This commit is contained in:
GF-Huang committed 2020-11-27 19:20:01 +08:00
1 parent b4d26dd2fa
commit 7b05651cc0
2 files changed
+69 -1

No files matched your search

@@ -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);
/// <summary>
/// 隐藏窗口而不是关闭,在使用 <see cref="NotifyIcon"/> 时很有用,这时点击主窗口的关闭按钮通常是隐藏窗口而不是退出程序
/// </summary>
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;
}
}
}
}
@@ -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();
}
}
/// <summary>
/// 设置或获取点击托盘图标时是否显示主窗口
/// </summary>
public bool ShowMainWindowOnClick {
get => (bool)GetValue(ShowMainWindowOnClickProperty);
set => SetValue(ShowMainWindowOnClickProperty, value);
}
//referenced from http://www.cnblogs.com/sczmzx/p/5158127.html
private IntPtr FindTrayToolbarWindow()
{