5

UI オートメーションを使用して右クリック コンテキスト メニューを実装しようとしています。UI オートメーションにはネイティブの右クリック パターンがないため、ExpandCollapse プロバイダーをリストビューの AutomationPeer クラスに追加し、展開と折りたたみをコンテキスト メニューの開閉にマッピングします。

私の質問は、プライベート コンストラクターを使用してクラスをインスタンス化しようとすることを伴わない、コンテキスト メニューを呼び出すためのより良い方法はありますか? Shift-F10 で SendKeys を使用できません。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;

    }
4

2 に答える 2

0

私も同じ問題で苦労しています。回避策として、user32.dll を使用して mouse_event 関数を使用し、クリック可能な領域の X、Y 座標を取得した後に右クリックを実行しています。

画面の X 座標、Y 座標は画面の解像度によって異なるため、これは適切な方法ではありません。

于 2010-02-20T07:25:03.590 に答える