WPF NotifyIcon

--------------
ADD   Completed events.
FIX   Suppressing second left mouse click event in case of a double click.
CHG   General cleanup.

git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@53 9f600761-6f11-4665-b6dc-0185e9171623
This commit is contained in:
Philipp Sumi
2009-03-30 23:00:01 +00:00
parent 94085b7293
commit 6cc31cea5b
6 changed files with 520 additions and 147 deletions

View File

@@ -35,13 +35,15 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// </summary>
private WindowProcedureHandler messageHandler;
/// <summary>
/// Creates the helper message window that is used
/// to receive messages from the taskbar icon.
/// </summary>
private void CreateMessageWindow()
{
WindowId = "WPFTaskbarIcon_" + Guid.NewGuid().ToString();
//generate a unique ID for the window
WindowId = "WPFTaskbarIcon_" + DateTime.Now.Ticks;
//register window message handler
messageHandler = OnWindowMessageReceived;
@@ -86,14 +88,13 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
if (messageId == taskbarRestartMessageId)
{
//recreate the icon if the taskbar was restarted
//TODO refresh icon
//recreate the icon if the taskbar was restarted (e.g. due to Win Explorer shutdown)
TaskbarCreated();
}
//forward message
ProcessWindowMessage(messageId, wparam, lparam);
//handle mouse clicks...
// Pass the message to the default window procedure
return WinApi.DefWindowProc(hwnd, messageId, wparam, lparam);
}

View File

@@ -24,6 +24,14 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// </summary>
public NotifyIconVersion Version { get; set; }
/// <summary>
/// Used to track whether a mouse-up event is just
/// the aftermath of a double-click and therefore needs
/// to be suppressed.
/// </summary>
private bool isDoubleClick;
#endregion
@@ -47,7 +55,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
public event Action<bool> BallonToolTipChanged;
/// <summary>
/// Fired if the taskbar was created. Requires the taskbar
/// Fired if the taskbar was created or restarted. Requires the taskbar
/// icon to be reset.
/// </summary>
public event Action TaskbarCreated;
@@ -119,11 +127,16 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
case 0x202:
Debug.WriteLine("left up");
MouseEventReceived(MouseEvent.IconLeftMouseUp);
if (!isDoubleClick)
{
MouseEventReceived(MouseEvent.IconLeftMouseUp);
}
isDoubleClick = false;
break;
case 0x203:
Debug.WriteLine("left click 2");
isDoubleClick = true;
MouseEventReceived(MouseEvent.IconDoubleClick);
break;

View File

