加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:15:02 所属栏目:百科 来源:网络整理
导读:我想复制其他程序的文字, 在这个程序中,Ctrl a被认为是其他命令,我不能使用“SendKeys.SendWait(”^ a“);”选择文字. 有没有办法复制该文本? 解决方法 您可以使用 UIAComWrapper执行此操作,您将需要处理该窗口(从您尝试复制的位置)以及有关该元素的信息,您
我想复制其他程序的文字,
在这个程序中,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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读