使用 UI 自动化以编程方式打开上下文菜单?

发布于 2024-08-17 01:54:14 字数 1579 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋风の叶未落 2024-08-24 01:54:14

我也正在努力解决同样的问题。作为解决方法,我使用 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.

屌丝范 2024-08-24 01:54:14

您已经有了一个好的开始,但是您应该直接使用 Activator 来创建 ContextMenuEventArgs 类。 (您所拥有的是使用内部使用反射的东西。您应该直接使用它。)

此外,为了完整性,您应该在实际打开菜单之前检查 Handled 属性,以使其与预期一致行为。

我在下面说明了这两件事。

var owner = (MyListView)Owner;

// Construct the ContextMenuEventArgs
var constructorArgs = new object[] { owner, true };

var contextMenuEventArgs = (ContextMenuEventArgs)Activator.CreateInstance(
    typeof(ContextMenuEventArgs),
    BindingFlags.Instance | BindingFlags.NonPublic,
    null,
    constructorArgs,
    null);

// Configure and use as needed
contextMenuEventArgs.RoutedEvent = FrameworkElement.ContextMenuOpeningEvent;
owner.RaiseEvent(contextMenuEventArgs);

if (contextMenuEventArgs.Handled)
    return;

owner.ContextMenu.Placement = PlacementMode.Center;
owner.ContextMenu.PlacementTarget = (UIElement)owner;
owner.ContextMenu.IsOpen = true;

我更进一步,创建了一个可以调用的全局函数,该函数简化了上述非公共构造函数的调用。这是该功能...

public static class PrivateActivator {

    public static T CreateInstance<T>(params object?[] arguments) { 

        var item = Activator.CreateInstance(
            typeof(T),
            BindingFlags.Instance | BindingFlags.NonPublic,
            null,
            arguments,
            null);

        if (item is T typedItem)
            return typedItem;
        else
            throw new Exception("Cannot create type");
    }
}

这是如何使用它...

var contextMenuEventArgs = PrivateActivator.CreateInstance<ContextMenuEventArgs>(AssociatedObject, true);

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.

var owner = (MyListView)Owner;

// Construct the ContextMenuEventArgs
var constructorArgs = new object[] { owner, true };

var contextMenuEventArgs = (ContextMenuEventArgs)Activator.CreateInstance(
    typeof(ContextMenuEventArgs),
    BindingFlags.Instance | BindingFlags.NonPublic,
    null,
    constructorArgs,
    null);

// Configure and use as needed
contextMenuEventArgs.RoutedEvent = FrameworkElement.ContextMenuOpeningEvent;
owner.RaiseEvent(contextMenuEventArgs);

if (contextMenuEventArgs.Handled)
    return;

owner.ContextMenu.Placement = PlacementMode.Center;
owner.ContextMenu.PlacementTarget = (UIElement)owner;
owner.ContextMenu.IsOpen = true;

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...

public static class PrivateActivator {

    public static T CreateInstance<T>(params object?[] arguments) { 

        var item = Activator.CreateInstance(
            typeof(T),
            BindingFlags.Instance | BindingFlags.NonPublic,
            null,
            arguments,
            null);

        if (item is T typedItem)
            return typedItem;
        else
            throw new Exception("Cannot create type");
    }
}

And here's how to use it...

var contextMenuEventArgs = PrivateActivator.CreateInstance<ContextMenuEventArgs>(AssociatedObject, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文