NotifyWPF

---------
CHG   Lot of plumbing, some fixes
CHG   Work on sample project.
CHG   Popups and ContextMenu now store coordinates - otherwise delayed opending may happen elsewhere.

git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@55 9f600761-6f11-4665-b6dc-0185e9171623
This commit is contained in:
Philipp Sumi
2009-03-31 22:20:07 +00:00
parent 229042b075
commit 354ba1ca43
26 changed files with 1379 additions and 308 deletions

View File

@@ -0,0 +1,14 @@
using System.Runtime.InteropServices;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
/// <summary>
/// Win API struct providing coordinates for a single point.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public int X;
public int Y;
}
}

View File

@@ -74,7 +74,14 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// consider the mouse action a double-click.</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int GetDoubleClickTime();
/// <summary>
/// Gets the screen coordinates of the current mouse position.
/// </summary>
/// <param name="lpPoint"></param>
/// <returns></returns>
[DllImport("USER32.DLL", SetLastError = true)]
public static extern bool GetCursorPos(ref Point lpPoint);
}
}

View File

@@ -8,7 +8,7 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// Receives messages from the taskbar icon through
/// window messages of an underlying helper window.
/// </summary>
public partial class WindowMessageSink : IDisposable
public class WindowMessageSink : IDisposable
{
#region members
@@ -196,10 +196,11 @@ namespace Hardcodet.Wpf.TaskbarNotification.Interop
/// <summary>
/// Processes incoming system messages.
/// </summary>
/// <param name="msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
/// <param name="msg">Callback ID.</param>
/// <param name="wParam">If the version is <see cref="NotifyIconVersion.Vista"/>
/// or higher, this parameter can be used to resolve mouse coordinates.
/// Currently not in use.</param>
/// <param name="lParam">Provides information about the event.</param>
private void ProcessWindowMessage(uint msg, uint wParam, uint lParam)
{
if (msg != CallbackMessageId) return;

View File

@@ -53,6 +53,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BalloonIcon.cs" />
<Compile Include="Interop\Point.cs" />
<Compile Include="Interop\TrayLocator.cs" />
<Compile Include="Interop\WindowClass.cs" />
<Compile Include="PopupActivationMode.cs" />

View File

@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
@@ -14,8 +15,107 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// </summary>
partial class TaskbarIcon
{
/// <summary>
/// Category name that is set on designer properties.
/// </summary>
public const string CategoryName = "NotifyIcon";
/// <summary>
/// A <see cref="ToolTip"/> control that was created
/// in order to display either <see cref="TaskbarIconToolTip"/>
/// or <see cref="ToolTipText"/>.
/// </summary>
internal ToolTip CustomToolTip { get; private set; }
/// <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; }
//DEPENDENCY PROPERTIES
#region Icon property / IconSource dependency property
private Icon icon;
/// <summary>
/// Gets or sets the icon to be displayed. This is not a
/// dependency property - if you want to assign the property
/// through XAML, please use the <see cref="IconSource"/>
/// dependency property.
/// </summary>
[Browsable(false)]
public Icon Icon
{
get { return icon; }
set
{
icon = value;
iconData.IconHandle = value == null ? IntPtr.Zero : icon.Handle;
Util.WriteIconData(ref iconData, NotifyCommand.Modify, IconDataMembers.Icon);
}
}
/// <summary>
/// Resolves an image source and updates the <see cref="Icon" /> property accordingly.
/// </summary>
public static readonly DependencyProperty IconSourceProperty =
DependencyProperty.Register("IconSource",
typeof(ImageSource),
typeof(TaskbarIcon),
new FrameworkPropertyMetadata(null, IconSourcePropertyChanged));
/// <summary>
/// A property wrapper for the <see cref="IconSourceProperty"/>
/// dependency property:<br/>
/// Resolves an image source and updates the <see cref="Icon" /> property accordingly.
/// </summary>
[Category(CategoryName)]
[Description("Sets the displayed taskbar icon.")]
public ImageSource IconSource
{
get { return (ImageSource)GetValue(IconSourceProperty); }
set { SetValue(IconSourceProperty, value); }
}
/// <summary>
/// A static callback listener which is being invoked if the
/// <see cref="IconSourceProperty"/> dependency property has
/// been changed. Invokes the <see cref="OnIconSourcePropertyChanged"/>
/// 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 IconSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TaskbarIcon owner = (TaskbarIcon)d;
owner.OnIconSourcePropertyChanged(e);
}
/// <summary>
/// Handles changes of the <see cref="IconSourceProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="IconSource"/> 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 OnIconSourcePropertyChanged(DependencyPropertyChangedEventArgs e)
{
ImageSource newValue = (ImageSource)e.NewValue;
//resolving the ImageSource at design time probably won't work
if (!Util.IsDesignMode) Icon = newValue.ToIcon();
}
#endregion
#region ToolTipText dependency property
/// <summary>
@@ -35,6 +135,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// A tooltip text that is being displayed if no custom <see cref="ToolTip"/>
/// was set or if custom tooltips are not supported.
/// </summary>
[Category(CategoryName)]
[Description("Alternative to a fully blown ToolTip, which is only displayed on Vista and above.")]
public string ToolTipText
{
get { return (string) GetValue(ToolTipTextProperty); }
@@ -66,128 +168,77 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <param name="e">Provides information about the updated property.</param>
private void OnToolTipTextPropertyChanged(DependencyPropertyChangedEventArgs e)
{
string newValue = (string) e.NewValue;
iconData.ToolTipText = newValue ?? String.Empty;
//only recreate tooltip if we're not using a custom control
if (CustomToolTip == null || CustomToolTip.Content is string)
{
CreateCustomToolTip();
}
WriteToolTipSettings();
}
#endregion
#region ToolTip dependency property override
#region TaskbarIconToolTip dependency property
/// <summary>
/// A static callback listener which is being invoked if the
/// <see cref="FrameworkElement.ToolTipProperty"/> dependency property has
/// been changed. Invokes the <see cref="OnToolTipPropertyChanged"/>
/// instance method of the changed instance.
/// A custom UI element that is displayed as a tooltip if the user hovers over the taskbar icon.
/// Works only with Vista and above. Accordingly, you should make sure that
/// the <see cref="ToolTipText"/> property is set as well.
/// </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 ToolTipPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TaskbarIcon owner = (TaskbarIcon) d;
owner.OnToolTipPropertyChanged(e);
}
/// <summary>
/// Handles changes of the <see cref="FrameworkElement.ToolTipProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="ToolTip"/> 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 OnToolTipPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null)
{
ToolTip tt = e.NewValue as ToolTip;
if (tt == null)
{
tt = new ToolTip();
tt.Content = e.NewValue;
ToolTip = tt;
return;
}
}
WriteToolTipSettings();
}
#endregion
#region Icon property / IconSource dependency property
private Icon icon;
/// <summary>
/// Gets or sets the icon to be displayed. This is not a
/// dependency property - if you want to assign the property
/// through XAML, please use the <see cref="IconSource"/>
/// dependency property.
/// </summary>
public Icon Icon
{
get { return icon; }
set
{
icon = value;
iconData.IconHandle = value == null ? IntPtr.Zero : icon.Handle;
Util.WriteIconData(ref iconData, NotifyCommand.Modify, IconDataMembers.Icon);
}
}
/// <summary>
/// Resolves an image source and updates the <see cref="Icon" /> property accordingly.
/// </summary>
public static readonly DependencyProperty IconSourceProperty =
DependencyProperty.Register("IconSource",
typeof (ImageSource),
public static readonly DependencyProperty TaskbarIconToolTipProperty =
DependencyProperty.Register("TaskbarIconToolTip",
typeof (UIElement),
typeof (TaskbarIcon),
new FrameworkPropertyMetadata(null, IconSourcePropertyChanged));
new FrameworkPropertyMetadata(null, TaskbarIconToolTipPropertyChanged));
/// <summary>
/// A property wrapper for the <see cref="IconSourceProperty"/>
/// A property wrapper for the <see cref="TaskbarIconToolTipProperty"/>
/// dependency property:<br/>
/// Resolves an image source and updates the <see cref="Icon" /> property accordingly.
/// A custom UI element that is displayed as a tooltip if the user hovers over the taskbar icon.
/// Works only with Vista and above. Accordingly, you should make sure that
/// the <see cref="ToolTipText"/> property is set as well.
/// </summary>
public ImageSource IconSource
[Category(CategoryName)]
[Description("Custom UI element that is displayed as a tooltip. Only on Vista and above")]
public UIElement TaskbarIconToolTip
{
get { return (ImageSource) GetValue(IconSourceProperty); }
set { SetValue(IconSourceProperty, value); }
get { return (UIElement) GetValue(TaskbarIconToolTipProperty); }
set { SetValue(TaskbarIconToolTipProperty, value); }
}
/// <summary>
/// A static callback listener which is being invoked if the
/// <see cref="IconSourceProperty"/> dependency property has
/// been changed. Invokes the <see cref="OnIconSourcePropertyChanged"/>
/// <see cref="TaskbarIconToolTipProperty"/> dependency property has
/// been changed. Invokes the <see cref="OnTaskbarIconToolTipPropertyChanged"/>
/// 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 IconSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void TaskbarIconToolTipPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TaskbarIcon owner = (TaskbarIcon) d;
owner.OnIconSourcePropertyChanged(e);
owner.OnTaskbarIconToolTipPropertyChanged(e);
}
/// <summary>
/// Handles changes of the <see cref="IconSourceProperty"/> dependency property. As
/// Handles changes of the <see cref="TaskbarIconToolTipProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="IconSource"/> property wrapper, updates of the property's value
/// <see cref="TaskbarIconToolTip"/> 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 OnIconSourcePropertyChanged(DependencyPropertyChangedEventArgs e)
private void OnTaskbarIconToolTipPropertyChanged(DependencyPropertyChangedEventArgs e)
{
ImageSource newValue = (ImageSource) e.NewValue;
Icon = newValue.ToIcon();
//recreate tooltip control
CreateCustomToolTip();
//udpate tooltip settings - needed to make sure a string is set, even
//if the ToolTipText property is not set. Otherwise, the event that
//triggers tooltip display is never fired.
WriteToolTipSettings();
}
#endregion
@@ -195,22 +246,24 @@ namespace Hardcodet.Wpf.TaskbarNotification
#region TaskbarIconPopup dependency property
/// <summary>
/// A custom popup that is displayed when the taskbar icon is clicked.
/// A control that is displayed as a popup when the taskbar icon is clicked.
/// </summary>
public static readonly DependencyProperty TaskbarIconPopupProperty =
DependencyProperty.Register("TaskbarIconPopup",
typeof (Popup),
typeof(UIElement),
typeof (TaskbarIcon),
new FrameworkPropertyMetadata(null, TaskbarIconPopupPropertyChanged));
/// <summary>
/// A property wrapper for the <see cref="TaskbarIconPopupProperty"/>
/// dependency property:<br/>
/// A custom popup that is displayed when the taskbar icon is clicked.
/// A control that is displayed as a popup when the taskbar icon is clicked.
/// </summary>
public Popup TaskbarIconPopup
[Category(CategoryName)]
[Description("Displayed as a Popup if the user clicks on the taskbar icon.")]
public UIElement TaskbarIconPopup
{
get { return (Popup) GetValue(TaskbarIconPopupProperty); }
get { return (UIElement)GetValue(TaskbarIconPopupProperty); }
set { SetValue(TaskbarIconPopupProperty, value); }
}
@@ -240,10 +293,12 @@ namespace Hardcodet.Wpf.TaskbarNotification
private void OnTaskbarIconPopupPropertyChanged(DependencyPropertyChangedEventArgs e)
{
//currently not needed
CreatePopup();
}
#endregion
#region MenuActivation dependency property
/// <summary>
@@ -262,6 +317,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// Defines what mouse events display the context menu.
/// Defaults to <see cref="PopupActivationMode.RightClick"/>.
/// </summary>
[Category(CategoryName)]
[Description("Defines what mouse events display the context menu.")]
public PopupActivationMode MenuActivation
{
get { return (PopupActivationMode) GetValue(MenuActivationProperty); }
@@ -316,6 +373,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// Defines what mouse events trigger the <see cref="TaskbarIconPopup" />.
/// Default is <see cref="PopupActivationMode.LeftClick" />.
/// </summary>
[Category(CategoryName)]
[Description("Defines what mouse events display the TaskbarIconPopup.")]
public PopupActivationMode PopupActivation
{
get { return (PopupActivationMode) GetValue(PopupActivationProperty); }
@@ -352,6 +411,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region Visibility dependency property override
/// <summary>
@@ -439,6 +499,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <summary>
/// Occurs when the user presses the left mouse button.
/// </summary>
[Category(CategoryName)]
public event RoutedEventHandler TaskbarIconLeftMouseDown
{
add { AddHandler(TaskbarIconLeftMouseDownEvent, value); }
@@ -1212,11 +1273,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
//register change listener for the ContextMenu property
md = new FrameworkPropertyMetadata(new PropertyChangedCallback(ContextMenuPropertyChanged));
ContextMenuProperty.OverrideMetadata(typeof (TaskbarIcon), md);
//register change listener for the ToolTip property
md = new FrameworkPropertyMetadata(new PropertyChangedCallback(ToolTipPropertyChanged));
ToolTipProperty.OverrideMetadata(typeof(TaskbarIcon), md);
}
}
}

