WPF NotifyIcon : Public Release

-------------------------------
CHG   Common cleanup, copyright etc.
DEL   Removed unused dependency property change handlers.
ADD   Added ResetBalloonCloseTimer method which keeps custom balloons open.
ADD   Provided implementation for attached BalloonClosingEvent.

git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@94 9f600761-6f11-4665-b6dc-0185e9171623
This commit is contained in:
Philipp Sumi
2009-05-04 19:36:22 +00:00
parent 2b05ff7bd7
commit 98a0017687
15 changed files with 568 additions and 135 deletions

View File

@@ -1,7 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// hardcodet.net NotifyIcon for WPF
// Copyright (c) 2009 Philipp Sumi
// Contact and Information: http://www.hardcodet.net
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Code Project Open License (CPOL);
// either version 1.0 of the License, or (at your option) any later
// version.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE
namespace Hardcodet.Wpf.TaskbarNotification
{
@@ -27,4 +46,4 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// </summary>
Error
}
}
}

View File

@@ -1,4 +1,6 @@
namespace Hardcodet.Wpf.TaskbarNotification.Interop
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
/// <summary>
/// Event flags for clicked events.

View File

@@ -1,4 +1,4 @@
using System;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{

View File

@@ -0,0 +1,172 @@
// Interop code taken from Mike Marshall's AnyForm
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Hardcodet.Wpf.TaskbarNotification.Interop
{
/// <summary>
/// Resolves the current tray position.
/// </summary>
public static class TrayInfo
{
[DllImport("user32.dll")]
private static extern Int32 GetWindowLong(IntPtr hWnd, Int32 Offset);
public static Point GetTrayLocation()
{
var info = new AppBarInfo();
info.GetSystemTaskBarPosition();
Rectangle rcWorkArea = info.WorkArea;
int x = 0, y = 0;
if (info.Edge == AppBarInfo.ScreenEdge.Left)
{
x = rcWorkArea.Left + 2;
y = rcWorkArea.Bottom;
}
else if (info.Edge == AppBarInfo.ScreenEdge.Bottom)
{
x = rcWorkArea.Right;
y = rcWorkArea.Bottom;
}
else if (info.Edge == AppBarInfo.ScreenEdge.Top)
{
x = rcWorkArea.Right;
y = rcWorkArea.Top;
}
else if (info.Edge == AppBarInfo.ScreenEdge.Right)
{
x = rcWorkArea.Right;
y = rcWorkArea.Bottom;
}
return new Point { X = x, Y = y};
}
}
internal class AppBarInfo
{
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("shell32.dll")]
private static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA data);
[DllImport("user32.dll")]
private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam,
IntPtr pvParam, UInt32 fWinIni);
private const int ABE_BOTTOM = 3;
private const int ABE_LEFT = 0;
private const int ABE_RIGHT = 2;
private const int ABE_TOP = 1;
private const int ABM_GETTASKBARPOS = 0x00000005;
// SystemParametersInfo constants
private const UInt32 SPI_GETWORKAREA = 0x0030;
private APPBARDATA m_data;
public ScreenEdge Edge
{
get { return (ScreenEdge) m_data.uEdge; }
}
public Rectangle WorkArea
{
get
{
Int32 bResult = 0;
var rc = new RECT();
IntPtr rawRect = Marshal.AllocHGlobal(Marshal.SizeOf(rc));
bResult = SystemParametersInfo(SPI_GETWORKAREA, 0, rawRect, 0);
rc = (RECT) Marshal.PtrToStructure(rawRect, rc.GetType());
if (bResult == 1)
{
Marshal.FreeHGlobal(rawRect);
return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}
return new Rectangle(0, 0, 0, 0);
}
}
public void GetPosition(string strClassName, string strWindowName)
{
m_data = new APPBARDATA();
m_data.cbSize = (UInt32) Marshal.SizeOf(m_data.GetType());
IntPtr hWnd = FindWindow(strClassName, strWindowName);
if (hWnd != IntPtr.Zero)
{
UInt32 uResult = SHAppBarMessage(ABM_GETTASKBARPOS, ref m_data);
if (uResult != 1)
{
throw new Exception("Failed to communicate with the given AppBar");
}
}
else
{
throw new Exception("Failed to find an AppBar that matched the given criteria");
}
}
public void GetSystemTaskBarPosition()
{
GetPosition("Shell_TrayWnd", null);
}
public enum ScreenEdge
{
Undefined = -1,
Left = ABE_LEFT,
Top = ABE_TOP,
Right = ABE_RIGHT,
Bottom = ABE_BOTTOM
}
[StructLayout(LayoutKind.Sequential)]
private struct APPBARDATA
{
public UInt32 cbSize;
public IntPtr hWnd;
public UInt32 uCallbackMessage;
public UInt32 uEdge;
public RECT rc;
public Int32 lParam;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
}
}
}

