编程方式打开快捷菜单使用UI自动化? [英] Programmatically open context menu using UI automation?
问题描述
我想实现使用UI自动化右键上下文菜单。由于UI自动化不具有本地右键单击模式我加入了一个ExpandCollapse提供商ListView的类的AutomationPeer和测绘展开和折叠来打开和关闭的上下文菜单。
我的问题,是有调用,不涉及试图实例化一个类与私有构造函数的上下文菜单的一个更好的办法?我不能用使用Shift-F10的SendKeys。我想用PopupControlService但被标记为内部。
我的可怕的解决方法:
公共类MyListViewAutomationPeer:ListViewAutomationPeer,IExpandCollapseProvider
{ 公共MyListViewAutomationPeer(MyListView所有者)
:基地(所有者){} 公众覆盖对象GetPattern(PatternInterface patternInterface)
{
如果(patternInterface == PatternInterface.ExpandCollapse)
{
返回此;
}
返回base.GetPattern(patternInterface);
} 公共无效展开()
{
MyListView所有者=(MyListView)所有者; // **********************
//哎哟!!!什么是黑客
// ********************** // ContextMenuEventArgs是一个密封类,具有私有构造函数
//实例也无妨...
ContextMenuEventArgs CMEA =(ContextMenuEventArgs)FormatterServices.GetUninitializedObject(typeof运算(ContextMenuEventArgs));
cmea.RoutedEvent = MyListView.ContextMenuOpeningEvent;
cmea.Source =所有者; //这将激活绑定到OpenContextMenuEvent任何开发人员code
owner.RaiseEvent(经互会); //上下文菜单没有打开,因为这是一个黑客,所以迫使它开放
owner.ContextMenu.Placement = PlacementMode.Center;
owner.ContextMenu.PlacementTarget =(的UIElement)业主;
owner.ContextMenu.IsOpen = TRUE; }
我也是用了同样的问题挣扎。由于周围的工作我使用mouse_event函数使用user32.dll中并获得在X后进行点击右键,Y坐标可点击区域。
这是不是一个好方法,在X,Y屏幕坐标屏幕分辨率不同而不同。
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;
}
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.
这篇关于编程方式打开快捷菜单使用UI自动化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!