WPF NotifyIcon

--------------
FIX   Commands did not work with RoutedCommands which require an explicit target.
ADD   Added command target properties for both left and double click commands.
      Allows to explicitly define another control as the target of a routed
      command.

git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@112 9f600761-6f11-4665-b6dc-0185e9171623
This commit is contained in:
Philipp Sumi
2009-07-02 11:18:13 +00:00
parent d6fe6cbf76
commit f4a1888e80
7 changed files with 114 additions and 16 deletions

View File

@@ -3,6 +3,16 @@ Copyright (c) 2009 Philipp Sumi
Contact and Information: http://www.hardcodet.net
----------------------------------------------------------------------------
1.0.3 (2009.07.02)
*****
FIX Commands did not work with RoutedCommands which require an explicit target.
ADD Added command target properties for both left and double click commands.
Allows to explicitly define another control as the target of a routed
command.
----------------------------------------------------------------------------
1.0.2 (2009.05.18)

View File

@@ -53,5 +53,5 @@ using System.Windows.Markup;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyVersion("1.0.3.0")]
[assembly: AssemblyFileVersion("1.0.3.0")]

View File

@@ -712,6 +712,32 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region DoubleClickCommandTarget dependency property
/// <summary>
/// The target of the command that is fired if the notify icon is double clicked.
/// </summary>
public static readonly DependencyProperty DoubleClickCommandTargetProperty =
DependencyProperty.Register("DoubleClickCommandTarget",
typeof (IInputElement),
typeof (TaskbarIcon),
new FrameworkPropertyMetadata(null));
/// <summary>
/// A property wrapper for the <see cref="DoubleClickCommandTargetProperty"/>
/// dependency property:<br/>
/// The target of the command that is fired if the notify icon is double clicked.
/// </summary>
public IInputElement DoubleClickCommandTarget
{
get { return (IInputElement) GetValue(DoubleClickCommandTargetProperty); }
set { SetValue(DoubleClickCommandTargetProperty, value); }
}
#endregion
#region LeftClickCommand dependency property
/// <summary>
@@ -762,6 +788,31 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region LeftClickCommandTarget dependency property
/// <summary>
/// The target of the command that is fired if the notify icon is clicked.
/// </summary>
public static readonly DependencyProperty LeftClickCommandTargetProperty =
DependencyProperty.Register("LeftClickCommandTarget",
typeof (IInputElement),
typeof (TaskbarIcon),
new FrameworkPropertyMetadata(null));
/// <summary>
/// A property wrapper for the <see cref="LeftClickCommandTargetProperty"/>
/// dependency property:<br/>
/// The target of the command that is fired if the notify icon is clicked.
/// </summary>
public IInputElement LeftClickCommandTarget
{
get { return (IInputElement) GetValue(LeftClickCommandTargetProperty); }
set { SetValue(LeftClickCommandTargetProperty, value); }
}
#endregion
//EVENTS
@@ -1038,7 +1089,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
protected RoutedEventArgs RaiseTrayMouseDoubleClickEvent()
{
RoutedEventArgs args = RaiseTrayMouseDoubleClickEvent(this);
DoubleClickCommand.ExecuteIfEnabled(DoubleClickCommandParameter);
DoubleClickCommand.ExecuteIfEnabled(DoubleClickCommandParameter, DoubleClickCommandTarget ?? this);
return args;
}

View File

@@ -380,7 +380,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
//show popup once we are sure it's not a double click
delayedTimerAction = () =>
{
LeftClickCommand.ExecuteIfEnabled(LeftClickCommandParameter);
LeftClickCommand.ExecuteIfEnabled(LeftClickCommandParameter, LeftClickCommandTarget ?? this);
ShowTrayPopup(cursorPosition);
};
singleClickTimer.Change(WinApi.GetDoubleClickTime(), Timeout.Infinite);
@@ -402,7 +402,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
//show context menu once we are sure it's not a double click
delayedTimerAction = () =>
{
LeftClickCommand.ExecuteIfEnabled(LeftClickCommandParameter);
LeftClickCommand.ExecuteIfEnabled(LeftClickCommandParameter, LeftClickCommandTarget ?? this);
ShowContextMenu(cursorPosition);
};
singleClickTimer.Change(WinApi.GetDoubleClickTime(), Timeout.Infinite);
@@ -419,7 +419,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
if (me == MouseEvent.IconLeftMouseUp && !isLeftClickCommandInvoked)
{
//show context menu once we are sure it's not a double click
delayedTimerAction = () => LeftClickCommand.ExecuteIfEnabled(LeftClickCommandParameter);
delayedTimerAction = () => LeftClickCommand.ExecuteIfEnabled(LeftClickCommandParameter, LeftClickCommandTarget ?? this);
singleClickTimer.Change(WinApi.GetDoubleClickTime(), Timeout.Infinite);
}

