【问题标题】:c# is checkbox checked in another applicationc# 在另一个应用程序中选中了复选框
【发布时间】:2016-02-21 14:31:03
【问题描述】:

我正在编写一个应用程序来连接其他两个现有应用程序。 我是 C# 新手,所以这对我来说是一个很大的挑战。 我目前尚未找到答案的问题是是否选中了复选框。 我试图使用 UIAutomation,但我不知道如何让它工作。 当我使用 UISpy 检查复选框时,它表明该复选框是一个窗格。 经过 2 多天的大量搜索后,我无法找到如何将复选框的信息作为窗格获取。 我在想 pInvoke 可以解决问题,但我也没有运气。 这是我尝试过的:

var ischecked = NativeMethods.SendMessage(variables.allCNumbers[29].Hwnd,BM_GETSTATE, IntPtr.Zero, IntPtr.Zero);
MessageBox.Show(variables.allCNumbers[29].Hwnd.ToString()); // This has a value
MessageBox.Show(ischecked.ToString()); // This always shows 0 whether the checkbox is checked or not

这是我尝试过的 UIAutomation:

    AutomationElement rootElement = AutomationElement.RootElement;

            Automation.Condition condition = new PropertyCondition(AutomationElement.ClassNameProperty,"TMainForm_ihm" );
        AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);
        AutomationElement workspace = appElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Workspace"));

        AutomationElement card = workspace.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Card"));

        AutomationElement pagecontrol = card.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TRzPageControl"));
        AutomationElement cardnumber = pagecontrol.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Card number"));

        if(cardnumber != null)
        {
            Automation.Condition usecardCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "25232366");
            AutomationElement usecard = cardnumber.FindFirst(TreeScope.Children, usecardCondition);

            MessageBox.Show("usecard: " + usecard.Current.ControlType.ProgrammaticName); // This returns "ControlType.Pane"

            //TogglePattern tp1 = usecard.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern; <- This throws an error: An unhandled exception of type 'System.InvalidOperationException' occurred in UIAutomationClient.dll  Additional information: Unsupported Pattern.
            //MessageBox.Show(tp1.Current.ToggleState.ToString());
        }

非常感谢任何帮助。

【问题讨论】:

  • 没有合适的 API 可用吗?如果我能在我的代码库中找到它,我会觉得这段代码很有趣。
  • 没有可用的 API。就像我说的那样,我对这一切都很陌生。我敢肯定“有趣”并不完全是一种恭维,但我也不认为这是一种侮辱。我真的很想学习正确的做事方式,但在这一点上,我只是想让事情顺利进行。在过去的几周里,我学到了很多东西(我在 9 月份才开始使用 C#),并且自从发现我错了之后,我重写了很多东西。
  • 这并不是侮辱。通常情况下,数据是通过 API 间接提供的(应用程序被扩展,或者另一个应用程序被用来检索这些数据)。这样,UI 仍然可以更改原始应用程序,但您的应用程序仍然可以工作。我不确定这个其他应用程序是如何工作的,所以恐怕我无法根据您提供的信息向您提供更多详细信息。
  • 试试 Winapi。 .. 检查此链接:bytes.com/topic/net/answers/…
  • Windows 控件不支持自动化框架,所以试试我上面发给你的链接

标签: c# user32 microsoft-ui-automation


【解决方案1】:

我以这篇帖子 https://bytes.com/topic/net/answers/637107-how-find-out-if-check-box-checked 为灵感:

using Accessibility;

[DllImport("oleacc.dll", PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern object AccessibleObjectFromWindow(IntPtr hwnd, uint dwId, ref Guid riid);

public static Nullable<bool> isCheckBoxChecked(IntPtr checkBoxHandle)
{
    const UInt32 OBJID_CLIENT = 0xFFFFFFFC;
    const int UNCHECKED       = 1048576;
    const int CHECKED         = 1048592;
    Guid uid = new Guid("618736e0-3c3d-11cf-810c-00aa00389b71");

    IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(checkBoxHandle, OBJID_CLIENT, ref uid);
    object o2 = accObj.get_accState(0);
    if ((int)o2 == UNCHECKED)
    {
        return false;
    }
    else if ((int)o2 == CHECKED)
    {
        return true;
    }
    else
    {
        return null;
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多