ボタンが押されているかどうかにかかわらず、uiautomationを介して決定する方法は?

StackOverflow https://stackoverflow.com/questions/6347244

質問

ボタンが押されているかどうかを知りたいです。これは、ボタンの公式プロパティではないようです(ボタンスタイルのチェックボックスではありません!)がアクセスできるようです。たとえば、bm_getStateメッセージがあり、目的の結果が得られるはずです。

問題は、多くの場合、ボタンのウィンドウハンドルを取得しないことです(それらは別のツールバーの一部にすぎませんが、自動化によって区別される可能性があります)。そして、私はsendmessage関数のためにそのようなハンドルが必要です。

それで..私がそのプロパティにアクセスする方法はありますか?私はそれがアクセス可能であることを知っています、私は他の自動化プログラムでそれを見たので、私はそれをどのように取得するかをKONWではありません。

C#を使用しますが、Cコードはすべて問題ありません。

どうもありがとう

役に立ちましたか?

解決

(編集:だから私はついにここでコード形式を適切に作成することができました - 最初に4つのスペースを挿入するだけです。)

それを楽しんでください、それを機能させるのにかなり長い時間がかかりました。しかし今、私は新しいレベルに達したように感じます。 :)

(適切にフォーマットする方法を教えてください - 引用とコードの両方が私に失敗しました)

int res;
#region direct method
int hwnd = ae.Current.NativeWindowHandle;
if (hwnd != 0)
{
    const UInt32 BM_GETSTATE = 0x00F2;
    res = SendMessage(hwnd, BM_GETSTATE, 0, 0);
}
#endregion
else
#region method via toolbar
{
    AutomationElement parent = TreeWalker.RawViewWalker.GetParent(ae);
    while ((parent != null) && (parent.Current.ControlType != ControlType.ToolBar))
        parent = TreeWalker.RawViewWalker.GetParent(ae);
    if (parent != null)
    {
        int toolBarHandle = parent.Current.NativeWindowHandle;
        #region defines
        const int WM_USER = 0x400;
        const int TB_GETSTATE = (WM_USER + 18);
        const int TB_GETBUTTON = (WM_USER + 23);
        const int TB_BUTTONCOUNT = (WM_USER + 24);
        #endregion

        #region get correct child number
        int numButtons = SendMessage(toolBarHandle, TB_BUTTONCOUNT, 0, 0);
        AutomationElement sibling = ae;
        int cnt = -1;
        while (sibling != null)
        {
            sibling = TreeWalker.RawViewWalker.GetPreviousSibling(sibling);
            ++cnt;
        }
        if (cnt >= numButtons)
            cnt = 0; // nonsense value, but pass a valid one
        #endregion

        #region get command id
        TBBUTTON butInfo = new TBBUTTON();
        butInfo.idCommand = 1234;
        uint pid;
        GetWindowThreadProcessId((IntPtr)toolBarHandle, out pid);
        IntPtr process = OpenProcess(ProcessAccessFlags.VMOperation | ProcessAccessFlags.VMRead |
        ProcessAccessFlags.VMWrite | ProcessAccessFlags.QueryInformation, false, pid);

        IntPtr p = VirtualAllocEx(process, IntPtr.Zero, (uint)Marshal.SizeOf(typeof(TBBUTTON)), AllocationType.Commit
                                     , MemoryProtection.ReadWrite);

        int _res = SendMessage(toolBarHandle, TB_GETBUTTON, cnt, p.ToInt32());

        #region getresult
        int read;
        IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TBBUTTON)));
        Marshal.StructureToPtr(butInfo, ptr, true);
        bool __res = ReadProcessMemory(process, p, ptr, Marshal.SizeOf(typeof(TBBUTTON)), out read);
        System.Diagnostics.Debug.Assert(read == Marshal.SizeOf(typeof(TBBUTTON)));
        butInfo = (TBBUTTON)Marshal.PtrToStructure(ptr, typeof(TBBUTTON));
        #endregion

        int commandId = butInfo.idCommand;
        VirtualFreeEx(process, p, 0, FreeType.Release);

        #endregion

        //!define BST_UNCHECKED      0
        //!define BST_CHECKED        1
        //!define BST_INDETERMINATE  2
        //!define BST_PUSHED         4
        //!define BST_FOCUS          8

        #region get state
        res = SendMessage(toolBarHandle, TB_GETSTATE, commandId, 0);
        #endregion
    }
}
#endregion

編集:こちら http://www.andreas-reiff.de/2011/06/c-speicher-anderen-prozess-befullen-lassen-ob-hein-button-gedruckt/ 読みやすいコードと奇妙な外国語の説明があります。ただし、コードコメントは英語です。あなたがそれが役に立つと思うことを願っています。また、私はここの情報なしでこれを解決することができなかったでしょう どうしていくつかのコントロールにWindowsのハンドルがないのですか?.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top