微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 有没有办法从其他程序中复制文本而不选择它?

我想复制其他程序的文字,
在这个程序中,Ctrl a被认为是其他命令,我不能使用“SendKeys.SendWait(”^ a“);”选择文字.

有没有办法复制该文本?

解决方法

您可以使用 UIAComWrapper执行此操作,您将需要处理该窗口(从您尝试复制的位置)以及有关该元素的信息,您可以从 UIAutomationVerify获得.

var elementCollection = AutomationElement.FromHandle(windowHandle).FindAll(TreeScope.Subtree,Condition.TrueCondition);
foreach (var item in elementCollection)
{
   //check item properties if element is the one you looking for
}

此外,您可以提供更复杂的过滤器来获取一个元素,而不是Condition.TrueCondition.

编辑,添加真实示例:

[DllImport("user32.dll",SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
const string InternetExplorerClass = "IEFrame";
static void Main()
{
    var windowHandle = new IntPtr(0);

    //Find internet explorer instance
    windowHandle = FindWindow(InternetExplorerClass,null);

    if (!windowHandle.Equals(IntPtr.Zero))
    {
        //create filter to improve search speed
        var localizedControlType = new PropertyCondition(
            AutomationElement.LocalizedControlTypeProperty,"tab item");

        //get all elements in internet explorer that match our filter
        var elementCollection =
            AutomationElement.FromHandle(windowHandle)
                .FindAll(TreeScope.Subtree,localizedControlType);

        //iterate through search results
        foreach (AutomationElement item in elementCollection)
        {
            Console.WriteLine(item.Current.Name);
        }
    }
    else
    {
        Console.WriteLine("Internet explorer not found");
    }

    Console.ReadLine();
}

上面的代码将找到Internet Explorer,并将所有选项卡标题打印到控制台.我把源代码放到了GitHub.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