buttonautomationpressedgetstate

How to determine via UIAutomation, whether a button is pressed or not?


I want to find out whether a button is pressed or not. This seems not to be an official property of a button (not a button-style checkbox!), but seems accessible, there is the BM_GETSTATE message for example that should get the desired result.

Problem is, frequently, I dont get window-handles for my buttons (they are just part of another Toolbar, though they can be distinguihed by the AutomationElement). And I would need such a handle for the SendMessage function.

So.. is there a way for me to access that property? I know it is accessible, since I have seen it in other automation-programmes, I just dont konw how to get at it.

I am going to use C#, but any C code would be fine.

Many thanks


Solution

  • (edit: so I finally managed to make the code format properly here - just insert 4 spaces at the beginning.)

    Enjoy it, it took me quite a long time to get it to work.. but now I feel like having reached a new level. :)

    (please tell me how to make it format properly - both quote and code failed on me)

    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
    

    EDIT: Here http://www.andreas-reiff.de/2011/06/c-speicher-anderen-prozess-befullen-lassen-checken-ob-ein-button-gedruckt/ with readable code and explanations in a strange, foreign language.. code comments are english, though. hope you find it useful. Also, I would not have been able to solve this without the info here How come some controls don't have a windows handle?.