modified the branch

This commit is contained in:
NaBian committed 2019-09-10 23:41:10 +08:00
1 parent 12bdc1a727
commit a91d33b078
8 files changed
+201 -91

No files matched your search

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
@@ -23,8 +25,6 @@ namespace HandyControl.Controls
private readonly int _id;
private static int NextId;
private ImageSource _icon;
private IntPtr _iconCurrentHandle;
@@ -55,6 +55,10 @@ namespace HandyControl.Controls
private bool _isDisposed;
private static int NextId;
private static readonly Dictionary<string, NotifyIcon> NotifyIconDic = new Dictionary<string, NotifyIcon>();
static NotifyIcon()
{
VisibilityProperty.OverrideMetadata(typeof(NotifyIcon), new PropertyMetadata(Visibility.Visible, OnVisibilityChanged));
@@ -110,6 +114,127 @@ namespace HandyControl.Controls
_dispatcherTimerPos.Tick += DispatcherTimerPos_Tick;
}
public static void Register(string token, NotifyIcon notifyIcon)
{
if (string.IsNullOrEmpty(token) || notifyIcon == null) return;
NotifyIconDic[token] = notifyIcon;
}
public static void Unregister(string token, NotifyIcon notifyIcon)
{
if (string.IsNullOrEmpty(token) || notifyIcon == null) return;
if (NotifyIconDic.ContainsKey(token))
{
if (ReferenceEquals(NotifyIconDic[token], notifyIcon))
{
NotifyIconDic.Remove(token);
}
}
}
public static void Unregister(NotifyIcon notifyIcon)
{
if (notifyIcon == null) return;
var first = NotifyIconDic.FirstOrDefault(item => ReferenceEquals(notifyIcon, item.Value));
if (!string.IsNullOrEmpty(first.Key))
{
NotifyIconDic.Remove(first.Key);
}
}
public static void Unregister(string token)
{
if (string.IsNullOrEmpty(token)) return;
if (NotifyIconDic.ContainsKey(token))
{
NotifyIconDic.Remove(token);
}
}
public static void ShowBalloonTip(string title, string content, NotifyIconInfoType infoType, string token)
{
if (NotifyIconDic.TryGetValue(token, out var notifyIcon))
{
notifyIcon.ShowBalloonTip(title, content, infoType);
}
}
public void ShowBalloonTip(string title, string content, NotifyIconInfoType infoType)
{
if (!_added || DesignerHelper.IsInDesignMode) return;
var data = new NOTIFYICONDATA
{
uFlags = NativeMethods.NIF_INFO,
hWnd = _messageWindowHandle,
uID = _id,
szInfoTitle = title ?? string.Empty,
szInfo = content ?? string.Empty
};
switch (infoType)
{
case NotifyIconInfoType.Info:
data.dwInfoFlags = NativeMethods.NIIF_INFO;
break;
case NotifyIconInfoType.Warning:
data.dwInfoFlags = NativeMethods.NIIF_WARNING;
break;
case NotifyIconInfoType.Error:
data.dwInfoFlags = NativeMethods.NIIF_ERROR;
break;
case NotifyIconInfoType.None:
data.dwInfoFlags = NativeMethods.NIIF_NONE;
break;
}
UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_MODIFY, data);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void CloseContextControl()
{
if (_contextContent != null)
{
_contextContent.IsOpen = false;
}
else if (ContextMenu != null)
{
ContextMenu.IsOpen = false;
}
}
public static readonly DependencyProperty TokenProperty = DependencyProperty.Register(
"Token", typeof(string), typeof(NotifyIcon), new PropertyMetadata(default(string), OnTokenChanged));
private static void OnTokenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is NotifyIcon notifyIcon)
{
if (e.NewValue == null)
{
Unregister(notifyIcon);
}
else
{
Register(e.NewValue.ToString(), notifyIcon);
}
}
}
public string Token
{
get => (string) GetValue(TokenProperty);
set => SetValue(TokenProperty, value);
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(NotifyIcon), new PropertyMetadata(default(string)));
@@ -165,38 +290,6 @@ namespace HandyControl.Controls
public static readonly DependencyProperty IsBlinkProperty = DependencyProperty.Register(
"IsBlink", typeof(bool), typeof(NotifyIcon), new PropertyMetadata(ValueBoxes.FalseBox, OnIsBlinkChanged));
public void ShowBalloonTip(string tipTitle, string tipText, ToolTipIcon tipIcon)
{
if (DesignerHelper.IsInDesignMode) return;
var data = new NOTIFYICONDATA
{
hWnd = _messageWindowHandle,
uID = _id,
uFlags = NativeMethods.NIF_INFO,
szInfoTitle = tipTitle ?? string.Empty,
szInfo = tipText ?? string.Empty,
};
switch (tipIcon)
{
case ToolTipIcon.Info:
data.dwInfoFlags = NativeMethods.NIIF_INFO;
break;
case ToolTipIcon.Warning:
data.dwInfoFlags = NativeMethods.NIIF_WARNING;
break;
case ToolTipIcon.Error:
data.dwInfoFlags = NativeMethods.NIIF_ERROR;
break;
case ToolTipIcon.None:
data.dwInfoFlags = NativeMethods.NIIF_NONE;
break;
}
UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_MODIFY, data);
}
private static void OnIsBlinkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (NotifyIcon)d;
@@ -221,6 +314,12 @@ namespace HandyControl.Controls
}
}
public bool IsBlink
{
get => (bool)GetValue(IsBlinkProperty);
set => SetValue(IsBlinkProperty, value);
}
private void DispatcherTimerBlinkTick(object sender, EventArgs e)
{
if (Visibility != Visibility.Visible || _iconCurrentHandle == IntPtr.Zero) return;
@@ -372,12 +471,6 @@ namespace HandyControl.Controls
return isFind;
}
public bool IsBlink
{
get => (bool) GetValue(IsBlinkProperty);
set => SetValue(IsBlinkProperty, value);
}
private void OnIconChanged()
{
if (_icon != null)
@@ -584,23 +677,5 @@ namespace HandyControl.Controls
_isDisposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void CloseContextControl()
{
if (_contextContent != null)
{
_contextContent.IsOpen = false;
}
else if (ContextMenu != null)
{
ContextMenu.IsOpen = false;
}
}
}
}