在第二个屏幕上放置WPF ContextMenu [英] Place WPF ContextMenu on second screen

查看:143
本文介绍了在第二个屏幕上放置WPF ContextMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试解决以下问题:

我没有父窗口,我只需要在第二个屏幕上单击鼠标右键,即可打开WPF ContextMenu(来自本地C ++母版应用程序),该菜单实际上并没有出现在第二个屏幕上,而是出现在第一个屏幕上.

  1. 我尝试使用鼠标坐标.无法使用,因为ContextMenu将停留在第一个屏幕上.
  2. 在第二个屏幕上放置一个不可见的帮助程序窗口,将其设置为父窗口,并在相对于第二个屏幕左上角的坐标处打开ContextMenu可以正常工作.但是我不想使用助手窗口.

所以现在:如何在没有父窗口的情况下将ContextMenu放在第二个屏幕上.有什么方法可以告诉菜单使用什么屏幕?也许我可以从ContextMenu的"CustomPopupPlacementCallback"中使用一些参数?

任何帮助将不胜感激:-D

欢呼!

解决方案

来自 ContextMenu类 PlacementMode枚举(实际上似乎很有用).

此外,请记住,这实际上显示在弹出窗口中.


可能不相关,但以下链接中的 Nmaait 答案使用一些win32调用来获取鼠标的位置.也许您可以利用它来将上下文菜单放置在您想要的位置:

drop-a-window-into-another-window

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!

解决方案

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

这篇关于在第二个屏幕上放置WPF ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