Place WPF ContextMenu on second screen

邮差的信 提交于 2019-12-02 19:16:45

问题


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:


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



来源:https://stackoverflow.com/questions/19861015/place-wpf-contextmenu-on-second-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!