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");
}

View File

@@ -4,5 +4,11 @@
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TaskbarIconResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
namespace Sample_Project.Commands
{
/// <summary>
/// Hides the main window.
/// </summary>
public class HideMainWindowCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
Application.Current.MainWindow.Hide();
TaskbarIconCommands.RefreshCommands();
}
public bool CanExecute(object parameter)
{
return Application.Current.MainWindow.IsVisible;
}
/// <summary>
/// Raises the <see cref="CanExecuteChanged"/> event.
/// </summary>
internal void RaiseCanExcecuteChanged()
{
if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
namespace Sample_Project.Commands
{
/// <summary>
/// Shows the main window.
/// </summary>
public class ShowMainWindowCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
Application.Current.MainWindow.Show();
TaskbarIconCommands.RefreshCommands();
}
public bool CanExecute(object parameter)
{
return Application.Current.MainWindow.IsVisible == false;
}
/// <summary>
/// Raises the <see cref="CanExecuteChanged"/> event.
/// </summary>
internal void RaiseCanExcecuteChanged()
{
if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
namespace Sample_Project.Commands
{
public static class TaskbarIconCommands
{
public static HideMainWindowCommand HideMain { get; set; }
public static ShowMainWindowCommand ShowMain { get; set; }
static TaskbarIconCommands()
{
HideMain = new HideMainWindowCommand();
ShowMain =new ShowMainWindowCommand();
}
public static void RefreshCommands()
{
HideMain.RaiseCanExcecuteChanged();
ShowMain.RaiseCanExcecuteChanged();
}
}
}

View File

@@ -0,0 +1,92 @@
<UserControl
x:Class="Sample_Project.FancyPopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="215"
Width="300" x:Name="me">
<Grid>
<Border
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="Auto"
Height="Auto"
Margin="5,5,5,5"
CornerRadius="10,10,10,10">
<Border.Effect>
<DropShadowEffect
Color="#FF474747" />
</Border.Effect>
<Border.Background>
<LinearGradientBrush
EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop
Color="#FF58C2FF"
Offset="0" />
<GradientStop
Color="#FFFFFFFF"
Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Image
HorizontalAlignment="Left"
Margin="19,10,0,0"
VerticalAlignment="Top"
Width="72"
Height="72"
Source="Images\preferences.png"
Stretch="Fill" />
<TextBlock
Margin="107,10,20,0"
TextWrapping="Wrap"
Height="32"
VerticalAlignment="Top"
FontSize="16"
FontWeight="Bold"
Foreground="#FF575757"><Run
Text="This is a fancy Popup..."
Language="de-ch" /></TextBlock>
<Button
Click="OnButtonClick"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Width="89"
Height="29"
Content="Click me"
Margin="0,0,20,20"
BorderBrush="#FFFFFFFF">
<Button.Background>
<LinearGradientBrush
EndPoint="0,1"
StartPoint="0,0">
<GradientStop
Color="#FFFFFFFF"
Offset="1" />
<GradientStop
Color="#FFFFD197"
Offset="0.459" />
<GradientStop
Color="#FFFFDBAD"
Offset="0" />
<GradientStop
Color="#FFF18E23"
Offset="0.508" />
<GradientStop
Color="#FFF9D4AC"
Offset="0.954" />
</LinearGradientBrush>
</Button.Background>
</Button>
<TextBlock
Margin="19,92,10,49"
TextWrapping="Wrap"><Run
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sed sem. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla dui nisi, venenatis" /><Run
Text="."
Language="de-ch" /></TextBlock>
<TextBlock Margin="0,52,20,0" VerticalAlignment="Top" Height="30" TextWrapping="Wrap" FontWeight="Bold" FontSize="14" HorizontalAlignment="Right" Foreground="#FF575757"><Run Text="Clicks: " Language="de-ch"/><InlineUIContainer>
<TextBlock Width="Auto" Height="Auto" Text="{Binding Path=ClickCount, ElementName=me, Mode=Default}" TextWrapping="Wrap"/>
</InlineUIContainer></TextBlock>
</Grid>
</UserControl>

View File

@@ -0,0 +1,46 @@
using System.Windows;
using System.Windows.Controls;
namespace Sample_Project
{
/// <summary>
/// Interaction logic for FancyPopup.xaml
/// </summary>
public partial class FancyPopup : UserControl
{
#region ClickCount dependency property
/// <summary>
/// The number of clicks on the popup button.
/// </summary>
public static readonly DependencyProperty ClickCountProperty =
DependencyProperty.Register("ClickCount",
typeof (int),
typeof (FancyPopup),
new FrameworkPropertyMetadata(0));
/// <summary>
/// A property wrapper for the <see cref="ClickCountProperty"/>
/// dependency property:<br/>
/// The number of clicks on the popup button.
/// </summary>
public int ClickCount
{
get { return (int) GetValue(ClickCountProperty); }
set { SetValue(ClickCountProperty, value); }
}
#endregion
public FancyPopup()
{
InitializeComponent();
}
private void OnButtonClick(object sender, RoutedEventArgs e)
{
//just increment a counter - will be displayed on screen
ClickCount++;
}
}
}

View File

@@ -0,0 +1,68 @@
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:tb="clr-namespace:Hardcodet.Wpf.TaskbarNotification;assembly=Hardcodet.Wpf.TaskbarNotification"
mc:Ignorable="d"
x:Class="Sample_Project.FancyToolTip"
x:Name="me"
Width="285"
Height="108">
<Grid
x:Name="LayoutRoot">
<Border
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="Auto"
Height="Auto"
CornerRadius="6,6,6,6"
BorderThickness="3,3,3,3"
Margin="0,0,5,5">
<Border.Effect>
<DropShadowEffect
Color="#FF7A7A7A" />
</Border.Effect>
<Border.Background>
<LinearGradientBrush
EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop
Color="#FFFFD283"
Offset="0" />
<GradientStop
Color="#FFFFFFFF"
Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Image
HorizontalAlignment="Left"
Margin="10,10,0,26"
Width="72"
Source="Images\Info.png"
Stretch="Fill"
VerticalAlignment="Top"
RenderTransformOrigin="0.792,0.486" />
<TextBlock
Margin="82,10,20,0"
TextWrapping="Wrap"
Height="32"
VerticalAlignment="Top"
FontSize="16"
FontWeight="Bold"
Foreground="#FF575757"><Run
Text="This is a fancy ToolTip..."
Language="de-ch" /></TextBlock>
<TextBlock
FontSize="12"
FontWeight="Normal"
Foreground="#FF141414"
TextWrapping="Wrap"
Margin="82,52,20,0"
VerticalAlignment="Top"
Text="{Binding ElementName=me, Path=InfoText}"
x:Name="txtToolTipDetail" />
</Grid>
</UserControl>

View File

@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Sample_Project
{
/// <summary>
/// Interaction logic for FancyToolTip.xaml
/// </summary>
public partial class FancyToolTip
{
#region InfoText dependency property
/// <summary>
/// The tooltip details.
/// </summary>
public static readonly DependencyProperty InfoTextProperty =
DependencyProperty.Register("InfoText",
typeof (string),
typeof (FancyToolTip),
new FrameworkPropertyMetadata("", InfoTextPropertyChanged));
/// <summary>
/// A property wrapper for the <see cref="InfoTextProperty"/>
/// dependency property:<br/>
/// The tooltip details.
/// </summary>
public string InfoText
{
get { return (string) GetValue(InfoTextProperty); }
set { SetValue(InfoTextProperty, value); }
}
/// <summary>
/// A static callback listener which is being invoked if the
/// <see cref="InfoTextProperty"/> dependency property has
/// been changed. Invokes the <see cref="OnInfoTextPropertyChanged"/>
/// 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 InfoTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FancyToolTip owner = (FancyToolTip) d;
owner.OnInfoTextPropertyChanged(e);
}
/// <summary>
/// Handles changes of the <see cref="InfoTextProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="InfoText"/> 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 OnInfoTextPropertyChanged(DependencyPropertyChangedEventArgs e)
{
// string newValue = (string) e.NewValue;
}
#endregion
public FancyToolTip()
{
this.InitializeComponent();
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 502 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 237 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

View File

@@ -33,6 +33,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationFramework.Aero" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@@ -46,29 +47,63 @@
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="UIAutomationProvider">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="PresentationCore">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="PresentationFramework">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Resource Include="Icons\Inactive.ico" />
<Resource Include="Icons\Error.ico" />
<Resource Include="Images\Info.png" />
<Resource Include="Images\preferences.png" />
<Page Include="FancyToolTip.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="TaskbarIconResources.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="FancyToolTip.xaml.cs">
<DependentUpon>FancyToolTip.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="FancyPopup.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Window1.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Window1.xaml.cs">
<DependentUpon>Window1.xaml</DependentUpon>
<SubType>Code</SubType>
<DependentUpon>Window1.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\TaskbarIconCommands.cs" />
<Compile Include="Commands\HideMainWindowCommand.cs" />
<Compile Include="Commands\ShowMainWindowCommand.cs" />
<Compile Include="FancyPopup.xaml.cs">
<DependentUpon>FancyPopup.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
@@ -79,8 +114,8 @@
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
@@ -107,6 +142,10 @@
<ItemGroup>
<Resource Include="Icons\Computers.ico" />
</ItemGroup>
<ItemGroup>
<Resource Include="Images\Add.png" />
<Resource Include="Images\Remove.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="Samples\" />
</ItemGroup>

View File

@@ -0,0 +1,142 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Commands="clr-namespace:Sample_Project.Commands"
xmlns:tb="clr-namespace:Hardcodet.Wpf.TaskbarNotification;assembly=Hardcodet.Wpf.TaskbarNotification"
>
<LinearGradientBrush x:Key="MenuBackground" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFFD892" Offset="1"/>
<GradientStop Color="#FFFFF3DD" Offset="0.259"/>
</LinearGradientBrush>
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="BorderThickness" Value="1,1,1,1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContextMenu}">
<Border Background="{DynamicResource MenuBackground}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}" Uid="ScrollViewer_9" CanContentScroll="True">
<ItemsPresenter Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Cycle"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- The taskbar context menu -->
<ContextMenu
x:Key="tbMenu">
<MenuItem
Header="Show Main Window"
Command="{Binding Source={x:Static Commands:TaskbarIconCommands.ShowMain}}">
<MenuItem.Icon>
<Image
Width="16"
Height="16"
Source="/Images/Add.png" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem
Header="Hide Main Window"
Command="{Binding Source={x:Static Commands:TaskbarIconCommands.HideMain}}">
<MenuItem.Icon>
<Image
Width="16"
Height="16"
Source="/Images/Remove.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
<ToolTip
x:Key="tbToolTip"
Background="Transparent"
BorderThickness="0"
HasDropShadow="False"
VerticalOffset="-10"
>
<Grid
>
<Border
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Width="Auto"
Height="Auto"
CornerRadius="6,6,6,6"
BorderThickness="3,3,3,3"
Margin="0,0,5,5">
<Border.Effect>
<DropShadowEffect
Color="#FF7A7A7A" />
</Border.Effect>
<Border.Background>
<LinearGradientBrush
EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop
Color="#FFFFD283"
Offset="0" />
<GradientStop
Color="#FFFFFFFF"
Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Image
HorizontalAlignment="Left"
Margin="10,10,0,26"
Width="72"
Source="Images\Info.png"
Stretch="Fill"
VerticalAlignment="Top"
RenderTransformOrigin="0.792,0.486" />
<TextBlock
Margin="82,10,20,0"
TextWrapping="Wrap"
Height="32"
VerticalAlignment="Top"
FontSize="16"
FontWeight="Bold"
Foreground="#FF575757"><Run
Text="This is a fancy ToolTip..."
Language="de-ch" /></TextBlock>
<TextBlock
FontSize="12"
FontWeight="Normal"
Foreground="#FF141414"
TextWrapping="Wrap"
Margin="82,52,20,0"
VerticalAlignment="Top"
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type tb:TaskbarIcon}}, Path=ToolTipText}"
/>
</Grid>
</ToolTip>
<!--
As we don't declare the popup with the control, use
commands to trigger an action.
-->
<Popup
x:Key="tbPopupSimple"
PopupAnimation="Slide"
Placement="Mouse"
AllowsTransparency="True"
StaysOpen="False"
>
<Border
Width="200"
Height="200"
Background="Red">
<Button>Click me</Button>
</Border>
</Popup>
</ResourceDictionary>

View File

@@ -2,146 +2,437 @@
x:Class="Sample_Project.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
Title="Window1" Height="480" Width="579">
<Window.Resources>
<Storyboard x:Key="OnTaskbarIconLeftMouseUp1">
<BooleanAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="popup" Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True"/>
<DiscreteBooleanKeyFrame KeyTime="00:00:02" Value="False"/>
</BooleanAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:01" Value="74"/>
<SplineDoubleKeyFrame KeyTime="00:00:01.5000000" Value="26"/>
<SplineDoubleKeyFrame KeyTime="00:00:02" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
<SplineDoubleKeyFrame KeyTime="00:00:01" Value="106"/>
<SplineDoubleKeyFrame KeyTime="00:00:01.5000000" Value="151"/>
<SplineDoubleKeyFrame KeyTime="00:00:02" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="popup" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.1"/>
<SplineDoubleKeyFrame KeyTime="00:00:02" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.47"/>
<SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
<SplineDoubleKeyFrame KeyTime="00:00:01.5000000" Value="1"/>
<SplineDoubleKeyFrame KeyTime="00:00:02" Value="0.685"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="border" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
<SplineDoubleKeyFrame KeyTime="00:00:01" Value="0.515"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="tb:TaskbarIcon.TaskbarIconLeftMouseUp" SourceName="tb">
<BeginStoryboard Storyboard="{StaticResource OnTaskbarIconLeftMouseUp1}"/>
</EventTrigger>
</Window.Triggers>
<Grid>
<Grid.Resources>
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1"
Height="808"
Width="769"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:Sample_Project">
<Window.Resources>
<Border
x:Key="yb"
Background="Yellow">
<Button
Width="200"
Height="200"
Content="CONTENT" />
</Border>
<BooleanToVisibilityConverter
x:Key="BooleanToVisibilityConverter" />
<ToolTip x:Key="borderToolTip">
<Border
Background="Red"
Opacity="0.75">
<TextBlock
Text="hello world" />
</Border>
</ToolTip>
</Grid.Resources>
<tb:TaskbarIcon
x:Name="tb"
ToolTip="{StaticResource yb}" Height="23" VerticalAlignment="Top"
>
<ObjectDataProvider
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="ActivationModes">
<ObjectDataProvider.MethodParameters>
<x:Type
TypeName="tb:PopupActivationMode" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<tb:TaskbarIcon.TaskbarIconPopup>
<Popup StaysOpen="False">
<Border
Width="200"
Height="200"
Background="Orange">
<TextBlock
Text="POPUP!" />
</Border>
</Popup>
<Storyboard
x:Key="OnTaskbarIconLeftMouseUp1">
<BooleanAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="popup"
Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame
KeyTime="00:00:00"
Value="True" />
<DiscreteBooleanKeyFrame
KeyTime="00:00:02"
Value="False" />
</BooleanAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<SplineDoubleKeyFrame
KeyTime="00:00:00"
Value="0" />
<SplineDoubleKeyFrame
KeyTime="00:00:01"
Value="74" />
<SplineDoubleKeyFrame
KeyTime="00:00:01.5000000"
Value="26" />
<SplineDoubleKeyFrame
KeyTime="00:00:02"
Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
<SplineDoubleKeyFrame
KeyTime="00:00:00"
Value="0" />
<SplineDoubleKeyFrame
KeyTime="00:00:01"
Value="106" />
<SplineDoubleKeyFrame
KeyTime="00:00:01.5000000"
Value="151" />
<SplineDoubleKeyFrame
KeyTime="00:00:02"
Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="popup"
Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame
KeyTime="00:00:00"
Value="0.1" />
<SplineDoubleKeyFrame
KeyTime="00:00:02"
Value="1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame
KeyTime="00:00:00"
Value="0.47" />
<SplineDoubleKeyFrame
KeyTime="00:00:01"
Value="1" />
<SplineDoubleKeyFrame
KeyTime="00:00:01.5000000"
Value="1" />
<SplineDoubleKeyFrame
KeyTime="00:00:02"
Value="0.685" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="border"
Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame
KeyTime="00:00:00"
Value="1" />
<SplineDoubleKeyFrame
KeyTime="00:00:01"
Value="0.515" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger
RoutedEvent="tb:TaskbarIcon.TaskbarIconLeftMouseUp"
SourceName="tb">
<BeginStoryboard
Storyboard="{StaticResource OnTaskbarIconLeftMouseUp1}" />
</EventTrigger>
</Window.Triggers>
<Grid>
<tb:TaskbarIcon
x:Name="tb"
VerticalAlignment="Top"
IconSource="{Binding Path=SelectedItem.Source, ElementName=iconList, Mode=Default}"
ContextMenu="{DynamicResource tbMenu}"
ToolTip="hello world"
ToolTipText="{Binding Path=Text, ElementName=txtToolTipText, Mode=Default}"
Visibility="{Binding Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=iconVisibility, Mode=Default}"
MenuActivation="{Binding Path=SelectedItem, ElementName=lstMenuTrigger, Mode=Default}"
PopupActivation="{Binding Path=SelectedItem, ElementName=lstPopupTrigger, Mode=Default}">
<tb:TaskbarIcon.TaskbarIconPopup>
<local:FancyPopup />
</tb:TaskbarIcon.TaskbarIconPopup>
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem
Header="Open" />
<MenuItem
Header="Quite" />
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
<!-- <tb:TaskbarIcon.ToolTip>-->
<!-- <Border Background="Yellow">-->
<!-- <TextBlock-->
<!-- Text="PopupToolTip" />-->
<!-- </Border>-->
<!-- </tb:TaskbarIcon.ToolTip>-->
</tb:TaskbarIcon>
<tb:TaskbarIcon.TaskbarIconToolTip>
<!-- the control will be put into a tooltip with an explicit DataContext -->
<local:FancyToolTip
Opacity="0.85"
InfoText="{Binding Path=ToolTipText}" />
</tb:TaskbarIcon.TaskbarIconToolTip>
</tb:TaskbarIcon>
<Button
x:Name="btn"
Click="OnClick"
Content="Click me"
Height="24"
Margin="12,33,66,0"
VerticalAlignment="Top" />
Margin="0,70,68,0"
VerticalAlignment="Top"
Width="73"
HorizontalAlignment="Right" />
<Button
Height="23"
HorizontalAlignment="Right"
Margin="0,91,64,0"
Name="button1"
Margin="0,116,66,0"
x:Name="button1"
VerticalAlignment="Top"
Width="75">Button</Button>
<TextBox
Text="{Binding ElementName=tb, Path=ToolTipText}"
Height="23"
Margin="13,91,0,0"
Name="textBox1"
Width="75"
Content="Button" />
<Popup
x:Name="popup"
Margin="-231,-276,231,0"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="120" />
<Popup x:Name="popup">
<Grid Width="180" Height="227">
<Border CornerRadius="10,10,10,10" x:Name="border">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFF4B569" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Ellipse Fill="#FFC13E3E" Stroke="#FF000000" VerticalAlignment="Top" Height="58" Margin="51,19,75,0" x:Name="ellipse" RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Ellipse.RenderTransform>
</Ellipse>
</Grid>
Height="549"
d:IsHidden="True"
d:IsLocked="True">
<Grid
Width="180"
Height="227">
<Border
CornerRadius="10,10,10,10"
x:Name="border">
<Border.Background>
<LinearGradientBrush
EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop
Color="#FFF4B569"
Offset="0" />
<GradientStop
Color="#FFFFFFFF"
Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Ellipse
Fill="#FFC13E3E"
Stroke="#FF000000"
VerticalAlignment="Top"
Height="58"
Margin="51,19,75,0"
x:Name="ellipse"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
<TransformGroup>
<ScaleTransform
ScaleX="1"
ScaleY="1" />
<SkewTransform
AngleX="0"
AngleY="0" />
<RotateTransform
Angle="0" />
<TranslateTransform
X="0"
Y="0" />
</TransformGroup>
</Ellipse.RenderTransform>
</Ellipse>
</Grid>
</Popup>
<CheckBox
HorizontalAlignment="Left"
Margin="8,116,0,0"
VerticalAlignment="Top"
Width="155.42"
Content="Taskbar Icon Visible"
x:Name="iconVisibility"
IsChecked="True" />
<ListBox
HorizontalAlignment="Left"
Margin="8,171,0,0"
Width="123"
IsSynchronizedWithCurrentItem="True"
Height="51"
VerticalAlignment="Top"
x:Name="iconList"
SelectedIndex="0">
<Image
Width="16"
Margin="0,4,0,0"
Height="16"
Source="Icons\Inactive.ico" />
<Image
Width="16"
Margin="0,4,0,0"
Height="16"
Source="Icons\Error.ico" />
</ListBox>
<TextBlock
HorizontalAlignment="Left"
Margin="8,149,0,0"
VerticalAlignment="Top"
Width="Auto"
Height="22"
TextWrapping="Wrap"><Run
Text="Taskbar Icon:"
Language="de-ch" /></TextBlock>
<Grid
Margin="0,0,10,24"
VerticalAlignment="Bottom"
Height="242"
Width="358"
HorizontalAlignment="Right"
x:Name="Balloons">
<Border
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderBrush="#FF000000"
BorderThickness="2,2,2,2" />
<TextBox
Margin="127,20,15,0"
x:Name="txtBalloonTitle"
VerticalAlignment="Top"
Height="23" />
<TextBox
Margin="127,65,15,97"
x:Name="txtBalloonText" />
<RadioButton
HorizontalAlignment="Left"
Margin="14,0,0,54"
VerticalAlignment="Bottom"
Width="111"
Height="22"
Content="Show as Info"
IsChecked="True" />
<TextBlock
HorizontalAlignment="Left"
Margin="14,20,0,0"
VerticalAlignment="Top"
Width="85"
Height="23"
TextWrapping="Wrap"><Run
Text="Balloon Title"
Language="de-ch" /></TextBlock>
<TextBlock
Margin="14,65,0,0"
VerticalAlignment="Top"
Height="23"
TextWrapping="Wrap"
HorizontalAlignment="Left"
Width="85"><Run
Text="Balloon Text"
Language="de-ch" /></TextBlock>
<RadioButton
Margin="14,0,0,32"
VerticalAlignment="Bottom"
Height="22"
Content="Show as Error"
HorizontalAlignment="Left"
Width="111" />
<RadioButton
Margin="14,0,0,10"
VerticalAlignment="Bottom"
Height="22"
Content="Show Custom Icon"
HorizontalAlignment="Left"
Width="130" />
<Button
HorizontalAlignment="Right"
Margin="0,0,21,12.52"
x:Name="showBalloonTip"
Width="120"
Content="Show Balloon Tip"
VerticalAlignment="Bottom"
Height="23" />
</Grid>
<Grid
HorizontalAlignment="Left"
Margin="12,0,0,24"
VerticalAlignment="Bottom"
Width="307"
Height="242"
x:Name="Popups">
<Border
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderBrush="#FF000000"
BorderThickness="2,2,2,2" />
<ListBox
IsSynchronizedWithCurrentItem="False"
x:Name="lstPopupTrigger"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource ActivationModes}}"
Margin="14,115,97,10"
SelectedIndex="0" />
<RadioButton
HorizontalAlignment="Left"
Margin="14,19,0,0"
VerticalAlignment="Top"
Width="Auto"
Height="22"
Content="Use simple Popup"
IsChecked="True" />
<RadioButton
Margin="14,41,97,0"
VerticalAlignment="Top"
Height="22"
Content="Use Popup with Storyboard" />
<TextBlock
Margin="14,83,106,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Height="22"><Run
Text="Display Popup on:"
Language="de-ch" /></TextBlock>
</Grid>
<Grid
Margin="0,323,10,276"
Width="358"
HorizontalAlignment="Right"
x:Name="ToolTips">
<Border
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderBrush="#FF000000"
BorderThickness="2,2,2,2" />
<RadioButton
Margin="14,38,10,42"
Content="Use custom ToolTip on MouseOver (Vista and above)" />
<RadioButton
Margin="14,60,97,0"
VerticalAlignment="Top"
Height="22"
Content="Use custom ToolTip with Storyboard" />
<RadioButton
Margin="14,16,10,0"
Content="Only use ToolTipText (no custom tooltip)"
VerticalAlignment="Top"
Height="22"
IsChecked="True" />
<TextBox
Margin="14,0,21,19"
x:Name="txtToolTipText"
VerticalAlignment="Bottom"
Height="23"
Text="Click to open tooltip" />
<TextBlock
Margin="14,0,157,52"
VerticalAlignment="Bottom"
Height="16"
TextWrapping="Wrap"><Run
Text="ToolTip Text:"
Language="de-ch" /></TextBlock>
</Grid>
<Grid
HorizontalAlignment="Left"
Margin="12,323,0,276"
Width="307"
x:Name="ContextMenus">
<Border
BorderBrush="#FF000000"
BorderThickness="2,2,2,2" />
<ListBox
Margin="10,0,128,10"
IsSynchronizedWithCurrentItem="False"
x:Name="lstMenuTrigger"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource ActivationModes}}"
Height="124"
VerticalAlignment="Bottom"
SelectedIndex="1" />
<TextBlock
Margin="10,10,110,0"
VerticalAlignment="Top"
Height="22"
TextWrapping="Wrap"><Run
Text="Trigger Context Menu:"
Language="de-ch" /></TextBlock>
</Grid>
<TextBlock
Margin="10,10,45,0"
VerticalAlignment="Top"
Height="Auto"
TextWrapping="Wrap"
FontWeight="Bold"
FontSize="14"><Run
Text="This window only contains the plumbing. In order to keep things separated, I created a few user controls for the popups and the TaskbarIcon instance."
Language="de-ch" /></TextBlock>
</Grid>
</Window>

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
@@ -19,13 +20,11 @@ namespace Sample_Project
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
//set icon in code - setting the IconSource in XAML
//works just fine but breaks the VS designer
tb.Icon = Properties.Resources.Computers;
}
private void OnClick(object sender, RoutedEventArgs e)