在 Windows 中获取鼠标光标下的单词
大家好,
我和一个朋友正在讨论一个新项目的可能性:一个翻译程序,每当你将鼠标悬停在任何控件中的任何单词上时,即使是静态的、不可编辑的单词,它也会弹出翻译。我知道有很多浏览器插件可以在网页上执行此类操作;我们正在考虑如何在系统范围内(在 Windows 上)做到这一点。
当然,关键的困难是弄清楚用户将鼠标悬停在哪个单词上。我知道 MSAA 和自动化,但据我所知,这些东西只允许您获取控件的全部内容,而不是鼠标悬停的特定单词。
我偶然发现了这个(专有)应用程序,它几乎完全符合我们想要做的事情: http://www.gettranslateit.com /
他们能够以某种方式获取用户在几乎所有应用程序中悬停的确切单词(在某些应用程序中似乎遇到问题,特别是 Windows 资源管理器)。它甚至以某种方式从明显自定义绘制的控件中抓取文本。一开始我以为一定是用OCR。但即使当我将字体缩小到文本变成完全无法阅读的斑点时,它仍然可以完美地识别单词。 (然而,如果我将字体更改为 Wingdings,它不会识别任何内容。但也许这是设计使然?)
关于它如何实现这个看似不可能的任务,有什么想法吗?
编辑:它不适用于 Wingdings,但它确实适用于其他一些无意义字体,所以我已经确认它不能是 OCR。
Greetings everyone,
A friend and I are discussing the possibility of a new project: A translation program that will pop up a translation whenever you hover over any word in any control, even static, non-editable ones. I know there are many browser plugins to do this sort of thing on webpages; we're thinking about how we would do it system-wide (on Windows).
Of course, the key difficulty is figuring out the word the user is hovering over. I'm aware of MSAA and Automation, but as far as I can tell, those things only allow you to get the entire contents of a control, not the specific word the mouse is over.
I stumbled upon this (proprietary) application that does pretty much exactly what we want to do: http://www.gettranslateit.com/
Somehow they are able to get the exact word the user is hovering over in almost any application (It seems to have trouble in a few apps, notably Windows Explorer). It even grabs text out of obviously custom-drawn controls, somehow. At first I thought it must be using OCR. But even when I shrink the font so far down that the text becomes a completely unreadable blob, it can still recognize words perfectly. (And yet, it doesn't recognize anything if I change the font to Wingdings. But maybe that's by design?)
Any ideas as to how it's achieving this seemingly impossible task?
EDIT: It doesn't work with Wingdings, but it does work with some other nonsense fonts, so I've confirmed it can't be OCR.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以捕获将文本输出到显示器的 GDI 调用,然后找出光标所在的单词的边界框。
You could capture the GDI calls that output text to the display, and then figure out which word's bounding box the cursor falls in.
好吧,对于 GDI 控件,您可以获得控件的位置和大小,通常还可以获得字体信息。例如,使用静态文本控件您可以使用WM_GETFONT。然后,一旦您获得了鼠标相对于控件位置的位置,并使用 字体函数,可能类似于 GetTextExtentPoint32 找出光标下的内容。我很确定答案就在这个方向...
您可以在其他应用程序上运行 dumpbin /imports 并查看它们正在调用哪些 API。
Well, for GDI controls you can get the position and size of the control, and you can usually get the font info. For example, with static text controls you'd use WM_GETFONT. Then once you have that you can get the position of the mouse relative to the position of the control and use one of the font functions, perhaps something like GetTextExtentPoint32 to figure out what is under the cursor. I'm pretty sure the answer lies in that direction...
You can run
dumpbin /imports
on the other application and see what APIs they are calling.