使用 UI 自动化以编程方式打开上下文菜单?
我正在尝试使用 UI 自动化实现右键单击上下文菜单。由于 UI 自动化没有本机右键单击模式,因此我将 ExpandCollapse 提供程序添加到列表视图的 AutomationPeer 类中,并将展开和折叠映射到打开和关闭上下文菜单。
我的问题是,是否有更好的方法来调用上下文菜单,而无需尝试使用私有构造函数实例化类?我无法将 SendKeys 与 Shift-F10 一起使用。我想使用 PopupControlService 但它被标记为内部。
我糟糕的解决方法:
public class MyListViewAutomationPeer : ListViewAutomationPeer, IExpandCollapseProvider
{
public MyListViewAutomationPeer(MyListView owner)
: base(owner){}
public override object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.ExpandCollapse)
{
return this;
}
return base.GetPattern(patternInterface);
}
public void Expand()
{
MyListView owner = (MyListView)Owner;
//**********************
//Ouch!!! What a hack
//**********************
//ContextMenuEventArgs is a sealed class, with private constructors
//Instantiate it anyway ...
ContextMenuEventArgs cmea = (ContextMenuEventArgs)FormatterServices.GetUninitializedObject(typeof(ContextMenuEventArgs));
cmea.RoutedEvent = MyListView.ContextMenuOpeningEvent;
cmea.Source = owner;
//This will fire any developer code that is bound to the OpenContextMenuEvent
owner.RaiseEvent(cmea);
//The context menu didn't open because this is a hack, so force it open
owner.ContextMenu.Placement = PlacementMode.Center;
owner.ContextMenu.PlacementTarget = (UIElement)owner;
owner.ContextMenu.IsOpen = true;
}
I'm trying to implement a right click context menu using UI automation. Since UI automation does not have a native right click pattern I am adding an ExpandCollapse provider to the listview's AutomationPeer class and mapping the expand and collapse to opening and closing the context menu.
My question, is there a better method of invoking the context menu that doesn't involve trying to instantiate a class with a private constructor? I can't use SendKeys with Shift-F10. I'd like to use the PopupControlService but that is tagged as internal.
My awful workaround:
public class MyListViewAutomationPeer : ListViewAutomationPeer, IExpandCollapseProvider
{
public MyListViewAutomationPeer(MyListView owner)
: base(owner){}
public override object GetPattern(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.ExpandCollapse)
{
return this;
}
return base.GetPattern(patternInterface);
}
public void Expand()
{
MyListView owner = (MyListView)Owner;
//**********************
//Ouch!!! What a hack
//**********************
//ContextMenuEventArgs is a sealed class, with private constructors
//Instantiate it anyway ...
ContextMenuEventArgs cmea = (ContextMenuEventArgs)FormatterServices.GetUninitializedObject(typeof(ContextMenuEventArgs));
cmea.RoutedEvent = MyListView.ContextMenuOpeningEvent;
cmea.Source = owner;
//This will fire any developer code that is bound to the OpenContextMenuEvent
owner.RaiseEvent(cmea);
//The context menu didn't open because this is a hack, so force it open
owner.ContextMenu.Placement = PlacementMode.Center;
owner.ContextMenu.PlacementTarget = (UIElement)owner;
owner.ContextMenu.IsOpen = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也正在努力解决同样的问题。作为解决方法,我使用 mouse_event 函数使用 user32.dll 并在获取可点击区域的 X、Y 坐标后执行右键单击。
这不是一个好方法,因为屏幕的 X 、 Y 坐标随屏幕分辨率的变化而变化。
I too am struggling with the same issue. As a work around i am using the mouse_event function using user32.dll and performing the right click after getting the X, Y coordinates of the clickable area.
This is not a good method as the X , Y coordinates of screen vary with the screen resolution.
您已经有了一个好的开始,但是您应该直接使用 Activator 来创建 ContextMenuEventArgs 类。 (您所拥有的是使用内部使用反射的东西。您应该直接使用它。)
此外,为了完整性,您应该在实际打开菜单之前检查
Handled
属性,以使其与预期一致行为。我在下面说明了这两件事。
我更进一步,创建了一个可以调用的全局函数,该函数简化了上述非公共构造函数的调用。这是该功能...
这是如何使用它...
What you have is a good start, but you should use Activator directly to create the
ContextMenuEventArgs
class. (What you have is using something that internally uses the reflection. You should just use it directly.)Also, for completeness, you should check the
Handled
property before actually opening the menu to be consistent with the expected behavior.I've illustrated both things below.
I took it a step further and created a global function I can call that simplifies the above calling of non-public constructors. Here's the function...
And here's how to use it...