View File

@@ -1,4 +1,29 @@
using System;
// hardcodet.net NotifyIcon for WPF
// Copyright (c) 2009 Philipp Sumi
// Contact and Information: http://www.hardcodet.net
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Code Project Open License (CPOL);
// either version 1.0 of the License, or (at your option) any later
// version.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE
using System;
using System.ComponentModel;
using System.Diagnostics;

View File

@@ -40,14 +40,18 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Compile Include="BalloonIcon.cs" />
<Compile Include="Interop\AnyForm.cs" />
<Compile Include="Interop\TrayInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Interop\Point.cs" />
<Compile Include="Interop\WindowClass.cs" />
<Compile Include="PopupActivationMode.cs" />

View File

@@ -1,4 +1,28 @@
namespace Hardcodet.Wpf.TaskbarNotification
// hardcodet.net NotifyIcon for WPF
// Copyright (c) 2009 Philipp Sumi
// Contact and Information: http://www.hardcodet.net
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Code Project Open License (CPOL);
// either version 1.0 of the License, or (at your option) any later
// version.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE
namespace Hardcodet.Wpf.TaskbarNotification
{
/// <summary>
/// Defines flags that define when a popup

View File

@@ -1,6 +1,4 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Markup;
@@ -8,16 +6,16 @@ using System.Windows.Markup;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("NotifyIconWpf")]
[assembly: AssemblyDescription("NotifyIcon Implementation for WPF.")]
[assembly: AssemblyTitle("NotifyIcon for WPF")]
[assembly: AssemblyDescription("NotifyIcon Implementation for the WPF platform.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("hardcodet.net")]
[assembly: AssemblyProduct("NotifyIconWpf")]
[assembly: AssemblyProduct("NotifyIcon WPF")]
[assembly: AssemblyCopyright("Copyright © Philipp Sumi 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
//provides simplified declaration
[assembly: XmlnsDefinition("http://www.hardcodet.net/taskbar", "Hardcodet.Wpf.TaskbarNotification")]
// Setting ComVisible to false makes the types in this assembly not visible

View File

@@ -1,4 +1,29 @@
using System;
// hardcodet.net NotifyIcon for WPF
// Copyright (c) 2009 Philipp Sumi
// Contact and Information: http://www.hardcodet.net
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Code Project Open License (CPOL);
// either version 1.0 of the License, or (at your option) any later
// version.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
@@ -33,6 +58,11 @@ namespace Hardcodet.Wpf.TaskbarNotification
= DependencyProperty.RegisterReadOnly("TrayPopupResolved", typeof(Popup), typeof(TaskbarIcon),
new FrameworkPropertyMetadata(null));
/// <summary>
/// A read-only dependency property that returns the <see cref="Popup"/>
/// that is being displayed in the taskbar area based on a user action.
/// </summary>
public static readonly DependencyProperty TrayPopupResolvedProperty
= TrayPopupResolvedPropertyKey.DependencyProperty;
@@ -70,6 +100,11 @@ namespace Hardcodet.Wpf.TaskbarNotification
= DependencyProperty.RegisterReadOnly("TrayToolTipResolved", typeof(ToolTip), typeof(TaskbarIcon),
new FrameworkPropertyMetadata(null ));
/// <summary>
/// A read-only dependency property that returns the <see cref="ToolTip"/>
/// that is being displayed.
/// </summary>
public static readonly DependencyProperty TrayToolTipResolvedProperty
= TrayToolTipResolvedPropertyKey.DependencyProperty;
@@ -336,7 +371,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
if (e.OldValue != null)
{
//remove the taskbar icon reference from the previously used element
SetParentTaskbarIcon((DependencyObject) e.OldValue, this);
SetParentTaskbarIcon((DependencyObject) e.OldValue, null);
}
@@ -407,7 +442,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
if (e.OldValue != null)
{
//remove the taskbar icon reference from the previously used element
SetParentTaskbarIcon((DependencyObject)e.OldValue, this);
SetParentTaskbarIcon((DependencyObject)e.OldValue, null);
}
@@ -434,7 +469,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
DependencyProperty.Register("MenuActivation",
typeof (PopupActivationMode),
typeof (TaskbarIcon),
new FrameworkPropertyMetadata(PopupActivationMode.RightClick, MenuActivationPropertyChanged));
new FrameworkPropertyMetadata(PopupActivationMode.RightClick));
/// <summary>
/// A property wrapper for the <see cref="MenuActivationProperty"/>
@@ -450,34 +485,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
set { SetValue(MenuActivationProperty, value); }
}
/// <summary>
/// A static callback listener which is being invoked if the
/// <see cref="MenuActivationProperty"/> dependency property has
/// been changed. Invokes the <see cref="OnMenuActivationPropertyChanged"/>
/// 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 MenuActivationPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TaskbarIcon owner = (TaskbarIcon) d;
owner.OnMenuActivationPropertyChanged(e);
}
/// <summary>
/// Handles changes of the <see cref="MenuActivationProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="MenuActivation"/> 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 OnMenuActivationPropertyChanged(DependencyPropertyChangedEventArgs e)
{
//currently not needed
}
#endregion
#region PopupActivation dependency property
@@ -490,7 +497,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
DependencyProperty.Register("PopupActivation",
typeof (PopupActivationMode),
typeof (TaskbarIcon),
new FrameworkPropertyMetadata(PopupActivationMode.LeftClick, PopupActivationPropertyChanged));
new FrameworkPropertyMetadata(PopupActivationMode.LeftClick));
/// <summary>
/// A property wrapper for the <see cref="PopupActivationProperty"/>
@@ -506,34 +513,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
set { SetValue(PopupActivationProperty, value); }
}
/// <summary>
/// A static callback listener which is being invoked if the
/// <see cref="PopupActivationProperty"/> dependency property has
/// been changed. Invokes the <see cref="OnPopupActivationPropertyChanged"/>
/// 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 PopupActivationPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TaskbarIcon owner = (TaskbarIcon) d;
owner.OnPopupActivationPropertyChanged(e);
}
/// <summary>
/// Handles changes of the <see cref="PopupActivationProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="PopupActivation"/> 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 OnPopupActivationPropertyChanged(DependencyPropertyChangedEventArgs e)
{
//currently not needed
}
#endregion
@@ -726,21 +705,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
/// <summary>
/// Executes a given command.
/// </summary>
/// <param name="command">The command to be executed.</param>
/// <param name="commandParameter">An optional parameter that was associated with
/// the command.</param>
private static void RunCommand(ICommand command, object commandParameter)
{
if (command == null) return;
if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
//EVENTS
@@ -769,7 +733,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
{
//first raise event, then command
RoutedEventArgs args = RaiseTrayLeftMouseDownEvent(this);
RunCommand(LeftClickCommand, LeftClickCommandParameter);
LeftClickCommand.ExecuteIfEnabled(LeftClickCommandParameter);
return args;
}
@@ -1019,7 +983,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
protected RoutedEventArgs RaiseTrayMouseDoubleClickEvent()
{
RoutedEventArgs args = RaiseTrayMouseDoubleClickEvent(this);
RunCommand(DoubleClickCommand, DoubleClickCommandParameter);
DoubleClickCommand.ExecuteIfEnabled(DoubleClickCommandParameter);
return args;
}
@@ -1685,21 +1649,66 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// A static helper method to raise the BalloonShowing event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
internal static RoutedEventArgs RaiseBalloonShowingEvent(DependencyObject target)
/// <param name="source">The <see cref="TaskbarIcon"/> instance that manages the balloon.</param>
internal static RoutedEventArgs RaiseBalloonShowingEvent(DependencyObject target, TaskbarIcon source)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = BalloonShowingEvent;
RoutedEventArgs args = new RoutedEventArgs(BalloonShowingEvent, source);
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
#region BalloonClosing
/// <summary>
/// BalloonClosing Attached Routed Event
/// </summary>
public static readonly RoutedEvent BalloonClosingEvent = EventManager.RegisterRoutedEvent("BalloonClosing",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TaskbarIcon));
/// <summary>
/// Adds a handler for the BalloonClosing attached event
/// </summary>
/// <param name="element">UIElement or ContentElement that listens to the event</param>
/// <param name="handler">Event handler to be added</param>
public static void AddBalloonClosingHandler(DependencyObject element, RoutedEventHandler handler)
{
RoutedEventHelper.AddHandler(element, BalloonClosingEvent, handler);
}
/// <summary>
/// Removes a handler for the BalloonClosing attached event
/// </summary>
/// <param name="element">UIElement or ContentElement that listens to the event</param>
/// <param name="handler">Event handler to be removed</param>
public static void RemoveBalloonClosingHandler(DependencyObject element, RoutedEventHandler handler)
{
RoutedEventHelper.RemoveHandler(element, BalloonClosingEvent, handler);
}
/// <summary>
/// A static helper method to raise the BalloonClosing event on a target element.
/// </summary>
/// <param name="target">UIElement or ContentElement on which to raise the event</param>
/// <param name="source">The <see cref="TaskbarIcon"/> instance that manages the balloon.</param>
internal static RoutedEventArgs RaiseBalloonClosingEvent(DependencyObject target, TaskbarIcon source)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs(BalloonClosingEvent, source);
RoutedEventHelper.RaiseEvent(target, args);
return args;
}
#endregion
//ATTACHED PROPERTIES
//TODO put into use
#region ParentTaskbarIcon
/// <summary>
@@ -1729,9 +1738,6 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
//BASE CLASS PROPERTY OVERRIDES
/// <summary>

