如何在Windows中获取光标下的单词? [英] How to get the word under the cursor in Windows?

查看:26
本文介绍了如何在Windows中获取光标下的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个应用程序来获取光标下的单词(不仅用于文本字段),但我找不到如何做到这一点.使用 OCR 非常困难.我见过的唯一工作是 Deskperience 组件.他们支持本地"方式,但我他们花了很多钱.现在我试图弄清楚这种本地"方式是什么(也许是某种方式的挂钩).任何帮助将不胜感激.

I want to create a application which gets the word under the cursor (not only for text fields), but I can't find how to do that. Using OCR is pretty hard. The only thing I've seen working is the Deskperience components. They support a 'native' way, but I they cost a lot. Now I'm trying to figure out what is this 'native' way (maybe somehow of hooking). Any help will be appreciated.

我找到了一种方法,但它只获取控件的整个文本.知道如何从整个文本中只获取光标下的单词吗?

I found a way, but it gets only the whole text of the control. Any idea how to get only the word under the cursor from the whole text?

推荐答案

在最新版本的 Windows 上,从一个应用程序收集信息到另一个应用程序(当然,如果您没有目标应用程序)的推荐方法是使用用户界面自动化技术.维基百科提供了更多关于这方面的信息:Microsoft UI 自动化

On recent versions of Windows, the recommended way to gather information from one application to another (if you don't own the targeted application of course) is to use the UI Automation technology. Wikipedia is pretty good for more information on this: Microsoft UI Automation

基本上,UI 自动化将使用所有必要的手段来收集可以收集的内容

Basically, UI automation will use all necessary means to gather what can be gathered

这是一个小的控制台应用程序代码,它将监视其他应用程序的 UI.运行它并将鼠标移到不同的应用程序上.每个应用程序对各种UI 自动化模式"都有不同的支持.例如,这里展示了 Value 模式和 Text 模式.

Here is a small console application code that will spy the UI of other apps. Run it and move the mouse over to different applications. Each application has a different support for various "UI automation patterns". For example, there is the Value pattern and the Text pattern as demonstrated here.

static void Main(string[] args)
{
    do
    {
        System.Drawing.Point mouse = System.Windows.Forms.Cursor.Position; // use Windows forms mouse code instead of WPF
        AutomationElement element = AutomationElement.FromPoint(new System.Windows.Point(mouse.X, mouse.Y));
        if (element == null)
        {
            // no element under mouse
            return;
        }

        Console.WriteLine("Element at position " + mouse + " is '" + element.Current.Name + "'");

        object pattern;
        // the "Value" pattern is supported by many application (including IE & FF)
        if (element.TryGetCurrentPattern(ValuePattern.Pattern, out pattern))
        {
            ValuePattern valuePattern = (ValuePattern)pattern;
            Console.WriteLine(" Value=" + valuePattern.Current.Value);
        }

        // the "Text" pattern is supported by some applications (including Notepad)and returns the current selection for example
        if (element.TryGetCurrentPattern(TextPattern.Pattern, out pattern))
        {
            TextPattern textPattern = (TextPattern)pattern;
            foreach(TextPatternRange range in textPattern.GetSelection())
            {
                Console.WriteLine(" SelectionRange=" + range.GetText(-1));
            }
        }
        Thread.Sleep(1000);
        Console.WriteLine(); Console.WriteLine();
    }
    while (true);
}

Internet Explorer 和 Firefox 实际上支持 UI 自动化,但据我所知 Chrome 不支持.请参阅此链接:何时可以访问 Google Chrome?

UI automation is actually supported by Internet Explorer and Firefox, but not by Chrome to my knowledge. See this link: When will Google Chrome be accessible?

现在,这只是您工作的开始:-),因为:

Now, this is just the beginning of work for you :-), because:

  • 大多数时候,所有这些都具有严重的安全隐患.使用此技术(或直接的 Windows 技术,例如 WindowFromPoint)将需要足够的权限(例如作为管理员).而且我认为 DExperience 没有办法克服这些限制,除非他们在计算机上安装内核驱动程序.

  • Most of the time, all this has heavy security implication. Using this technology (or direct Windows technology such as WindowFromPoint) will require sufficient rights to do so (such as being an administrator). And I don't think DExperience has any way to overcome these limitations, unless they install a kernel driver on the computer.

有些应用程序不会向任何人公开任何内容,即使拥有适当的权限.例如,如果我正在编写银行应用程序,我不希望您窥探我的应用程序将显示的内容:-).出于同样的原因,其他应用程序(例如带有 DRM 的 Outlook)不会公开任何内容.

Some applications will not expose anything to anyone, even with proper rights. For example, if I'm writing a banking application, I don't want you to spy on what my application will display :-). Other applications such as Outlook with DRM will not expose anything for the same reasons.

只有 UI 自动化文本模式支持才能提供比整个文本更多的信息(如单词).唉,IE 和 FF 都不支持这种特定模式,即使它们全局支持 UI 自动化.

Only the UI automation Text pattern support can give more information (like the word) than just the whole text. Alas, this specific pattern is not supported by IE nor FF even if they support UI automation globally.

因此,如果所有这些都不适合您,您将不得不深入研究并使用 OCR 或形状识别技术.即便如此,在某些情况下,您将根本无法执行此操作(因为担保权).

So, if all this does not work for you, you will have to dive deeper and use OCR or Shape recognition techniques. Even with this, there will be some cases where you won't be able to do it at all (because of security rights).

这篇关于如何在Windows中获取光标下的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