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

@@ -198,5 +198,9 @@
public static readonly string BlogsView = nameof(BlogsView);
public static readonly string NotifyIconDemo = nameof(NotifyIconDemo);
public static readonly string NotifyIconContextDemo = nameof(NotifyIconContextDemo);
}
}
@@ -110,7 +110,9 @@ var messageList = new List<string>
"MainWindow",
"OverView",
"ProjectsView",
"BlogsView"
"BlogsView",
"NotifyIconDemo",
"NotifyIconContextDemo"
};
#>
namespace HandyControlDemo.Data
@@ -5,35 +5,42 @@
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
xmlns:data="clr-namespace:HandyControlDemo.Data"
DataContext="{Binding NotifyIconDemo,Source={StaticResource Locator}}"
Background="{DynamicResource RegionBrush}">
<hc:TransitioningContentControl>
<hc:SimplePanel Margin="32">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<GroupBox Header="ContextMenu" Padding="10">
<StackPanel>
<GroupBox hc:TitleElement.TitleAlignment="Left" Style="{StaticResource GroupBoxOriginal}" Header="{x:Static langs:Lang.Visibility}">
<ToggleButton IsChecked="{Binding ContextMenuIsShow}" Style="{StaticResource ToggleButtonSwitch}"/>
</GroupBox>
<TextBlock Foreground="{DynamicResource InfoBrush}" Visibility="{Binding ContextMenuIsShow,Converter={StaticResource Boolean2VisibilityConverter}}" Text="{x:Static langs:Lang.Try2CloseApp}" Margin="0,6,0,0"/>
<GroupBox Margin="0,10,0,0" hc:TitleElement.TitleAlignment="Left" Style="{StaticResource GroupBoxOriginal}" Header="{x:Static langs:Lang.Blink}">
<ToggleButton IsChecked="{Binding ContextMenuIsBlink}" IsEnabled="{Binding ContextMenuIsShow}" Style="{StaticResource ToggleButtonSwitch}"/>
</GroupBox>
</StackPanel>
</GroupBox>
<GroupBox Header="ContextContent" Margin="16,0,0,0" Padding="10">
<StackPanel>
<GroupBox hc:TitleElement.TitleAlignment="Left" Style="{StaticResource GroupBoxOriginal}" Header="{x:Static langs:Lang.Visibility}">
<ToggleButton IsChecked="{Binding ContextContentIsShow}" Style="{StaticResource ToggleButtonSwitch}"/>
</GroupBox>
<TextBlock Foreground="{DynamicResource InfoBrush}" Visibility="{Binding ContextContentIsShow,Converter={StaticResource Boolean2VisibilityConverter}}" Text="{x:Static langs:Lang.Try2CloseApp}" Margin="0,6,0,0"/>
<GroupBox Margin="0,10,0,0" hc:TitleElement.TitleAlignment="Left" Style="{StaticResource GroupBoxOriginal}" Header="{x:Static langs:Lang.Blink}">
<ToggleButton IsChecked="{Binding ContextContentIsBlink}" IsEnabled="{Binding ContextContentIsShow}" Style="{StaticResource ToggleButtonSwitch}"/>
</GroupBox>
</StackPanel>
</GroupBox>
<hc:SimplePanel Margin="32" VerticalAlignment="Center">
<StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<GroupBox Header="ContextMenu" Padding="10">
<StackPanel>
<GroupBox hc:TitleElement.TitleAlignment="Left" Style="{StaticResource GroupBoxOriginal}" Header="{x:Static langs:Lang.Visibility}">
<ToggleButton IsChecked="{Binding ContextMenuIsShow}" Style="{StaticResource ToggleButtonSwitch}"/>
</GroupBox>
<TextBlock Foreground="{DynamicResource InfoBrush}" Visibility="{Binding ContextMenuIsShow,Converter={StaticResource Boolean2VisibilityConverter}}" Text="{x:Static langs:Lang.Try2CloseApp}" Margin="0,6,0,0"/>
<GroupBox Margin="0,10,0,0" hc:TitleElement.TitleAlignment="Left" Style="{StaticResource GroupBoxOriginal}" Header="{x:Static langs:Lang.Blink}">
<ToggleButton IsChecked="{Binding ContextMenuIsBlink}" IsEnabled="{Binding ContextMenuIsShow}" Style="{StaticResource ToggleButtonSwitch}"/>
</GroupBox>
</StackPanel>
</GroupBox>
<GroupBox Header="ContextContent" Margin="16,0,0,0" Padding="10">
<StackPanel>
<GroupBox hc:TitleElement.TitleAlignment="Left" Style="{StaticResource GroupBoxOriginal}" Header="{x:Static langs:Lang.Visibility}">
<ToggleButton IsChecked="{Binding ContextContentIsShow}" Style="{StaticResource ToggleButtonSwitch}"/>
</GroupBox>
<TextBlock Foreground="{DynamicResource InfoBrush}" Visibility="{Binding ContextContentIsShow,Converter={StaticResource Boolean2VisibilityConverter}}" Text="{x:Static langs:Lang.Try2CloseApp}" Margin="0,6,0,0"/>
<GroupBox Margin="0,10,0,0" hc:TitleElement.TitleAlignment="Left" Style="{StaticResource GroupBoxOriginal}" Header="{x:Static langs:Lang.Blink}">
<ToggleButton IsChecked="{Binding ContextContentIsBlink}" IsEnabled="{Binding ContextContentIsShow}" Style="{StaticResource ToggleButtonSwitch}"/>
</GroupBox>
</StackPanel>
</GroupBox>
</StackPanel>
<DockPanel Margin="0,16,0,0" LastChildFill="True">
<Button Command="{Binding SendNotificationCmd}" Content="{x:Static langs:Lang.SendNotification}" DockPanel.Dock="Right" HorizontalAlignment="Stretch" Margin="10,0,0,0"/>
<TextBox Text="{Binding Content}"/>
</DockPanel>
</StackPanel>
<hc:NotifyIcon Text="HandyControl" IsBlink="{Binding ContextMenuIsBlink}" Visibility="{Binding ContextMenuIsShow,Converter={StaticResource Boolean2VisibilityConverter}}">
<hc:NotifyIcon Token="{x:Static data:MessageToken.NotifyIconDemo}" Text="HandyControl" IsBlink="{Binding ContextMenuIsBlink}" Visibility="{Binding ContextMenuIsShow,Converter={StaticResource Boolean2VisibilityConverter}}">
<hc:NotifyIcon.ContextMenu>
<ContextMenu>
<MenuItem Command="hc:ControlCommands.PushMainWindow2Top" Header="{x:Static langs:Lang.OpenPanel}"/>
@@ -49,7 +56,7 @@
</i:EventTrigger>
</i:Interaction.Triggers>
</hc:NotifyIcon>
<hc:NotifyIcon x:Name="NotifyIconContextContent" Text="HandyControl" IsBlink="{Binding ContextContentIsBlink}" Visibility="{Binding ContextContentIsShow,Converter={StaticResource Boolean2VisibilityConverter}}" Icon="/HandyControlDemo;component/Resources/Img/icon-white.ico">
<hc:NotifyIcon Token="{x:Static data:MessageToken.NotifyIconContextDemo}" x:Name="NotifyIconContextContent" Text="HandyControl" IsBlink="{Binding ContextContentIsBlink}" Visibility="{Binding ContextContentIsShow,Converter={StaticResource Boolean2VisibilityConverter}}" Icon="/HandyControlDemo;component/Resources/Img/icon-white.ico">
<hc:NotifyIcon.ContextContent>
<Border CornerRadius="4" Margin="10" Background="White" Effect="{StaticResource EffectShadow2}">
<StackPanel VerticalAlignment="Center" Margin="16">
@@ -6,6 +6,7 @@ using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.CommandWpf;
# endif
using HandyControl.Controls;
using HandyControl.Data;
using HandyControlDemo.Data;
namespace HandyControlDemo.ViewModel
@@ -16,6 +17,18 @@ namespace HandyControlDemo.ViewModel
private bool _reversed;
private string _content = "Hello~~~";
public string Content
{
get => _content;
#if netle40
set => Set(nameof(Content), ref _content, value);
#else
set => Set(ref _content, value);
#endif
}
private bool _contextMenuIsShow;
public bool ContextMenuIsShow
@@ -87,6 +100,14 @@ namespace HandyControlDemo.ViewModel
public RelayCommand<object> MouseCmd => new Lazy<RelayCommand<object>>(() =>
new RelayCommand<object>(str=> Growl.Info(str.ToString()))).Value;
public RelayCommand SendNotificationCmd => new Lazy<RelayCommand>(() =>
new RelayCommand(SendNotification)).Value;
private void SendNotification()
{
NotifyIcon.ShowBalloonTip("HandyControl", Content, NotifyIconInfoType.None, ContextMenuIsShow ? MessageToken.NotifyIconDemo : MessageToken.NotifyIconContextDemo);
}
public override void Cleanup()
{
base.Cleanup();
@@ -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;
}
}
}
}
@@ -1,25 +1,25 @@
namespace HandyControl.Data
{
public enum ToolTipIcon
public enum NotifyIconInfoType
{
/// <summary>
/// No Icon.
/// </summary>
None = 0,
None,
/// <summary>
/// A Information Icon.
/// </summary>
Info = 1,
Info,
/// <summary>
/// A Warning Icon.
/// </summary>
Warning = 2,
Warning,
/// <summary>
/// A Error Icon.
/// </summary>
Error = 3
Error
}
}
@@ -35,7 +35,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Slider\RangeSlider\TwoWayRangeBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Block\RunningBlock.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\ShowAnimation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\ToolTipIcon.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\NotifyIconInfoType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Commands\CloseWindowCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Commands\OpenLinkCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Converter\BorderCircularClipConverter.cs" />
@@ -32,6 +32,7 @@ namespace HandyControl.Tools.Interop
NIIF_INFO = 0x00000001,
NIIF_WARNING = 0x00000002,
NIIF_ERROR = 0x00000003,
NIIF_LARGE_ICON = 0x00000020,
WM_KEYDOWN = 0x0100,
WM_KEYUP = 0x0101,
WM_SYSKEYDOWN = 0x0104,