NotifyIcon WPF

--------------
CHG   Merged partial classes of TaskbarIcon - no only 2 files.
ADD   Added attached events that indicate an opened popup/tooltip
ADD   Added storyboards in samples that make use of attached events.

git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@56 9f600761-6f11-4665-b6dc-0185e9171623
This commit is contained in:
Philipp Sumi
2009-04-01 07:34:50 +00:00
parent 354ba1ca43
commit 1242596214
8 changed files with 682 additions and 618 deletions

View File

@@ -58,7 +58,6 @@
<Compile Include="Interop\WindowClass.cs" />
<Compile Include="PopupActivationMode.cs" />
<Compile Include="RoutedEventHelper.cs" />
<Compile Include="TaskbarIcon.Interop.cs" />
<Compile Include="Interop\WinApi.cs" />
<Compile Include="Interop\MouseEvent.cs" />
<Compile Include="Interop\NotifyCommand.cs" />

View File

@@ -25,14 +25,98 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// in order to display either <see cref="TaskbarIconToolTip"/>
/// or <see cref="ToolTipText"/>.
/// </summary>
internal ToolTip CustomToolTip { get; private set; }
internal ToolTip CustomToolTip
{
get { return IconToolTipResolved; }
private set { SetIconToolTipResolved(value); }
}
/// <summary>
/// A <see cref="Popup"/> which is either the
/// <see cref="TaskbarIconPopup"/> control itself or a
/// <see cref="Popup"/> that wraps it.
/// </summary>
internal Popup CustomPopup { get; private set; }
internal Popup CustomPopup
{
get { return IconPopupResolved; }
private set { SetIconPopupResolved(value); }
}
//RESOLVED POPUPS CONTROLS
#region IconPopupResolved
/// <summary>
/// IconPopupResolved Read-Only Dependency Property
/// </summary>
private static readonly DependencyPropertyKey IconPopupResolvedPropertyKey
= DependencyProperty.RegisterReadOnly("IconPopupResolved", typeof(Popup), typeof(TaskbarIcon),
new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty IconPopupResolvedProperty
= IconPopupResolvedPropertyKey.DependencyProperty;
/// <summary>
/// Gets the IconPopupResolved property. This dependency property
/// indicates ....
/// </summary>
[Category(CategoryName)]
public Popup IconPopupResolved
{
get { return (Popup)GetValue(IconPopupResolvedProperty); }
}
/// <summary>
/// Provides a secure method for setting the IconPopupResolved property.
/// This dependency property indicates ....
/// </summary>
/// <param name="value">The new value for the property.</param>
protected void SetIconPopupResolved(Popup value)
{
SetValue(IconPopupResolvedPropertyKey, value);
}
#endregion
#region IconToolTipResolved
/// <summary>
/// IconToolTipResolved Read-Only Dependency Property
/// </summary>
private static readonly DependencyPropertyKey IconToolTipResolvedPropertyKey
= DependencyProperty.RegisterReadOnly("IconToolTipResolved", typeof(ToolTip), typeof(TaskbarIcon),
new FrameworkPropertyMetadata(null ));
public static readonly DependencyProperty IconToolTipResolvedProperty
= IconToolTipResolvedPropertyKey.DependencyProperty;
/// <summary>
/// Gets the IconToolTipResolved property. This dependency property
/// indicates ....
/// </summary>
[Category(CategoryName)]
[Browsable(true)]
[Bindable(true)]
public ToolTip IconToolTipResolved
{
get { return (ToolTip)GetValue(IconToolTipResolvedProperty); }
}
/// <summary>
/// Provides a secure method for setting the IconToolTipResolved property.
/// This dependency property indicates ....
/// </summary>
/// <param name="value">The new value for the property.</param>
protected void SetIconToolTipResolved(ToolTip value)
{
SetValue(IconToolTipResolvedPropertyKey, value);
}
#endregion
//DEPENDENCY PROPERTIES
@@ -292,7 +376,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <param name="e">Provides information about the updated property.</param>
private void OnTaskbarIconPopupPropertyChanged(DependencyPropertyChangedEventArgs e)
{
//currently not needed
//create a pop
CreatePopup();
}
@@ -453,38 +537,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region ContextMenu dependency property override
/// <summary>
/// A static callback listener which is being invoked if the
/// <see cref="FrameworkElement.ContextMenuProperty"/> dependency property has
/// been changed. Invokes the <see cref="OnContextMenuPropertyChanged"/>
/// instance method of the changed instance.
/// </summary>
/// <param name="d">The currently processed owner of the property.</param>
/// <param name="e">Provides information about the updated property.</param>
private static void ContextMenuPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TaskbarIcon owner = (TaskbarIcon) d;
owner.OnContextMenuPropertyChanged(e);
}
/// <summary>
/// Handles changes of the <see cref="FrameworkElement.ContextMenuProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="ContextMenu"/> property wrapper, updates of the property's value
/// should be handled here.
/// </summary
/// <param name="e">Provides information about the updated property.</param>
private void OnContextMenuPropertyChanged(DependencyPropertyChangedEventArgs e)
{
//currently not needed
}
#endregion
//EVENTS
@@ -1258,6 +1310,148 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
//ATTACHED EVENTS
#region PopupOpened
/// <summary>
/// PopupOpened Attached Routed Event
/// </summary>
public static readonly RoutedEvent PopupOpenedEvent = EventManager.RegisterRoutedEvent("PopupOpened",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Adds a handler for the PopupOpened attached event
/// </summary>
/// <param name="element">UIElement or ContentElement that listens to the event</param>
/// <param name="handler">Event handler to be added</param>
public static void AddPopupOpenedHandler(DependencyObject element, RoutedEventHandler handler)
{
RoutedEventHelper.AddHandler(element, PopupOpenedEvent, handler);
}
/// <summary>
/// Removes a handler for the PopupOpened attached event
/// </summary>
/// <param name="element">UIElement or ContentElement that listens to the event</param>
/// <param name="handler">Event handler to be removed</param>
public static void RemovePopupOpenedHandler(DependencyObject element, RoutedEventHandler handler)
{
RoutedEventHelper.RemoveHandler(element, PopupOpenedEvent, handler);
}
/// <summary>
/// A static helper method to raise the PopupOpened event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaisePopupOpenedEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = PopupOpenedEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region ToolTipOpened
/// <summary>
/// ToolTipOpened Attached Routed Event
/// </summary>
public static readonly RoutedEvent ToolTipOpenedEvent = EventManager.RegisterRoutedEvent("ToolTipOpened",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Adds a handler for the ToolTipOpened attached event
/// </summary>
/// <param name="element">UIElement or ContentElement that listens to the event</param>
/// <param name="handler">Event handler to be added</param>
public static void AddToolTipOpenedHandler(DependencyObject element, RoutedEventHandler handler)
{
RoutedEventHelper.AddHandler(element, ToolTipOpenedEvent, handler);
}
/// <summary>
/// Removes a handler for the ToolTipOpened attached event
/// </summary>
/// <param name="element">UIElement or ContentElement that listens to the event</param>
/// <param name="handler">Event handler to be removed</param>
public static void RemoveToolTipOpenedHandler(DependencyObject element, RoutedEventHandler handler)
{
RoutedEventHelper.RemoveHandler(element, ToolTipOpenedEvent, handler);
}
/// <summary>
/// A static helper method to raise the ToolTipOpened event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseToolTipOpenedEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = ToolTipOpenedEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region ToolTipClose
/// <summary>
/// ToolTipClose Attached Routed Event
/// </summary>
public static readonly RoutedEvent ToolTipCloseEvent = EventManager.RegisterRoutedEvent("ToolTipClose",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Adds a handler for the ToolTipClose attached event
/// </summary>
/// <param name="element">UIElement or ContentElement that listens to the event</param>
/// <param name="handler">Event handler to be added</param>
public static void AddToolTipCloseHandler(DependencyObject element, RoutedEventHandler handler)
{
RoutedEventHelper.AddHandler(element, ToolTipCloseEvent, handler);
}
/// <summary>
/// Removes a handler for the ToolTipClose attached event
/// </summary>
/// <param name="element">UIElement or ContentElement that listens to the event</param>
/// <param name="handler">Event handler to be removed</param>
public static void RemoveToolTipCloseHandler(DependencyObject element, RoutedEventHandler handler)
{
RoutedEventHelper.RemoveHandler(element, ToolTipCloseEvent, handler);
}
/// <summary>
/// A static helper method to raise the ToolTipClose event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseToolTipCloseEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = ToolTipCloseEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
//BASE CLASS PROPERTY OVERRIDES
@@ -1269,10 +1463,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
//register change listener for the Visibility property
PropertyMetadata md = new PropertyMetadata(Visibility.Visible, VisibilityPropertyChanged);
VisibilityProperty.OverrideMetadata(typeof(TaskbarIcon), md);
//register change listener for the ContextMenu property
md = new FrameworkPropertyMetadata(new PropertyChangedCallback(ContextMenuPropertyChanged));
ContextMenuProperty.OverrideMetadata(typeof (TaskbarIcon), md);
}
}
}

