如何使用Hook制作字典 [英] how to make dictionary with using the Hook

查看:81
本文介绍了如何使用Hook制作字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个字典来翻译鼠标移动的单词,我正在使用C#和SQL服务器。请帮助我如何操作以及如何使用Hook。

解决方案

我想给你一些非常可靠的理由,为什么你应该放弃这个问题,避免重大挫折和巨大的时间浪费。在我解释之后,我将向您解释解决方案的所有主要思想,原则上不能完整。然后你可以自己决定。



由于第一部分是最重要的部分,我先解释一下。首先,根据Microsoft文档,您无法在.NET中创建全局挂钩;您需要在本机DLL中设置钩子。更重要的是,问题不能保证原则上的一般解决方案。我会解释原因。那么,为什么我认为你不应该打扰?您看,有许多开发人员,包括许多CodeProject成员,他们清楚地了解所涉及的所有想法和技术。然而,我相信他们中的大多数人都会否认做这种工作的想法。为什么?因为从客户的角度来看,与解决方案的可怜的好处相比,所需的努力非常高,不成比例地高。那么,我们如何才能为某人讨论这项工作的可行性,甚至不知道这个想法,就像你自己一样?大多数用户宁愿使用一些可用的工具复制一些更大的文本并翻译一次,而不是弄乱鼠标。即使翻译质量目前太低,最好还是用鼠标一个接一个地抓单词。



更糟糕的是,一般的100%保证解决方案是不可能。为什么?因为很多内容被人类用作文本,从Window API的角度来看并不是真正的文本。为什么?由于此文本未知,Windows API将仅显示图形。 Windows API只能使用 GetText API函数读取Windowed控件。此外,不使用Windows API在图形区域内的技术在传统Windows API中占据越来越多的地位。最广泛使用的技术是例如WPF,SVG和Flash。从Windows API的角度来看,使用这些技术显示的文本内容是纯图形,而不是文本。当然,人们可以使用OCR,但......这就是为什么我说100%保证解决方案是不可能的,而不仅仅是不可能。



现在,什么可以做到?首先,您可以创建一个全局Windows挂钩。您可以阅读所有相关内容: http:// msdn .microsoft.com / zh-CN / library / windows / desktop / ms632589%28v = vs.85%29.aspx [ ^ ]。



你需要的钩子类型是全局钩子。因此,您需要一些针对本机操作系统而非.NET的技术来将钩子安装在本机(非托管)DLL中。您的代码的很大一部分可能是.NET,但您仍然需要使用该DLL与您的应用程序进行通信。



现在,您可以在鼠标所在的屏幕(我不知道你需要利用什么鼠标事件,这取决于你)。在该位置,您需要找到顶级弹出窗口并在其句柄上调用 GetText 。这是最困难的部分。在很多情况下,根据我上面解释的原因,它不会给你任何文字。如果你得到一些文字,你很难找到它是哪个单词(由于快速回答中难以解释的原因);相反,您将获得与控件关联的整个文本。您可以尝试翻译所有内容。 (我假设你的目标不是进行语义句翻译,这在全球范围内是一个难题,并没有真正解决。我宁愿假设你只想找到每个单词的所有词典条目。)



查找字典中的单词是一个单独的问题。只有一本字典是不够的。要提供合理的性能,您需要创建一个功能强大的索引系统。我的意思是冗余。您必须消化整个字典以开发适合快速搜索的索引数据量。我会更多地向你解释,但这完全取决于我们尚未开始讨论的许多细节。



我的目标不是劝阻你,而是在这种情况下,我无法做得更好。我宁愿提请你注意,你可以真正接触到更多有用且更可行的问题。



-SA

I want to make a dictionary to translate words with the mouse movement , I am working with C# and SQL server . Please help me how i do it and how use the Hook .

解决方案

I want to give you some very solid reasons why you ought to give up this problem, to avoid major frustrations and enormous waste of time. After I explain that, I'm going to explain you all the main ideas of the solution, which cannot be complete in principle. Then you can decide by yourself.

As the first part is the most important, I'll explain it first. First of all, according to Microsoft documentation, you cannot create global hooks in .NET; you will need to set the hook in a native DLL. More importantly, the problem cannot guarantee general solution in principle. I'll explain why. So, why do I think you should not bother? You see, there are many developers, including many CodeProject members, who clearly understand all the ideas and the pieces of technologies involved. And yet, I'm sure most of them will deny the idea of doing such work. Why? Because the required effort is really high, disproportionally high, when compared with the miserable benefit of the solution from the customer point of view. So, how can we discuss the feasibility of this work for someone how doesn't even know the idea, like yourself? Instead of messing with the mouse, most users will rather end up copying some bigger text and translating it once using some available tools; even though the quality of translation is presently too low, it's better then catching separate words one by one with the mouse.

What's worse, the general 100% guaranteed solution is impossible. Why? Because, a lot of content which is used as text by human being, is not really the text from the standpoint of the Window API. Why? Because this text is unknown, the Windows API will see only the graphics. Only the Windowed controls could be read by the Windows API using the GetText API function. Moreover, the technologies which don't use Windows API "inside the graphics area" dominate more and more over the legacy Windows API. Most widely used technologies are, for example, WPF, SVG and Flash. From the standpoint of Windows API, the textual content displayed using these technology is pure graphics, not text. Of course, one can use OCR, but… this is exactly why I said "100% guaranteed solution is impossible", not just "impossible".

Now, what can be done? First, you could create a global Windows hook. You can read all about it: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^].

The kind of hooks you would need are global hooks. So, you would need some technology targeting native OS, not .NET, to install the hook in a native (unmanaged) DLL. The big part of your code could be .NET, but you still need to use that DLL to communicate with your application.

Now, you can locate a place on the screen where the mouse is (I don't know what mouse event you would need to leverage, it's up to you). At that location, you would need to find the top-level popup and call GetText on its handle. This is the most difficult part. In many case, it won't give you any text, by the reasons I explained above. If you get some text, you hardly can locate which word it was (by the reasons which is difficult to explain in a Quick Answer); rather, you will get the whole text associated with the control. You can try to translate it all. (I assume you are not aiming to do semantic sentence translation, this is a difficult problem on a global level, not really solved. I would rather assume you would like to merely find all dictionary entries for each of the words.)

Finding the words in a dictionary is a separate problem. It's not enough just to have a dictionary. To deliver reasonable performance, you will need to create a powerful indexing system. I means redundancy. You have to digest the whole dictionary to develop the volumes of indexed data suitable for fast search. I would explain you more on that, but it all depends on many detail we are not yet started to discuss.

It wasn't my goal to discourage you, but I just cannot do any better in this case. I would rather bring to your attention that there are many much more useful and more feasible problem you could really approach.

—SA


这篇关于如何使用Hook制作字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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