Skip to content

Commit ec2e870

Browse files
authored
Implement WM_ContextMenu (#53)
Improve the usability via the keyboard. * Implement WM_CONTEXTMENU * Handle NIN_SELECT and NIN_KEYSELECT
1 parent b198c45 commit ec2e870

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/NotifyIconWpf/Interop/WindowMessageSink.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ public class WindowMessageSink : IDisposable
9494
/// </summary>
9595
public event Action<MouseEvent> MouseEventReceived;
9696

97+
/// <summary>
98+
/// Fired in case the user uses the WM_CONTEXTMENU-key
99+
/// on the taskbar icon.
100+
/// </summary>
101+
public event Action<Point> ContextMenuReceived;
102+
97103
/// <summary>
98104
/// Fired if a balloon ToolTip was either displayed
99105
/// or closed (indicated by the boolean flag).
@@ -242,8 +248,19 @@ private void ProcessWindowMessage(uint msg, IntPtr wParam, IntPtr lParam)
242248
switch (message)
243249
{
244250
case WindowsMessages.WM_CONTEXTMENU:
245-
// TODO: Handle WM_CONTEXTMENU, see https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shell_notifyiconw
246-
Debug.WriteLine("Unhandled WM_CONTEXTMENU");
251+
case WindowsMessages.NIN_SELECT:
252+
case WindowsMessages.NIN_KEYSELECT:
253+
/*
254+
* GET_X_LPARAM should be used to retrieve anchor X-coordinate, this is defined as
255+
* ((int)(short)((WORD)(((ULONG_PTR)(wParam)) & 0xffff)))
256+
* GET_Y_LPARAM should be used to retrieve anchor Y-coordinate, this is defined as
257+
* ((int)(short)((WORD)((((ULONG_PTR)(wParam)) >> 16) & 0xffff)))
258+
*/
259+
ContextMenuReceived?.Invoke(new Point()
260+
{
261+
X = (short)((nint)wParam & 0xFFFF),
262+
Y = (short)((nint)wParam >> 16 & 0xFFFF)
263+
});
247264
break;
248265

249266
case WindowsMessages.WM_MOUSEMOVE:
@@ -312,16 +329,6 @@ private void ProcessWindowMessage(uint msg, IntPtr wParam, IntPtr lParam)
312329
ChangeToolTipStateRequest?.Invoke(false);
313330
break;
314331

315-
case WindowsMessages.NIN_SELECT:
316-
// TODO: Handle NIN_SELECT see https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shell_notifyiconw
317-
Debug.WriteLine("Unhandled NIN_SELECT");
318-
break;
319-
320-
case WindowsMessages.NIN_KEYSELECT:
321-
// TODO: Handle NIN_KEYSELECT see https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shell_notifyiconw
322-
Debug.WriteLine("Unhandled NIN_KEYSELECT");
323-
break;
324-
325332
default:
326333
Debug.WriteLine("Unhandled NotifyIcon message ID: " + lParam);
327334
break;

src/NotifyIconWpf/TaskbarIcon.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public TaskbarIcon()
132132

133133
// register event listeners
134134
messageSink.MouseEventReceived += OnMouseEvent;
135+
messageSink.ContextMenuReceived += ShowContextMenu;
135136
messageSink.TaskbarCreated += OnTaskbarCreated;
136137
messageSink.ChangeToolTipStateRequest += OnToolTipChange;
137138
messageSink.BalloonToolTipChanged += OnBalloonToolTipChanged;

0 commit comments

Comments
 (0)