@@ -74,7 +74,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region ToolTip dependency property override
/// <summary>
@@ -119,7 +118,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region Icon property / IconSource dependency property
private Icon icon;
@@ -194,7 +192,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region TaskbarIconPopup dependency property
/// <summary>
@@ -242,12 +239,11 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <param name="e">Provides information about the updated property.</param>
private void OnTaskbarIconPopupPropertyChanged(DependencyPropertyChangedEventArgs e)
{
Popup newValue = (Popup) e.NewValue;
//currently not needed
}
#endregion
#region MenuActivation dependency property
/// <summary>
@@ -297,19 +293,15 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <param name="e">Provides information about the updated property.</param>
private void OnMenuActivationPropertyChanged(DependencyPropertyChangedEventArgs e)
{
PopupActivationMode newValue = (PopupActivationMode) e.NewValue;
//TODO provide implementation
throw new NotImplementedException("Change event handler for dependency property MenuActivation not implemented.");
//currently not needed
}
#endregion
#region PopupActivation dependency property
/// <summary>
/// Defines what mouse events trigger the <see cref="IconPopup" />.
/// Defines what mouse events trigger the <see cref="TaskbarIconPopup" />.
/// Default is <see cref="PopupActivationMode.LeftClick" />.
/// </summary>
public static readonly DependencyProperty PopupActivationProperty =
@@ -321,7 +313,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <summary>
/// A property wrapper for the <see cref="PopupActivationProperty"/>
/// dependency property:<br/>
/// Defines what mouse events trigger the <see cref="IconPopup" />.
/// Defines what mouse events trigger the <see cref="TaskbarIconPopup" />.
/// Default is <see cref="PopupActivationMode.LeftClick" />.
/// </summary>
public PopupActivationMode PopupActivation
@@ -355,15 +347,11 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <param name="e">Provides information about the updated property.</param>
private void OnPopupActivationPropertyChanged(DependencyPropertyChangedEventArgs e)
{
PopupActivationMode newValue = (PopupActivationMode) e.NewValue;
//TODO provide implementation
throw new NotImplementedException("Change event handler for dependency property PopupActivation not implemented.");
//currently not needed
}
#endregion
#region Visibility dependency property override
/// <summary>
@@ -405,7 +393,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region ContextMenu dependency property override
/// <summary>
@@ -432,8 +419,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <param name="e">Provides information about the updated property.</param>
private void OnContextMenuPropertyChanged(DependencyPropertyChangedEventArgs e)
{
ContextMenu newValue = (ContextMenu) e.NewValue;
//TODO provide implementation
//currently not needed
}
#endregion
@@ -524,6 +510,48 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region TaskbarIconMiddleMouseDown
/// <summary>
/// TaskbarIconMiddleMouseDown Routed Event
/// </summary>
public static readonly RoutedEvent TaskbarIconMiddleMouseDownEvent = EventManager.RegisterRoutedEvent("TaskbarIconMiddleMouseDown",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Occurs when the user presses the middle mouse button.
/// </summary>
public event RoutedEventHandler TaskbarIconMiddleMouseDown
{
add { AddHandler(TaskbarIconMiddleMouseDownEvent, value); }
remove { RemoveHandler(TaskbarIconMiddleMouseDownEvent, value); }
}
/// <summary>
/// A helper method to raise the TaskbarIconMiddleMouseDown event.
/// </summary>
protected RoutedEventArgs RaiseTaskbarIconMiddleMouseDownEvent()
{
return RaiseTaskbarIconMiddleMouseDownEvent(this);
}
/// <summary>
/// A static helper method to raise the TaskbarIconMiddleMouseDown event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseTaskbarIconMiddleMouseDownEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = TaskbarIconMiddleMouseDownEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region TaskbarIconLeftMouseUp
/// <summary>
@@ -606,6 +634,332 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region TaskbarIconMiddleMouseUp
/// <summary>
/// TaskbarIconMiddleMouseUp Routed Event
/// </summary>
public static readonly RoutedEvent TaskbarIconMiddleMouseUpEvent = EventManager.RegisterRoutedEvent("TaskbarIconMiddleMouseUp",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Occurs when the user releases the middle mouse button.
/// </summary>
public event RoutedEventHandler TaskbarIconMiddleMouseUp
{
add { AddHandler(TaskbarIconMiddleMouseUpEvent, value); }
remove { RemoveHandler(TaskbarIconMiddleMouseUpEvent, value); }
}
/// <summary>
/// A helper method to raise the TaskbarIconMiddleMouseUp event.
/// </summary>
protected RoutedEventArgs RaiseTaskbarIconMiddleMouseUpEvent()
{
return RaiseTaskbarIconMiddleMouseUpEvent(this);
}
/// <summary>
/// A static helper method to raise the TaskbarIconMiddleMouseUp event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseTaskbarIconMiddleMouseUpEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = TaskbarIconMiddleMouseUpEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region TaskbarIconMouseDoubleClick
/// <summary>
/// TaskbarIconMouseDoubleClick Routed Event
/// </summary>
public static readonly RoutedEvent TaskbarIconMouseDoubleClickEvent = EventManager.RegisterRoutedEvent("TaskbarIconMouseDoubleClick",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Occurs when the user double-clicks the taskbar icon.
/// </summary>
public event RoutedEventHandler TaskbarIconMouseDoubleClick
{
add { AddHandler(TaskbarIconMouseDoubleClickEvent, value); }
remove { RemoveHandler(TaskbarIconMouseDoubleClickEvent, value); }
}
/// <summary>
/// A helper method to raise the TaskbarIconMouseDoubleClick event.
/// </summary>
protected RoutedEventArgs RaiseTaskbarIconMouseDoubleClickEvent()
{
return RaiseTaskbarIconMouseDoubleClickEvent(this);
}
/// <summary>
/// A static helper method to raise the TaskbarIconMouseDoubleClick event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseTaskbarIconMouseDoubleClickEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = TaskbarIconMouseDoubleClickEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region TaskbarIconMouseMove
/// <summary>
/// TaskbarIconMouseMove Routed Event
/// </summary>
public static readonly RoutedEvent TaskbarIconMouseMoveEvent = EventManager.RegisterRoutedEvent("TaskbarIconMouseMove",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Occurs when the user moves the mouse over the taskbar icon.
/// </summary>
public event RoutedEventHandler TaskbarIconMouseMove
{
add { AddHandler(TaskbarIconMouseMoveEvent, value); }
remove { RemoveHandler(TaskbarIconMouseMoveEvent, value); }
}
/// <summary>
/// A helper method to raise the TaskbarIconMouseMove event.
/// </summary>
protected RoutedEventArgs RaiseTaskbarIconMouseMoveEvent()
{
return RaiseTaskbarIconMouseMoveEvent(this);
}
/// <summary>
/// A static helper method to raise the TaskbarIconMouseMove event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseTaskbarIconMouseMoveEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = TaskbarIconMouseMoveEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region TaskbarIconBalloonTipShown
/// <summary>
/// TaskbarIconBalloonTipShown Routed Event
/// </summary>
public static readonly RoutedEvent TaskbarIconBalloonTipShownEvent = EventManager.RegisterRoutedEvent("TaskbarIconBalloonTipShown",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Occurs when a balloon ToolTip is displayed.
/// </summary>
public event RoutedEventHandler TaskbarIconBalloonTipShown
{
add { AddHandler(TaskbarIconBalloonTipShownEvent, value); }
remove { RemoveHandler(TaskbarIconBalloonTipShownEvent, value); }
}
/// <summary>
/// A helper method to raise the TaskbarIconBalloonTipShown event.
/// </summary>
protected RoutedEventArgs RaiseTaskbarIconBalloonTipShownEvent()
{
return RaiseTaskbarIconBalloonTipShownEvent(this);
}
/// <summary>
/// A static helper method to raise the TaskbarIconBalloonTipShown event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseTaskbarIconBalloonTipShownEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = TaskbarIconBalloonTipShownEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region TaskbarIconBalloonTipClosed
/// <summary>
/// TaskbarIconBalloonTipClosed Routed Event
/// </summary>
public static readonly RoutedEvent TaskbarIconBalloonTipClosedEvent = EventManager.RegisterRoutedEvent("TaskbarIconBalloonTipClosed",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Occurs when a balloon ToolTip was closed.
/// </summary>
public event RoutedEventHandler TaskbarIconBalloonTipClosed
{
add { AddHandler(TaskbarIconBalloonTipClosedEvent, value); }
remove { RemoveHandler(TaskbarIconBalloonTipClosedEvent, value); }
}
/// <summary>
/// A helper method to raise the TaskbarIconBalloonTipClosed event.
/// </summary>
protected RoutedEventArgs RaiseTaskbarIconBalloonTipClosedEvent()
{
return RaiseTaskbarIconBalloonTipClosedEvent(this);
}
/// <summary>
/// A static helper method to raise the TaskbarIconBalloonTipClosed event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseTaskbarIconBalloonTipClosedEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = TaskbarIconBalloonTipClosedEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region TaskbarIconBalloonTipClicked
/// <summary>
/// TaskbarIconBalloonTipClicked Routed Event
/// </summary>
public static readonly RoutedEvent TaskbarIconBalloonTipClickedEvent = EventManager.RegisterRoutedEvent("TaskbarIconBalloonTipClicked",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Occurs when the user clicks on a balloon ToolTip.
/// </summary>
public event RoutedEventHandler TaskbarIconBalloonTipClicked
{
add { AddHandler(TaskbarIconBalloonTipClickedEvent, value); }
remove { RemoveHandler(TaskbarIconBalloonTipClickedEvent, value); }
}
/// <summary>
/// A helper method to raise the TaskbarIconBalloonTipClicked event.
/// </summary>
protected RoutedEventArgs RaiseTaskbarIconBalloonTipClickedEvent()
{
return RaiseTaskbarIconBalloonTipClickedEvent(this);
}
/// <summary>
/// A static helper method to raise the TaskbarIconBalloonTipClicked event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseTaskbarIconBalloonTipClickedEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = TaskbarIconBalloonTipClickedEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region TaskbarIconContextMenuOpen (and PreviewTaskbarIconContextMenuOpen)
/// <summary>
/// TaskbarIconContextMenuOpen Routed Event
/// </summary>
public static readonly RoutedEvent TaskbarIconContextMenuOpenEvent = EventManager.RegisterRoutedEvent("TaskbarIconContextMenuOpen",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Bubbled event that occurs when the context menu of the taskbar icon is being displayed.
/// </summary>
public event RoutedEventHandler TaskbarIconContextMenuOpen
{
add { AddHandler(TaskbarIconContextMenuOpenEvent, value); }
remove { RemoveHandler(TaskbarIconContextMenuOpenEvent, value); }
}
/// <summary>
/// A helper method to raise the TaskbarIconContextMenuOpen event.
/// </summary>
protected RoutedEventArgs RaiseTaskbarIconContextMenuOpenEvent()
{
return RaiseTaskbarIconContextMenuOpenEvent(this);
}
/// <summary>
/// A static helper method to raise the TaskbarIconContextMenuOpen event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseTaskbarIconContextMenuOpenEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = TaskbarIconContextMenuOpenEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
/// <summary>
/// PreviewTaskbarIconContextMenuOpen Routed Event
/// </summary>
public static readonly RoutedEvent PreviewTaskbarIconContextMenuOpenEvent = EventManager.RegisterRoutedEvent("PreviewTaskbarIconContextMenuOpen",
RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Tunneled event that occurs when the context menu of the taskbar icon is being displayed.
/// </summary>
public event RoutedEventHandler PreviewTaskbarIconContextMenuOpen
{
add { AddHandler(PreviewTaskbarIconContextMenuOpenEvent, value); }
remove { RemoveHandler(PreviewTaskbarIconContextMenuOpenEvent, value); }
}
/// <summary>
/// A helper method to raise the PreviewTaskbarIconContextMenuOpen event.
/// </summary>
protected RoutedEventArgs RaisePreviewTaskbarIconContextMenuOpenEvent()
{
return RaisePreviewTaskbarIconContextMenuOpenEvent(this);
}
/// <summary>
/// A static helper method to raise the PreviewTaskbarIconContextMenuOpen event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaisePreviewTaskbarIconContextMenuOpenEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = PreviewTaskbarIconContextMenuOpenEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region TaskbarIconPopupOpen (and PreviewTaskbarIconPopupOpen)
@@ -685,6 +1039,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region TaskbarIconToolTipOpen (and PreviewTaskbarIconToolTipOpen)
/// <summary>
@@ -763,88 +1118,87 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region TaskbarIconContextMenuOpen (and PreviewTaskbarIconContextMenuOpen)
#region TaskbarIconToolTipClose (and PreviewTaskbarIconToolTipClose)
/// <summary>
/// TaskbarIconContextMenuOpen Routed Event
/// TaskbarIconToolTipClose Routed Event
/// </summary>
public static readonly RoutedEvent TaskbarIconContextMenuOpenEvent = EventManager.RegisterRoutedEvent("TaskbarIconContextMenuOpen",
public static readonly RoutedEvent TaskbarIconToolTipCloseEvent = EventManager.RegisterRoutedEvent("TaskbarIconToolTipClose",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Bubbled event that occurs when the context menu of the taskbar icon is being displayed.
/// Bubbled event that occurs when a custom tooltip is being closed.
/// </summary>
public event RoutedEventHandler TaskbarIconContextMenuOpen
public event RoutedEventHandler TaskbarIconToolTipClose
{
add { AddHandler(TaskbarIconContextMenuOpenEvent, value); }
remove { RemoveHandler(TaskbarIconContextMenuOpenEvent, value); }
add { AddHandler(TaskbarIconToolTipCloseEvent, value); }
remove { RemoveHandler(TaskbarIconToolTipCloseEvent, value); }
}
/// <summary>
/// A helper method to raise the TaskbarIconContextMenuOpen event.
/// A helper method to raise the TaskbarIconToolTipClose event.
/// </summary>
protected RoutedEventArgs RaiseTaskbarIconContextMenuOpenEvent()
protected RoutedEventArgs RaiseTaskbarIconToolTipCloseEvent()
{
return RaiseTaskbarIconContextMenuOpenEvent(this);
return RaiseTaskbarIconToolTipCloseEvent(this);
}
/// <summary>
/// A static helper method to raise the TaskbarIconContextMenuOpen event on a target element.
/// A static helper method to raise the TaskbarIconToolTipClose event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseTaskbarIconContextMenuOpenEvent(DependencyObject target)
internal static RoutedEventArgs RaiseTaskbarIconToolTipCloseEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = TaskbarIconContextMenuOpenEvent;
args.RoutedEvent = TaskbarIconToolTipCloseEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
/// <summary>
/// PreviewTaskbarIconContextMenuOpen Routed Event
/// PreviewTaskbarIconToolTipClose Routed Event
/// </summary>
public static readonly RoutedEvent PreviewTaskbarIconContextMenuOpenEvent = EventManager.RegisterRoutedEvent("PreviewTaskbarIconContextMenuOpen",
public static readonly RoutedEvent PreviewTaskbarIconToolTipCloseEvent = EventManager.RegisterRoutedEvent("PreviewTaskbarIconToolTipClose",
RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Tunneled event that occurs when the context menu of the taskbar icon is being displayed.
/// Tunneled event that occurs when a custom tooltip is being closed.
/// </summary>
public event RoutedEventHandler PreviewTaskbarIconContextMenuOpen
public event RoutedEventHandler PreviewTaskbarIconToolTipClose
{
add { AddHandler(PreviewTaskbarIconContextMenuOpenEvent, value); }
remove { RemoveHandler(PreviewTaskbarIconContextMenuOpenEvent, value); }
add { AddHandler(PreviewTaskbarIconToolTipCloseEvent, value); }
remove { RemoveHandler(PreviewTaskbarIconToolTipCloseEvent, value); }
}
/// <summary>
/// A helper method to raise the PreviewTaskbarIconContextMenuOpen event.
/// A helper method to raise the PreviewTaskbarIconToolTipClose event.
/// </summary>
protected RoutedEventArgs RaisePreviewTaskbarIconContextMenuOpenEvent()
protected RoutedEventArgs RaisePreviewTaskbarIconToolTipCloseEvent()
{
return RaisePreviewTaskbarIconContextMenuOpenEvent(this);
return RaisePreviewTaskbarIconToolTipCloseEvent(this);
}
/// <summary>
/// A static helper method to raise the PreviewTaskbarIconContextMenuOpen event on a target element.
/// A static helper method to raise the PreviewTaskbarIconToolTipClose event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaisePreviewTaskbarIconContextMenuOpenEvent(DependencyObject target)
internal static RoutedEventArgs RaisePreviewTaskbarIconToolTipCloseEvent(DependencyObject target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = PreviewTaskbarIconContextMenuOpenEvent;
args.RoutedEvent = PreviewTaskbarIconToolTipCloseEvent;
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
//CONSTRUCTOR DECLARATIONS
//BASE CLASS PROPERTY OVERRIDES
/// <summary>
/// Registers properties.

View File

@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Hardcodet.Wpf.TaskbarNotification.Interop;
namespace Hardcodet.Wpf.TaskbarNotification
@@ -27,39 +23,11 @@ namespace Hardcodet.Wpf.TaskbarNotification
private readonly Timer singleClickTimer;
#region SetVersion
#region Update ToolTip Settings
/// <summary>
/// Sets the version flag for the <see cref="iconData"/>.
/// </summary>
private void SetVersion()
{
iconData.VersionOrTimeout = (uint) NotifyIconVersion.Vista;
bool status = WinApi.Shell_NotifyIcon(NotifyCommand.SetVersion, ref iconData);
if (!status)
{
iconData.VersionOrTimeout = (uint) NotifyIconVersion.Win2000;
status = Util.WriteIconData(ref iconData, NotifyCommand.SetVersion);
}
if (!status)
{
iconData.VersionOrTimeout = (uint)NotifyIconVersion.Win95;
status = Util.WriteIconData(ref iconData, NotifyCommand.SetVersion);
}
if (!status)
{
Debug.Fail("Could not set version");
}
}
#endregion
/// <summary>
/// Sets tooltip settings for the class.
/// Sets tooltip settings for the class depending on defined
/// dependency properties and OS support.
/// </summary>
private void WriteToolTipSettings()
{
@@ -86,6 +54,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
Util.WriteIconData(ref iconData, NotifyCommand.Modify, flags);
}
#endregion
#region Show / Hide Balloon ToolTip
@@ -98,7 +67,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <param name="icon">Indicates the severity.</param>
public void ShowBalloonTip(string title, string message, BalloonIcon icon)
{
lock(this)
lock (this)
{
ShowBalloonTip(title, message, icon.GetBalloonFlag(), IntPtr.Zero);
}
@@ -118,14 +87,13 @@ namespace Hardcodet.Wpf.TaskbarNotification
{
if (customIcon == null) throw new ArgumentNullException("customIcon");
lock(this)
lock (this)
{
ShowBalloonTip(title, message, BalloonFlags.User, customIcon.Handle);
}
}
/// <summary>
/// Invokes <see cref="WinApi.Shell_NotifyIcon"/> in order to display
/// a given balloon ToolTip.
@@ -148,7 +116,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
}
/// <summary>
/// Hides a balloon ToolTip, if any is displayed.
/// </summary>
@@ -163,7 +130,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region Single Click Timer event
/// <summary>
@@ -175,6 +141,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
{
if (IsDisposed) return;
Console.Out.WriteLine("TIMER EVENT");
//run action
Action action = delayedTimerAction;
if (action != null)
@@ -189,7 +157,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region Show Tray Popup / Context Menu
/// <summary>
@@ -232,10 +199,10 @@ namespace Hardcodet.Wpf.TaskbarNotification
//raise preview event
var args = RaisePreviewTaskbarIconContextMenuOpenEvent();
if (args.Handled) return;
//CreateActivationWindow();
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);
@@ -247,4 +214,4 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
}
}
}

View File

@@ -1,47 +1,18 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Hardcodet.Wpf.TaskbarNotification.Interop;
using Rect=Hardcodet.Wpf.TaskbarNotification.Interop.Rect;
namespace Hardcodet.Wpf.TaskbarNotification
{
internal class MyClass
{
public void Test()
{
TaskbarIcon icon = new TaskbarIcon();
icon.Icon = Properties.Resources.DefaultTrayIcon;
Console.Out.WriteLine("DISPLAY NOW...");
Thread.CurrentThread.Join(1500);
//icon.ShowBalloonTip("some title", "hello world", Properties.Resources.DefaultTrayIcon);
//Console.Out.WriteLine("status = {0}", status);
Thread.CurrentThread.Join(5000);
}
public void Test2()
{
var tbInfo = TrayLocator.GetTaskbarInformation();
var w = new Window();
w.Background = Brushes.Red;
w.WindowStyle = WindowStyle.None;
Rect rect = tbInfo.Rectangle;
w.Width = Math.Max(20, rect.right - rect.left);
w.Height = Math.Max(20, rect.bottom - rect.top);
w.Left = rect.left;
w.Top = rect.top - 100;
w.ShowDialog();
}
}
/// <summary>
/// Represent a taskbar icon that sits in the system
/// tray.
/// A WPF proxy to for a taskbar icon (NotifyIcon) that sits in the system's
/// taskbar notification area ("system tray").
/// </summary>
public partial class TaskbarIcon : FrameworkElement, IDisposable
{
@@ -62,7 +33,9 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <summary>
/// Indicates whether custom tooltips are supported.
/// Indicates whether custom tooltips are supported, which depends
/// on the OS. Windows Vista or higher is required in order to
/// support this feature.
/// </summary>
public bool SupportsCustomToolTips
{
@@ -102,10 +75,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
//init single click timer
singleClickTimer = new Timer(DoSingleClickAction);
//register listener in order to get notified when the application closes
if (Application.Current != null) Application.Current.Exit += OnExit;
}
@@ -124,6 +95,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
switch(me)
{
case MouseEvent.MouseMove:
RaiseTaskbarIconMouseMoveEvent();
break;
case MouseEvent.IconRightMouseDown:
RaiseTaskbarIconRightMouseDownEvent();
@@ -138,16 +110,19 @@ namespace Hardcodet.Wpf.TaskbarNotification
RaiseTaskbarIconLeftMouseUpEvent();
break;
case MouseEvent.IconMiddleMouseDown:
RaiseTaskbarIconMiddleMouseDownEvent();
break;
case MouseEvent.IconMiddleMouseUp:
RaiseTaskbarIconMiddleMouseUpEvent();
break;
case MouseEvent.IconDoubleClick:
//cancel single click timer
singleClickTimer.Change(Timeout.Infinite, Timeout.Infinite);
//bubble event
RaiseTaskbarIconMouseDoubleClickEvent();
break;
case MouseEvent.BalloonToolTipClicked:
RaiseTaskbarIconBalloonTipClickedEvent();
break;
default:
throw new ArgumentOutOfRangeException("me", "Missing handler for mouse event flag: " + me);
@@ -191,23 +166,92 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <summary>
/// Displays a custom tooltip, if available. This functionality
/// is only available for Windows Vista and above.
/// </summary>
/// <param name="visible">Whether to show or hide the tooltip.</param>
private void OnToolTipChange(bool visible)
{
//if we have a custom tooltip, show it now
if (ToolTip == null) return;
ToolTip tt = (ToolTip)ToolTip;
tt.IsOpen = visible;
if (visible)
{
var args = RaisePreviewTaskbarIconToolTipOpenEvent();
if (args.Handled) return;
tt.IsOpen = true;
RaiseTaskbarIconToolTipOpenEvent();
}
else
{
var args = RaisePreviewTaskbarIconToolTipCloseEvent();
if (args.Handled) return;
tt.IsOpen = false;
RaiseTaskbarIconToolTipCloseEvent();
}
}
/// <summary>
/// Bubbles events if a balloon ToolTip was displayed
/// or removed.
/// </summary>
/// <param name="visible">Whether the ToolTip was just displayed
/// or removed.</param>
private void OnBalloonToolTipChanged(bool visible)
{
//TODO just raise event
if (visible)
{
RaiseTaskbarIconBalloonTipShownEvent();
}
else
{
RaiseTaskbarIconBalloonTipClosedEvent();
}
}
#region SetVersion
/// <summary>
/// Sets the version flag for the <see cref="iconData"/>.
/// </summary>
private void SetVersion()
{
iconData.VersionOrTimeout = (uint)NotifyIconVersion.Vista;
bool status = WinApi.Shell_NotifyIcon(NotifyCommand.SetVersion, ref iconData);
if (!status)
{
iconData.VersionOrTimeout = (uint)NotifyIconVersion.Win2000;
status = Util.WriteIconData(ref iconData, NotifyCommand.SetVersion);
}
if (!status)
{
iconData.VersionOrTimeout = (uint)NotifyIconVersion.Win95;
status = Util.WriteIconData(ref iconData, NotifyCommand.SetVersion);
}
if (!status)
{
Debug.Fail("Could not set version");
}
}
#endregion
#region Create / Remove Taskbar Icon
/// <summary>
/// Recreates the taskbar icon if the whole taskbar was
/// recreated (e.g. because Explorer was shut down).
@@ -219,12 +263,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
}
#region create / remove taskbar icon
/// <summary>
/// Creates the taskbar icon. This message is invoked during initialization,
/// if the taskbar is restarted, and whenever the icon is displayed.

View File

@@ -68,8 +68,8 @@ Title="Window1" Height="480" Width="579">
x:Name="tb"
ToolTip="{StaticResource yb}" Height="23" VerticalAlignment="Top"
>
<tb:TaskbarIcon.TaskbarIconPopup>
<tb:TaskbarIcon.TaskbarIconPopup>
<Popup StaysOpen="False">
<Border
Width="200"