2
votes

I have been trying to add a context menu to a notify icon (system tray icon). This is for my own custom WPF wrapper around the native Shell_NotifyIcon function.

I have been using the following code to show a context menu:

contextMenu.Placement = PlacementMode.AbsolutePoint;
contextMenu.HorizontalOffset = args.X;
contextMenu.VerticalOffset = args.Y;
contextMenu.IsOpen = true;

The args.X and args.Y are the absolute screen coordinates of the mouse at the time the notify icon was clicked (I have verified that it contains the correct mouse position).

For some reason, the context menu shows up about 5-7 pixels up and to the left of the mouse position:

enter image description here

The context menus of the other icons on the system tray display exactly where the mouse is.

I searched all the available properties of the ContextMenu and nothing seemed like it would fix it. Am I missing something? I could always manually adjust by 7 or so pixels but if there is a better way I'd rather do whatever that way may be.

1
My first guess would be that there is a default margin on ContextMenu. Have you tried explicitly setting the margin to 0? - Max Hampton
Yes, I already have tried setting the Margin to 0. No difference. :( - Bradley Odell

1 Answers

1
votes

I'm not sure why it behaves that way with AbsolutePoint. But if you want to place the context menu at the tip of the mouse pointer, have you tried

contextMenu.Placement = PlacementMode.MousePoint;
contextMenu.HorizontalOffset = 0;
contextMenu.VerticalOffset = 0;

MSDN (https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.placementmode%28v=vs.110%29.aspx) explains that MousePoint will position the context menu "...relative to the tip of the mouse cursor and at an offset...".