View File

@@ -1,4 +1,28 @@
using System;
// hardcodet.net NotifyIcon for WPF
// Copyright (c) 2009 Philipp Sumi
// Contact and Information: http://www.hardcodet.net
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Code Project Open License (CPOL);
// either version 1.0 of the License, or (at your option) any later
// version.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
@@ -6,6 +30,7 @@ using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using Hardcodet.Wpf.TaskbarNotification.Interop;
using Point=Hardcodet.Wpf.TaskbarNotification.Interop.Point;
@@ -122,6 +147,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
#endregion
#region Custom Balloons
/// <summary>
/// Shows a custom control as a tooltip in the tray location.
/// </summary>
@@ -133,6 +160,13 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// is a null reference.</exception>
public void ShowCustomBalloon(UIElement balloon, PopupAnimation animation, int? timeout)
{
if (!Application.Current.Dispatcher.CheckAccess())
{
var action = new Action(() => ShowCustomBalloon(balloon, animation, timeout));
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, action);
return;
}
if (balloon == null) throw new ArgumentNullException("balloon");
if (timeout.HasValue && timeout < 500)
{
@@ -184,7 +218,7 @@ namespace Hardcodet.Wpf.TaskbarNotification
SetParentTaskbarIcon(balloon, this);
//fire attached event
RaiseBalloonShowingEvent(balloon);
RaiseBalloonShowingEvent(balloon, this);
//display item
popup.IsOpen = true;
@@ -200,12 +234,39 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// <summary>
/// Closes the current <see cref="CustomBalloon"/>, if it's set.
/// Resets the closing timeout, which effectively
/// keeps a displayed balloon message open until
/// it is either closed programmatically through
/// <see cref="CloseBalloon"/> or due to a new
/// message being displayed.
/// </summary>
public void ResetBalloonCloseTimer()
{
if (IsDisposed) return;
lock (this)
{
//reset timer in any case
balloonCloseTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
}
/// <summary>
/// Closes the current <see cref="CustomBalloon"/>, if the
/// property is set.
/// </summary>
public void CloseBalloon()
{
if (IsDisposed) return;
if (!Application.Current.Dispatcher.CheckAccess())
{
Action action = CloseBalloon;
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, action);
return;
}
lock (this)
{
//reset timer in any case
@@ -215,13 +276,23 @@ namespace Hardcodet.Wpf.TaskbarNotification
Popup popup = CustomBalloon;
if (popup != null)
{
//if a balloon message is already displayed, close it immediately
popup.IsOpen = false;
//reset attached property
UIElement element = popup.Child;
if (element != null) SetParentTaskbarIcon(element, null);
//announce closing
RoutedEventArgs eventArgs = RaiseBalloonClosingEvent(element, this);
if (!eventArgs.Handled)
{
//if the event was handled, clear the reference to the popup,
//but don't close it - the handling code has to manage this stuff now
//close the popup
popup.IsOpen = false;
//reset attached property
if (element != null) SetParentTaskbarIcon(element, null);
}
//remove custom balloon anyway
SetCustomBalloon(null);
}
}
@@ -241,6 +312,9 @@ namespace Hardcodet.Wpf.TaskbarNotification
Application.Current.Dispatcher.Invoke(action);
}
#endregion
#region Process Incoming Mouse Events
@@ -405,7 +479,8 @@ namespace Hardcodet.Wpf.TaskbarNotification
tt.Placement = PlacementMode.Mouse;
//do *not* set the placement target, as it causes the popup to become hidden if the
//TaskbarIcon's parent is hidden, too.
//TaskbarIcon's parent is hidden, too. At runtime, the parent can be resolved through
//the ParentTaskbarIcon attached dependency property:
//tt.PlacementTarget = this;
//the tooltip (and implicitly its context) explicitly gets
@@ -478,13 +553,10 @@ namespace Hardcodet.Wpf.TaskbarNotification
/// property which prevents this issue.</remarks>
private void CreatePopup()
{
//no popup is available
if (TrayPopup == null) return;
//check if the item itself is a popup
Popup popup = TrayPopup as Popup;
if (popup == null)
if (popup == null && TrayPopup != null)
{
//create an invisible popup that hosts the UIElement
popup = new Popup();
@@ -501,8 +573,9 @@ namespace Hardcodet.Wpf.TaskbarNotification
Popup.CreateRootPopup(popup, TrayPopup);
//TODO we don't really need this and it causes the popup to become hidden if the
//TaskbarIcon's parent is hidden, too.
//do *not* set the placement target, as it causes the popup to become hidden if the
//TaskbarIcon's parent is hidden, too. At runtime, the parent can be resolved through
//the ParentTaskbarIcon attached dependency property:
//popup.PlacementTarget = this;
popup.Placement = PlacementMode.AbsolutePoint;

View File

@@ -1,7 +1,33 @@
using System;
// hardcodet.net NotifyIcon for WPF
// Copyright (c) 2009 Philipp Sumi
// Contact and Information: http://www.hardcodet.net
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Code Project Open License (CPOL);
// either version 1.0 of the License, or (at your option) any later
// version.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Resources;
using Hardcodet.Wpf.TaskbarNotification.Interop;
@@ -222,5 +248,27 @@ namespace Hardcodet.Wpf.TaskbarNotification
}
#endregion
#region execute command
/// <summary>
/// Executes a given command if its <see cref="ICommand.CanExecute"/> method
/// indicates it can run.
/// </summary>
/// <param name="command">The command to be executed, or a null reference.</param>
/// <param name="commandParameter">An optional parameter that is associated with
/// the command.</param>
public static void ExecuteIfEnabled(this ICommand command, object commandParameter)
{
if (command == null) return;
if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
#endregion
}
}