From 16ef216a4c2bc64028b9fa64c0c8d23c2f79398d Mon Sep 17 00:00:00 2001 From: Philipp Sumi Date: Sun, 17 Nov 2013 23:10:18 +0000 Subject: [PATCH] FIX Activating context menu itself rather than messagesink window, in order for the menu to receive keyboard input (NTFY-3). git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@184 9f600761-6f11-4665-b6dc-0185e9171623 --- Source/NotifyIconWpf/TaskbarIcon.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Source/NotifyIconWpf/TaskbarIcon.cs b/Source/NotifyIconWpf/TaskbarIcon.cs index ec7eee2..c227d62 100644 --- a/Source/NotifyIconWpf/TaskbarIcon.cs +++ b/Source/NotifyIconWpf/TaskbarIcon.cs @@ -703,9 +703,22 @@ namespace Hardcodet.Wpf.TaskbarNotification ContextMenu.IsOpen = true; - //activate the message window to track deactivation - otherwise, the context menu - //does not close if the user clicks somewhere else - WinApi.SetForegroundWindow(messageSink.MessageWindowHandle); + IntPtr handle = IntPtr.Zero; + + //try to get a handle on the context itself + HwndSource source = (HwndSource)PresentationSource.FromVisual(ContextMenu); + if (source != null) + { + handle = source.Handle; + } + + //if we don't have a handle for the popup, fall back to the message sink + if (handle == IntPtr.Zero) handle = messageSink.MessageWindowHandle; + + //activate the context menu or the message window to track deactivation - otherwise, the context menu + //does not close if the user clicks somewhere else. With the message window + //fallback, the context menu can't receive keyboard events - should not happen though + WinApi.SetForegroundWindow(handle); //bubble event RaiseTrayContextMenuOpenEvent();