From 2a4cf8de7ed6130533de082a87f93c874f20819e Mon Sep 17 00:00:00 2001 From: Philipp Sumi Date: Tue, 22 Sep 2009 07:48:22 +0000 Subject: [PATCH] WPF NotifyIcon -------------- FIX Dispose only deregisters OnExit event listenter if there is actually an application (not the case in WinForms environments). CHG Added dispose to WinForms sample which closes the NotifyIcon. git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@121 9f600761-6f11-4665-b6dc-0185e9171623 --- Source/Changelog.txt | 6 ++++-- Source/NotifyIconWpf/TaskbarIcon.cs | 5 ++++- Source/WindowsFormsSample/Form1.cs | 19 +++++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Source/Changelog.txt b/Source/Changelog.txt index 5230200..e63696e 100644 --- a/Source/Changelog.txt +++ b/Source/Changelog.txt @@ -12,8 +12,10 @@ FIX If a popup is opened, its window handle is now being set as the foreground This fixes an issue with certain controls being disabled on popups. (thanks Andrew Smith for pointing me in the right direction!). FIX Changed dispatcher access in order to work in WinForms scenarios, too. -FIX Corrected typo in WindowMessageSink.BalloonToolTipChanged event. Used internally - (although event is public), so users should be fine. +FIX Corrected typo in WindowMessageSink.BalloonToolTipChanged event (thanks Mertsch). + Used internally (although event is public), so this shouldn't be a breaking change. +FIX Dispose only deregisters OnExit event listenter if there is actually an + application (not the case in WinForms environments). ---------------------------------------------------------------------------- diff --git a/Source/NotifyIconWpf/TaskbarIcon.cs b/Source/NotifyIconWpf/TaskbarIcon.cs index 4d9169a..787bb1e 100644 --- a/Source/NotifyIconWpf/TaskbarIcon.cs +++ b/Source/NotifyIconWpf/TaskbarIcon.cs @@ -994,7 +994,10 @@ namespace Hardcodet.Wpf.TaskbarNotification IsDisposed = true; //deregister application event listener - Application.Current.Exit -= OnExit; + if (Application.Current != null) + { + Application.Current.Exit -= OnExit; + } //stop timers singleClickTimer.Dispose(); diff --git a/Source/WindowsFormsSample/Form1.cs b/Source/WindowsFormsSample/Form1.cs index 0689ff2..93a7e2d 100644 --- a/Source/WindowsFormsSample/Form1.cs +++ b/Source/WindowsFormsSample/Form1.cs @@ -15,6 +15,8 @@ namespace WindowsFormsSample { public partial class Form1 : Form { + private TaskbarIcon notifyIcon; + public Form1() { InitializeComponent(); @@ -23,11 +25,20 @@ namespace WindowsFormsSample protected override void OnLoad(EventArgs e) { base.OnLoad(e); - TaskbarIcon tb = new TaskbarIcon(); - tb.Icon = Resources.Led; - tb.Visibility = Visibility.Visible; + notifyIcon = new TaskbarIcon(); + notifyIcon.Icon = Resources.Led; + notifyIcon.ToolTipText = "Left-click to open popup"; + notifyIcon.Visibility = Visibility.Visible; - tb.TrayPopup = new FancyPopup(); + notifyIcon.TrayPopup = new FancyPopup(); + } + + protected override void OnClosed(EventArgs e) + { + base.OnClosed(e); + + //close the notify icon + notifyIcon.Dispose(); } } }