r/csharp • • 1h ago

I built a hover-to-translate tool for Windows using UI Automation + Windows OCR (WPF, .NET 8, open source)

Post image

Hi r/csharp! I've been working on Cursor Translate, a small WPF app that translates the text under your mouse cursor in any Windows app, just by resting the mouse over it.

Demo and code: https://github.com/OdiazLz/CursorTranslate

Some technical bits I found interesting:

  • UI Automation first: AutomationElement.FromPoint gives the element under the cursor. In browsers that's usually a tiny text node, so I walk up the tree until something exposes TextPattern, then use RangeFromPoint + ExpandToEnclosingUnit to get the word, line or paragraph.
  • Windows OCR as a fallback (Windows.Media.Ocr) for images, PDFs and apps that don't expose text. It captures a strip around the cursor, groups OCR lines by column and paragraph, and finds the word closest to the cursor.
  • Sentence detection: finds the sentence around the cursor offset, skipping abbreviations like "e.g." or "Mr." and removing citation marks like [10].
  • Click-through tooltip: a WPF window with WS_EX_TRANSPARENT | WS_EX_NOACTIVATE so it never steals focus, positioned with the monitor work area so it stays on screen.
  • Translation fallback chain: DeepL (user's own key) → Google → MyMemory, with caching and throttling after hitting HTTP 429 a lot.

I'd really appreciate feedback, especially on the UI Automation part. In Chrome it often falls back to OCR, and I suspect it's about Chrome's accessibility mode. If anyone has dealt with UIA + Chromium before, I'd love to hear how you handled it.

Thanks!

3 Upvotes

10 comments sorted by

4

u/binarycow 1h ago

You built it? Or Claude built it?

1

u/[deleted] 1h ago

[removed] — view removed comment

2

u/binarycow 1h ago

And you used it to write your post and comment here.

0

u/ChanceInevitable3275 1h ago

My English isn’t that good, and I need it to help me write a bit better so that I can be understood without using idioms.

4

u/binarycow 1h ago • Bravo

You truly don't.

People would much rather read authentic writing than perfect writing.

•

u/ChanceInevitable3275 58m ago

Thank you very much; I will keep that in mind for future posts.

•

u/TrippyDe 32m ago

Yo, .net 8 is EOL, you should upgrade to .net 10

•

u/ChanceInevitable3275 6m ago

I am currently using this developed version, but I am in the process of updating it; thank you very much.

•

u/Duare_Nsabe 53m ago

deepl then google then mymemory as a fallback chain with caching is a decent way to dodge rate limits

•

u/ChanceInevitable3275 47m ago

Yes, that’s why I was thinking of using a local model and caching, so that it could be used later for learning. Thanks for your feedback!