View File

@@ -1,360 +0,0 @@
using System;
using System.Drawing;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Hardcodet.Wpf.TaskbarNotification.Interop;
using Point=Hardcodet.Wpf.TaskbarNotification.Interop.Point;
namespace Hardcodet.Wpf.TaskbarNotification
{
partial class TaskbarIcon
{
/// <summary>
/// An action that is being invoked if the
/// <see cref="singleClickTimer"/> fires.
/// </summary>
private Action delayedTimerAction;
/// <summary>
/// A timer that is used to differentiate between single
/// and double clicks.
/// </summary>
private readonly Timer singleClickTimer;
#region ToolTip
/// <summary>
/// Displays a custom tooltip, if available. This method is only
/// invoked for Windows Vista and above.
/// </summary>
/// <param name="visible">Whether to show or hide the tooltip.</param>
private void OnToolTipChange(bool visible)
{
//if we don't have a tooltip, there's nothing to do here...
if (CustomToolTip == null) return;
if (visible)
{
if (ContextMenu != null && ContextMenu.IsOpen ||
CustomPopup != null && CustomPopup.IsOpen)
{
//ignore if we have an open context menu or popup
return;
}
var args = RaisePreviewTaskbarIconToolTipOpenEvent();
if (args.Handled) return;
CustomToolTip.IsOpen = true;
RaiseTaskbarIconToolTipOpenEvent();
}
else
{
var args = RaisePreviewTaskbarIconToolTipCloseEvent();
if (args.Handled) return;
CustomToolTip.IsOpen = false;
RaiseTaskbarIconToolTipCloseEvent();
}
}
/// <summary>
/// Creates a <see cref="ToolTip"/> control that either
/// wraps the currently set <see cref="TaskbarIconToolTip"/>
/// control or the <see cref="ToolTipText"/> string.<br/>
/// If <see cref="TaskbarIconToolTip"/> itself is already
/// a <see cref="ToolTip"/> instance, it will be used directly.
/// </summary>
/// <remarks>We use a <see cref="ToolTip"/> rather than
/// <see cref="Popup"/> because there was no way to prevent a
/// popup from causing cyclic open/close commands if it was
/// placed under the mouse. ToolTip internally uses a Popup of
/// its own, but takes advance of Popup's internal <see cref="Popup.HitTestable"/>
/// property which prevents this issue.</remarks>
private void CreateCustomToolTip()
{
//check if the item itself is a tooltip
ToolTip tt = TaskbarIconToolTip as ToolTip;
if (tt == null && TaskbarIconToolTip != null)
{
//create an invisible tooltip that hosts the UIElement
tt = new ToolTip();
tt.Placement = PlacementMode.Mouse;
tt.PlacementTarget = this;
//the tooltip (and implicitly its context) explicitly gets
//the DataContext of this instance. If there is no DataContext,
//the TaskbarIcon sets itself
tt.DataContext = DataContext ?? this;
//make sure the tooltip is invisible
tt.HasDropShadow = false;
tt.BorderThickness = new Thickness(0);
tt.Background = System.Windows.Media.Brushes.Transparent;
//setting the
tt.StaysOpen = true;
tt.Content = TaskbarIconToolTip;
}
else if (tt == null && !String.IsNullOrEmpty(ToolTipText))
{
//create a simple tooltip for the string
tt = new ToolTip();
tt.Content = ToolTipText;
}
//store a reference to the used tooltip
CustomToolTip = tt;
}
/// <summary>
/// Sets tooltip settings for the class depending on defined
/// dependency properties and OS support.
/// </summary>
private void WriteToolTipSettings()
{
const IconDataMembers flags = IconDataMembers.Tip;
iconData.ToolTipText = ToolTipText;
if (messageSink.Version == NotifyIconVersion.Vista)
{
//we need to set a tooltip text to get tooltip events from the
//taskbar icon
if (String.IsNullOrEmpty(iconData.ToolTipText) && CustomToolTip != null)
{
//if we have not tooltip text but a custom tooltip, we
//need to set a dummy value (we're displaying the ToolTip control, not the string)
iconData.ToolTipText = "ToolTip";
}
}
//update the tooltip text
Util.WriteIconData(ref iconData, NotifyCommand.Modify, flags);
}
#endregion
#region Show / Hide Balloon Tip
/// <summary>
/// Displays a balloon tip with the specified title,
/// text, and icon in the taskbar for the specified time period.
/// </summary>
/// <param name="title">The title to display on the balloon tip.</param>
/// <param name="message">The text to display on the balloon tip.</param>
/// <param name="symbol">A symbol that indicates the severity.</param>
public void ShowBalloonTip(string title, string message, BalloonIcon symbol)
{
lock (this)
{
ShowBalloonTip(title, message, symbol.GetBalloonFlag(), IntPtr.Zero);
}
}
/// <summary>
/// Displays a balloon tip with the specified title,
/// text, and a custom icon in the taskbar for the specified time period.
/// </summary>
/// <param name="title">The title to display on the balloon tip.</param>
/// <param name="message">The text to display on the balloon tip.</param>
/// <param name="customIcon">A custom icon.</param>
/// <exception cref="ArgumentNullException">If <paramref name="customIcon"/>
/// is a null reference.</exception>
public void ShowBalloonTip(string title, string message, Icon customIcon)
{
if (customIcon == null) throw new ArgumentNullException("customIcon");
lock (this)
{
ShowBalloonTip(title, message, BalloonFlags.User, customIcon.Handle);
}
}
/// <summary>
/// Invokes <see cref="WinApi.Shell_NotifyIcon"/> in order to display
/// a given balloon ToolTip.
/// </summary>
/// <param name="title">The title to display on the balloon tip.</param>
/// <param name="message">The text to display on the balloon tip.</param>
/// <param name="flags">Indicates what icon to use.</param>
/// <param name="balloonIconHandle">A handle to a custom icon, if any, or
/// <see cref="IntPtr.Zero"/>.</param>
private void ShowBalloonTip(string title, string message, BalloonFlags flags, IntPtr balloonIconHandle)
{
EnsureNotDisposed();
iconData.BalloonText = message;
iconData.BalloonTitle = title;
iconData.BalloonFlags = flags;
iconData.CustomBalloonIconHandle = balloonIconHandle;
Util.WriteIconData(ref iconData, NotifyCommand.Modify, IconDataMembers.Info);
}
/// <summary>
/// Hides a balloon ToolTip, if any is displayed.
/// </summary>
public void HideBalloonTip()
{
EnsureNotDisposed();
//reset balloon by just setting the info to an empty string
iconData.BalloonText = iconData.BalloonTitle = String.Empty;
Util.WriteIconData(ref iconData, NotifyCommand.Modify, IconDataMembers.Info);
}
#endregion
#region Single Click Timer event
/// <summary>
/// Performs a delayed action if the user requested an action
/// based on a single click of the left mouse.<br/>
/// This method is invoked by the <see cref="singleClickTimer"/>.
/// </summary>
private void DoSingleClickAction(object state)
{
if (IsDisposed) return;
Console.Out.WriteLine("TIMER EVENT");
//run action
Action action = delayedTimerAction;
if (action != null)
{
//cleanup action
delayedTimerAction = null;
//switch to UI thread
Application.Current.Dispatcher.Invoke(action);
}
}
#endregion
#region Create Popup
/// <summary>
/// Creates a <see cref="ToolTip"/> control that either
/// wraps the currently set <see cref="TaskbarIconToolTip"/>
/// control or the <see cref="ToolTipText"/> string.<br/>
/// If <see cref="TaskbarIconToolTip"/> itself is already
/// a <see cref="ToolTip"/> instance, it will be used directly.
/// </summary>
/// <remarks>We use a <see cref="ToolTip"/> rather than
/// <see cref="Popup"/> because there was no way to prevent a
/// popup from causing cyclic open/close commands if it was
/// placed under the mouse. ToolTip internally uses a Popup of
/// its own, but takes advance of Popup's internal <see cref="Popup.HitTestable"/>
/// property which prevents this issue.</remarks>
private void CreatePopup()
{
//no popup is available
if (TaskbarIconPopup == null) return;
//check if the item itself is a popup
Popup popup = TaskbarIconPopup as Popup;
if (popup == null)
{
//create an invisible popup that hosts the UIElement
popup = new Popup();
popup.AllowsTransparency = true;
popup.PopupAnimation = PopupAnimation.Fade;
//the tooltip (and implicitly its context) explicitly gets
//the DataContext of this instance. If there is no DataContext,
//the TaskbarIcon assigns itself
popup.DataContext = DataContext ?? this;
Popup.CreateRootPopup(popup, TaskbarIconPopup);
popup.PlacementTarget = this;
popup.Placement = PlacementMode.AbsolutePoint;
popup.StaysOpen = false;
}
//store a reference to the used tooltip
CustomPopup = popup;
}
#endregion
#region Show Tray Popup / Context Menu
/// <summary>
/// Displays the <see cref="TaskbarIconPopup"/> control if
/// it was set.
/// </summary>
private void ShowTrayPopup(Point cursorPosition)
{
if (IsDisposed) return;
//raise preview event no matter whether popup is currently set
//or not (enables client to set it on demand)
var args = RaisePreviewTaskbarIconPopupOpenEvent();
if (args.Handled) return;
if (TaskbarIconPopup != null)
{
//use absolute position, but place the popup centered above the icon
CustomPopup.Placement = PlacementMode.AbsolutePoint;
CustomPopup.HorizontalOffset = cursorPosition.X; //+ TaskbarIconPopup.ActualWidth/2;
CustomPopup.VerticalOffset = cursorPosition.Y;
//open popup
CustomPopup.IsOpen = true;
//activate the message window to track deactivation - otherwise, the context menu
//does not close if the user clicks somewhere else
WinApi.SetForegroundWindow(messageSink.MessageWindowHandle);
//bubble event
RaiseTaskbarIconPopupOpenEvent();
}
}
/// <summary>
/// Displays the <see cref="ContextMenu"/> if
/// it was set.
/// </summary>
private void ShowContextMenu(Point cursorPosition)
{
if (IsDisposed) return;
//raise preview event no matter whether context menu is currently set
//or not (enables client to set it on demand)
var args = RaisePreviewTaskbarIconContextMenuOpenEvent();
if (args.Handled) return;
if (ContextMenu != null)
{
//use absolute position
ContextMenu.Placement = PlacementMode.AbsolutePoint;
ContextMenu.HorizontalOffset = cursorPosition.X;
ContextMenu.VerticalOffset = cursorPosition.Y;
ContextMenu.IsOpen = true;
//activate the message window to track deactivation - otherwise, the context menu
//does not close if the user clicks somewhere else
WinApi.SetForegroundWindow(messageSink.MessageWindowHandle);
//bubble event
RaiseTaskbarIconContextMenuOpenEvent();
}
}
#endregion
}
}