View File

@@ -27,7 +27,6 @@ using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Resources;
@@ -263,10 +262,18 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <param name="command">The command to be executed, or a null reference.</param>
/// <param name="commandParameter">An optional parameter that is associated with
/// the command.</param>
public static void ExecuteIfEnabled(this ICommand command, object commandParameter)
/// <param name="target">The target element on which to raise the command.</param>
public static void ExecuteIfEnabled(this ICommand command, object commandParameter, IInputElement target)
{
if (command == null) return;
if (command.CanExecute(commandParameter))
RoutedCommand rc = command as RoutedCommand;
if (rc != null)
{
//routed commands work on a target
if (rc.CanExecute(commandParameter, target)) rc.Execute(commandParameter, target);
}
else if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}

View File

@@ -5,9 +5,12 @@
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:local="clr-namespace:Samples.Tutorials.Commands"
Height="300"
Width="300">
Width="300"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<!-- declare the command as a local resource -->
<Grid.Resources>
<local:ShowMessageCommand
@@ -16,15 +19,40 @@
<!-- declare the NotifyIcon and configure commands with parameters -->
<tb:TaskbarIcon
x:Name="MyNotifyIcon"
x:Name="CustomCommandNotifyIcon"
IconSource="/Icons/Error.ico"
LeftClickCommand="{StaticResource MessageCommand}"
LeftClickCommandParameter="Left mouse button was clicked"
DoubleClickCommand="{StaticResource MessageCommand}"
DoubleClickCommandParameter="Double click on NotifyIcon" />
<TextBlock Margin="26,26,24,0" VerticalAlignment="Top" FontWeight="Bold" TextWrapping="Wrap"><Run Language="de-ch" Text="Left / Double clicks on NotifyIcon executes commands."/></TextBlock>
<!-- declare the NotifyIcon and configure commands with targets -->
<tb:TaskbarIcon
x:Name="RoutedCommandNotifyIcon"
IconSource="/Icons/Inactive.ico"
LeftClickCommand="ApplicationCommands.Cut"
LeftClickCommandTarget="{Binding ElementName=txtInput}"
DoubleClickCommand="ApplicationCommands.Paste"
DoubleClickCommandTarget="{Binding ElementName=txtInput}" />
<TextBlock
Margin="26,26,24,0"
VerticalAlignment="Top"
FontWeight="Bold"
TextWrapping="Wrap"><Run
Language="de-ch"
Text="Left / Double clicks on red NotifyIcon executes simple custom commands." /></TextBlock>
<TextBlock
Margin="26,112,24,80"
FontWeight="Bold"
TextWrapping="Wrap"><Run Language="de-ch" Text="Left / Double clicks on grey NotifyIcon executes routed commands."/><LineBreak/><Run Language="de-ch" Text="Single click: Cuts selected text"/><LineBreak/><Run Language="de-ch" Text="Double click: Paste text from clipboard"/></TextBlock>
<TextBox
Margin="26,0,24,48"
VerticalAlignment="Bottom"
Height="22"
Text="hello world"
TextWrapping="Wrap"
x:Name="txtInput" />
</Grid>
</Window>

View File

@@ -23,10 +23,12 @@ namespace Samples.Tutorials.Commands
InitializeComponent();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
//clean up notifyicon (would otherwise stay open until application finishes)
MyNotifyIcon.Dispose();
CustomCommandNotifyIcon.Dispose();
RoutedCommandNotifyIcon.Dispose();
base.OnClosing(e);
}