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

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

问题描述

我想创建一个应用程序,它获取字光标下(不仅仅是文本字段),但我找不到如何做到这一点。使用OCR为pretty努力。我见过的工作的唯一的事情就是Deskperience组件。他们支持'本土'的方式,但我他们花费了很多。现在,我想弄清楚这是什么'本土'的方式(可能在某种程度上挂钩的)。任何帮助将AP preciated。

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,推荐的方式,从一个应用程序收集信息到另一个(如果你没有自己当然有针对性的应用程序)是使用在 UI自动化的技术。
维基百科是pretty好它的更多信息:微软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

下面是一个小型控制台应用程序code,将窥探其他应用程序的UI。运行它,并在鼠标移动到不同的应用。每个应用程序都有各种UI自动化模式不同的支持。例如,有如下表现出的值模式和文本模式

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);
}

UI自动化实际上是由Internet Explorer和Firefox我的知识支持,但不是浏览器。请参阅此链接:谷歌浏览器什么时候会访问

现在,这是你的工作才刚刚开始:-),这是因为:

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.

有些应用程序不会暴露任何人,即使有适当的权限。例如,如果我正在写一个银行应用程序,我不希望你来监视我的是什么应用程序将显示:-)上。其他应用程序,如Outlook与DRM不会暴露出于同样的原因什么。

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天全站免登陆