【问题标题】:Is there any way to copy a text from another program without Select it?有没有办法在不选择它的情况下从另一个程序复制文本?
【发布时间】:2015-06-01 05:04:55
【问题描述】:

我想从其他程序复制文本, 在这个程序中 Ctrl+a 被考虑用于其他命令,我不能使用“ SendKeys.SendWait("^a");"选择文本。

有没有办法复制那个文本?

【问题讨论】:

    标签: c# api winapi sendmessage sendkeys


    【解决方案1】:

    您可以使用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

    【讨论】:

    • 感谢您的指导。我应该研究一下你的想法。希望你的idea能解决这个问题。
    • 你能告诉我更多吗?我应该如何使用它?
    • 我无法理解如何使用这个想法。 @floatas
    • 非常感谢亲爱的@floatas。
    【解决方案2】:

    那是什么类型的编辑器,为什么不能只获取源文件?也许这会起作用: 1.将光标放在第一行的开头 2. 按 Ctrl+Shift+End 3. Ctrl+C

    或者,您可以尝试通过Windows Input Simulator library模拟键盘输入

    【讨论】:

    • 感谢您的指导。它的类名是:AfxOleControl42。 Ctrl+Shift+End 在此应用程序中不起作用。还有其他方法吗?
    • 亲爱的cyberj0g 我正在研究你的想法。谢谢你。
    • 为什么我的InputSimulator只有Equals和RefrenceEquals,没有SimulateKeyDown等方法?
    • 伪造输入通常是错误的,原因有很多。在这种特殊情况下,这是错误的,因为如果不选择文本,您将无法复制文本。这是要求之一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多