View File

@@ -1,17 +1,14 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Drawing;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Hardcodet.Wpf.TaskbarNotification.Interop;
using Point=Hardcodet.Wpf.TaskbarNotification.Interop.Point;
namespace Hardcodet.Wpf.TaskbarNotification
{
/// <summary>
@@ -30,11 +27,22 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// </summary>
private readonly WindowMessageSink messageSink;
/// <summary>
/// An action that is being invoked if the
/// <see cref="singleClickTimer"/> fires.
/// </summary>
private Action delayedTimerAction;
/// <summary>
/// A timer that is used to differentiate between single
/// and double clicks.
/// </summary>
private readonly Timer singleClickTimer;
/// <summary>
/// Indicates whether the taskbar icon has been created or not.
/// </summary>
public bool IsTaskbarIconCreated { get; set; }
public bool IsTaskbarIconCreated { get; private set; }
/// <summary>
/// Indicates whether custom tooltips are supported, which depends
@@ -46,7 +54,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
get { return messageSink.Version == NotifyIconVersion.Vista; }
}
#region Construction
/// <summary>
@@ -55,16 +62,10 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// </summary>
public TaskbarIcon()
{
//do nothing if in design mode
if (Util.IsDesignMode)
{
messageSink = WindowMessageSink.CreateEmpty();
}
else
{
//create message sink that receives window messages
messageSink = new WindowMessageSink(NotifyIconVersion.Win95);
}
//using dummy sink in design mode
messageSink = Util.IsDesignMode
? WindowMessageSink.CreateEmpty()
: new WindowMessageSink(NotifyIconVersion.Win95);
//init icon data structure
iconData = NotifyIconData.CreateDefault(messageSink.MessageWindowHandle);
@@ -87,8 +88,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region Handle Mouse Events
#region Process Incoming Mouse Events
/// <summary>
/// Processes mouse events, which are bubbled
@@ -101,7 +101,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
{
if (IsDisposed) return;
switch(me)
switch (me)
{
case MouseEvent.MouseMove:
RaiseTaskbarIconMouseMoveEvent();
@@ -136,7 +136,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
break;
default:
throw new ArgumentOutOfRangeException("me", "Missing handler for mouse event flag: " + me);
}
@@ -178,6 +177,252 @@ namespace Hardcodet.Wpf.TaskbarNotification
}
}
#endregion
#region ToolTips
/// <summary>
/// Displays a custom tooltip, if available. This method is only
/// invoked for Windows Vista and above.
/// </summary>
/// <param name="visible">Whether to show or hide the tooltip.</param>
private void OnToolTipChange(bool visible)
{
//if we don't have a tooltip, there's nothing to do here...
if (CustomToolTip == null) return;
if (visible)
{
if (ContextMenu != null && ContextMenu.IsOpen ||
CustomPopup != null && CustomPopup.IsOpen)
{
//ignore if we have an open context menu or popup
return;
}
var args = RaisePreviewTaskbarIconToolTipOpenEvent();
if (args.Handled) return;
CustomToolTip.IsOpen = true;
//raise attached event first
if (TaskbarIconToolTip != null) RaiseToolTipOpenedEvent(TaskbarIconToolTip);
//bubble routed event
RaiseTaskbarIconToolTipOpenEvent();
}
else
{
var args = RaisePreviewTaskbarIconToolTipCloseEvent();
if (args.Handled) return;
//raise attached event first
if (TaskbarIconToolTip != null) RaiseToolTipCloseEvent(TaskbarIconToolTip);
//CustomToolTip.IsOpen = false;
RaiseTaskbarIconToolTipCloseEvent();
}
}
/// <summary>
/// Creates a <see cref="ToolTip"/> control that either
/// wraps the currently set <see cref="TaskbarIconToolTip"/>
/// control or the <see cref="ToolTipText"/> string.<br/>
/// If <see cref="TaskbarIconToolTip"/> itself is already
/// a <see cref="ToolTip"/> instance, it will be used directly.
/// </summary>
/// <remarks>We use a <see cref="ToolTip"/> rather than
/// <see cref="Popup"/> because there was no way to prevent a
/// popup from causing cyclic open/close commands if it was
/// placed under the mouse. ToolTip internally uses a Popup of
/// its own, but takes advance of Popup's internal <see cref="Popup.HitTestable"/>
/// property which prevents this issue.</remarks>
private void CreateCustomToolTip()
{
//check if the item itself is a tooltip
ToolTip tt = TaskbarIconToolTip as ToolTip;
if (tt == null && TaskbarIconToolTip != null)
{
//create an invisible tooltip that hosts the UIElement
tt = new ToolTip();
tt.Placement = PlacementMode.Mouse;
tt.PlacementTarget = this;
//the tooltip (and implicitly its context) explicitly gets
//the DataContext of this instance. If there is no DataContext,
//the TaskbarIcon sets itself
tt.DataContext = DataContext ?? this;
//make sure the tooltip is invisible
tt.HasDropShadow = false;
tt.BorderThickness = new Thickness(0);
tt.Background = System.Windows.Media.Brushes.Transparent;
//setting the
tt.StaysOpen = true;
tt.Content = TaskbarIconToolTip;
}
else if (tt == null && !String.IsNullOrEmpty(ToolTipText))
{
//create a simple tooltip for the string
tt = new ToolTip();
tt.Content = ToolTipText;
}
//store a reference to the used tooltip
CustomToolTip = tt;
}
/// <summary>
/// Sets tooltip settings for the class depending on defined
/// dependency properties and OS support.
/// </summary>
private void WriteToolTipSettings()
{
const IconDataMembers flags = IconDataMembers.Tip;
iconData.ToolTipText = ToolTipText;
if (messageSink.Version == NotifyIconVersion.Vista)
{
//we need to set a tooltip text to get tooltip events from the
//taskbar icon
if (String.IsNullOrEmpty(iconData.ToolTipText) && CustomToolTip != null)
{
//if we have not tooltip text but a custom tooltip, we
//need to set a dummy value (we're displaying the ToolTip control, not the string)
iconData.ToolTipText = "ToolTip";
}
}
//update the tooltip text
Util.WriteIconData(ref iconData, NotifyCommand.Modify, flags);
}
#endregion
#region Custom Popup
/// <summary>
/// Creates a <see cref="ToolTip"/> control that either
/// wraps the currently set <see cref="TaskbarIconToolTip"/>
/// control or the <see cref="ToolTipText"/> string.<br/>
/// If <see cref="TaskbarIconToolTip"/> itself is already
/// a <see cref="ToolTip"/> instance, it will be used directly.
/// </summary>
/// <remarks>We use a <see cref="ToolTip"/> rather than
/// <see cref="Popup"/> because there was no way to prevent a
/// popup from causing cyclic open/close commands if it was
/// placed under the mouse. ToolTip internally uses a Popup of
/// its own, but takes advance of Popup's internal <see cref="Popup.HitTestable"/>
/// property which prevents this issue.</remarks>
private void CreatePopup()
{
//no popup is available
if (TaskbarIconPopup == null) return;
//check if the item itself is a popup
Popup popup = TaskbarIconPopup as Popup;
if (popup == null)
{
//create an invisible popup that hosts the UIElement
popup = new Popup();
popup.AllowsTransparency = true;
popup.PopupAnimation = PopupAnimation.Fade;
//the tooltip (and implicitly its context) explicitly gets
//the DataContext of this instance. If there is no DataContext,
//the TaskbarIcon assigns itself
popup.DataContext = DataContext ?? this;
Popup.CreateRootPopup(popup, TaskbarIconPopup);
popup.PlacementTarget = this;
popup.Placement = PlacementMode.AbsolutePoint;
popup.StaysOpen = false;
}
//store a reference to the used tooltip
CustomPopup = popup;
}
/// <summary>
/// Displays the <see cref="TaskbarIconPopup"/> control if
/// it was set.
/// </summary>
private void ShowTrayPopup(Point cursorPosition)
{
if (IsDisposed) return;
//raise preview event no matter whether popup is currently set
//or not (enables client to set it on demand)
var args = RaisePreviewTaskbarIconPopupOpenEvent();
if (args.Handled) return;
if (TaskbarIconPopup != null)
{
//use absolute position, but place the popup centered above the icon
CustomPopup.Placement = PlacementMode.AbsolutePoint;
CustomPopup.HorizontalOffset = cursorPosition.X; //+ TaskbarIconPopup.ActualWidth/2;
CustomPopup.VerticalOffset = cursorPosition.Y;
//open popup
CustomPopup.IsOpen = true;
//activate the message window to track deactivation - otherwise, the context menu
//does not close if the user clicks somewhere else
WinApi.SetForegroundWindow(messageSink.MessageWindowHandle);
//raise attached event - item should never be null unless developers
//changed the CustomPopup directly...
if (TaskbarIconPopup != null) RaisePopupOpenedEvent(TaskbarIconPopup);
//bubble routed event
RaiseTaskbarIconPopupOpenEvent();
}
}
#endregion
#region Context Menu
/// <summary>
/// Displays the <see cref="ContextMenu"/> if
/// it was set.
/// </summary>
private void ShowContextMenu(Point cursorPosition)
{
if (IsDisposed) return;
//raise preview event no matter whether context menu is currently set
//or not (enables client to set it on demand)
var args = RaisePreviewTaskbarIconContextMenuOpenEvent();
if (args.Handled) return;
if (ContextMenu != null)
{
//use absolute position
ContextMenu.Placement = PlacementMode.AbsolutePoint;
ContextMenu.HorizontalOffset = cursorPosition.X;
ContextMenu.VerticalOffset = cursorPosition.Y;
ContextMenu.IsOpen = true;
//activate the message window to track deactivation - otherwise, the context menu
//does not close if the user clicks somewhere else
WinApi.SetForegroundWindow(messageSink.MessageWindowHandle);
//bubble event
RaiseTaskbarIconContextMenuOpenEvent();
}
}
#endregion
#region Balloon Tips
/// <summary>
/// Bubbles events if a balloon ToolTip was displayed
@@ -197,28 +442,122 @@ namespace Hardcodet.Wpf.TaskbarNotification
}
}
/// <summary>
/// Displays a balloon tip with the specified title,
/// text, and icon in the taskbar for the specified time period.
/// </summary>
/// <param name="title">The title to display on the balloon tip.</param>
/// <param name="message">The text to display on the balloon tip.</param>
/// <param name="symbol">A symbol that indicates the severity.</param>
public void ShowBalloonTip(string title, string message, BalloonIcon symbol)
{
lock (this)
{
ShowBalloonTip(title, message, symbol.GetBalloonFlag(), IntPtr.Zero);
}
}
/// <summary>
/// Displays a balloon tip with the specified title,
/// text, and a custom icon in the taskbar for the specified time period.
/// </summary>
/// <param name="title">The title to display on the balloon tip.</param>
/// <param name="message">The text to display on the balloon tip.</param>
/// <param name="customIcon">A custom icon.</param>
/// <exception cref="ArgumentNullException">If <paramref name="customIcon"/>
/// is a null reference.</exception>
public void ShowBalloonTip(string title, string message, Icon customIcon)
{
if (customIcon == null) throw new ArgumentNullException("customIcon");
lock (this)
{
ShowBalloonTip(title, message, BalloonFlags.User, customIcon.Handle);
}
}
/// <summary>
/// Invokes <see cref="WinApi.Shell_NotifyIcon"/> in order to display
/// a given balloon ToolTip.
/// </summary>
/// <param name="title">The title to display on the balloon tip.</param>
/// <param name="message">The text to display on the balloon tip.</param>
/// <param name="flags">Indicates what icon to use.</param>
/// <param name="balloonIconHandle">A handle to a custom icon, if any, or
/// <see cref="IntPtr.Zero"/>.</param>
private void ShowBalloonTip(string title, string message, BalloonFlags flags, IntPtr balloonIconHandle)
{
EnsureNotDisposed();
iconData.BalloonText = message;
iconData.BalloonTitle = title;
iconData.BalloonFlags = flags;
iconData.CustomBalloonIconHandle = balloonIconHandle;
Util.WriteIconData(ref iconData, NotifyCommand.Modify, IconDataMembers.Info);
}
/// <summary>
/// Hides a balloon ToolTip, if any is displayed.
/// </summary>
public void HideBalloonTip()
{
EnsureNotDisposed();
//reset balloon by just setting the info to an empty string
iconData.BalloonText = iconData.BalloonTitle = String.Empty;
Util.WriteIconData(ref iconData, NotifyCommand.Modify, IconDataMembers.Info);
}
#endregion
#region Single Click Timer event
#region SetVersion
/// <summary>
/// Performs a delayed action if the user requested an action
/// based on a single click of the left mouse.<br/>
/// This method is invoked by the <see cref="singleClickTimer"/>.
/// </summary>
private void DoSingleClickAction(object state)
{
if (IsDisposed) return;
//run action
Action action = delayedTimerAction;
if (action != null)
{
//cleanup action
delayedTimerAction = null;
//switch to UI thread
Application.Current.Dispatcher.Invoke(action);
}
}
#endregion
#region Set Version (API)
/// <summary>
/// Sets the version flag for the <see cref="iconData"/>.
/// </summary>
private void SetVersion()
{
iconData.VersionOrTimeout = (uint)NotifyIconVersion.Vista;
iconData.VersionOrTimeout = (uint) NotifyIconVersion.Vista;
bool status = WinApi.Shell_NotifyIcon(NotifyCommand.SetVersion, ref iconData);
if (!status)
{
iconData.VersionOrTimeout = (uint)NotifyIconVersion.Win2000;
iconData.VersionOrTimeout = (uint) NotifyIconVersion.Win2000;
status = Util.WriteIconData(ref iconData, NotifyCommand.SetVersion);
}
if (!status)
{
iconData.VersionOrTimeout = (uint)NotifyIconVersion.Win95;
iconData.VersionOrTimeout = (uint) NotifyIconVersion.Win95;
status = Util.WriteIconData(ref iconData, NotifyCommand.SetVersion);
}
@@ -230,7 +569,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region Create / Remove Taskbar Icon
/// <summary>
@@ -292,7 +630,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region Dispose / Exit
/// <summary>
@@ -311,7 +648,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
{
if (IsDisposed) throw new ObjectDisposedException(Name ?? GetType().FullName);
}
/// <summary>
/// Disposes the class if the application exits.
@@ -320,7 +657,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
{
Dispose();
}
/// <summary>
/// This destructor will run only if the <see cref="Dispose()"/>
@@ -372,7 +709,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <remarks>Check the <see cref="IsDisposed"/> property to determine whether
/// the method has already been called.</remarks>
private void Dispose(bool disposing)
{
{
//don't do anything if the component is already disposed
if (IsDisposed || !disposing) return;
@@ -389,10 +726,11 @@ namespace Hardcodet.Wpf.TaskbarNotification
//dispose message sink
messageSink.Dispose();
//remove icon
RemoveTaskbarIcon();
}
}
#endregion
}
}
}