Place WPF ContextMenu on second screen

2019-03-06 05:53发布

I'm currently trying to solve an issue as follows:

I have no parent window, i just right click on the second screen which opens a WPF ContextMenu (from a native C++ mother app) which actually does not appear on the second screen but on the first.

  1. I tried using the mouse coordinates. does not work because ContextMenu will stay on the first screen.
  2. Placing an invisible helper window on the second screen, setting this as a parent and opening the ContextMenu at the coordintes relative to the second screens upper left corner works fine. But i don't want to use the helper window.

So now: How can i put the ContextMenu on the second screen without a parent window. Is there some way to tell the menu what screen to use? Maybe there are some Parameters i can use from the the "CustomPopupPlacementCallback" in the ContextMenu?

Any help would be appreciated :-D

cheers!

1条回答
劳资没心,怎么记你
2楼-- · 2019-03-06 06:22

From msdn:

When the ContextMenu is assigned to the FrameworkElement.ContextMenu or FrameworkContentElement.ContextMenu property, the ContextMenuService changes this value of this property when the ContextMenu opens. If the user opens the ContextMenu by using the mouse, Placement is set to MousePoint. If the user opens the ContextMenu by using the keyboard, Placement is set to Center. If you want to change the position of the ContextMenu, set the ContextMenuService.Placement property on the FrameworkElement or FrameworkContentElement.

Also from MSDN

You can position a ContextMenu by setting the PlacementTarget, PlacementRectangle, Placement, HorizontalOffset, and VerticalOffsetProperty properties. These properties behave the same as they do for a Popup. For more information, see Popup Placement Behavior.

More reading: ContextMenu class, PlacementMode Enumeration (actually seems useful).

Also, remember that this is actually shown inside a Popup.


Might not be relevant, but Nmaait's answer in the following link uses some win32 calls to get the position of the mouse. Maybe you can leverage this in order to get your context menu where you want it to be:

drop-a-window-into-another-window

查看更多
登录 后发表回答