View File

@@ -1,10 +1,12 @@
using System;
using System.Diagnostics;
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
{
@@ -23,7 +25,94 @@ namespace Hardcodet.Wpf.TaskbarNotification
private readonly Timer singleClickTimer;
#region Update ToolTip Settings
#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
@@ -31,32 +120,28 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// </summary>
private void WriteToolTipSettings()
{
IconDataMembers flags = IconDataMembers.Tip;
const IconDataMembers flags = IconDataMembers.Tip;
iconData.ToolTipText = ToolTipText;
if (messageSink.Version == NotifyIconVersion.Vista)
{
if (String.IsNullOrEmpty(ToolTipText) && ToolTip != null)
//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
//need to set a dummy value (we're displaying the ToolTip control, not the string)
iconData.ToolTipText = "ToolTip";
}
else if (!String.IsNullOrEmpty(ToolTipText) && ToolTip == null)
{
//if a tooltip text was set but there is no custom tooltip,
//we need to fall back to legacy operations
flags |= IconDataMembers.UseLegacyToolTips;
}
}
//just write the the tooltip
//update the tooltip text
Util.WriteIconData(ref iconData, NotifyCommand.Modify, flags);
}
#endregion
#region Show / Hide Balloon ToolTip
#region Show / Hide Balloon Tip
/// <summary>
/// Displays a balloon tip with the specified title,
@@ -64,12 +149,12 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// </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="icon">Indicates the severity.</param>
public void ShowBalloonTip(string title, string message, BalloonIcon icon)
/// <param name="symbol">A symbol that indicates the severity.</param>
public void ShowBalloonTip(string title, string message, BalloonIcon symbol)
{
lock (this)
{
ShowBalloonTip(title, message, icon.GetBalloonFlag(), IntPtr.Zero);
ShowBalloonTip(title, message, symbol.GetBalloonFlag(), IntPtr.Zero);
}
}
@@ -157,24 +242,78 @@ namespace Hardcodet.Wpf.TaskbarNotification
#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()
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)
{
//raise preview event
var args = RaisePreviewTaskbarIconPopupOpenEvent();
if (args.Handled) return;
//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
TaskbarIconPopup.IsOpen = true;
CustomPopup.IsOpen = true;
//activate the message window to track deactivation - otherwise, the context menu
//does not close if the user clicks somewhere else
@@ -190,17 +329,21 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// Displays the <see cref="ContextMenu"/> if
/// it was set.
/// </summary>
private void ShowContextMenu()
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)
{
//raise preview event
var args = RaisePreviewTaskbarIconContextMenuOpenEvent();
if (args.Handled) return;
//CreateActivationWindow();
//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

View File

@@ -1,11 +1,15 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
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
@@ -43,6 +47,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
}
#region Construction
/// <summary>
/// Inits the taskbar icon and registers a message listener
/// in order to receive events from the taskbar area.
@@ -79,8 +85,11 @@ namespace Hardcodet.Wpf.TaskbarNotification
if (Application.Current != null) Application.Current.Exit += OnExit;
}
#endregion
#region Handle Mouse Events
/// <summary>
/// Processes mouse events, which are bubbled
/// through the class' routed events, trigger
@@ -96,7 +105,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
{
case MouseEvent.MouseMove:
RaiseTaskbarIconMouseMoveEvent();
break;
//immediately return - there's nothing left to evaluate
return;
case MouseEvent.IconRightMouseDown:
RaiseTaskbarIconRightMouseDownEvent();
break;
@@ -130,19 +140,23 @@ namespace Hardcodet.Wpf.TaskbarNotification
}
//get mouse coordinates
Point cursorPosition = new Point();
WinApi.GetCursorPos(ref cursorPosition);
//show popup, if requested
if (me.IsMatch(PopupActivation))
{
if (me == MouseEvent.IconLeftMouseUp)
{
//show popup once we are sure it's not a double click
delayedTimerAction = ShowTrayPopup;
delayedTimerAction = () => ShowTrayPopup(cursorPosition);
singleClickTimer.Change(WinApi.GetDoubleClickTime(), Timeout.Infinite);
}
else
{
//show popup immediately
ShowTrayPopup();
ShowTrayPopup(cursorPosition);
}
}
@@ -153,51 +167,18 @@ namespace Hardcodet.Wpf.TaskbarNotification
if (me == MouseEvent.IconLeftMouseUp)
{
//show context menu once we are sure it's not a double click
delayedTimerAction = ShowContextMenu;
delayedTimerAction = () => ShowContextMenu(cursorPosition);
singleClickTimer.Change(WinApi.GetDoubleClickTime(), Timeout.Infinite);
}
else
{
//show context menu immediately
ShowContextMenu();
ShowContextMenu(cursorPosition);
}
}
}
/// <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;
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.
@@ -208,7 +189,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
{
if (visible)
{
RaiseTaskbarIconBalloonTipShownEvent();
RaiseTaskbarIconBalloonTipShownEvent();
}
else
{
@@ -216,7 +197,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
}
}
#endregion
#region SetVersion

View File

@@ -214,7 +214,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
case PopupActivationMode.MiddleClick:
return me == MouseEvent.IconMiddleMouseUp;
case PopupActivationMode.All:
return true;
//return true for everything except mouse movements
return me != MouseEvent.MouseMove;
default:
throw new ArgumentOutOfRangeException("activationMode");
}