Solved

get text(word) at mouse cursor from any program

Posted on 2003-11-04
8
376 Views
Last Modified: 2010-04-05
Question: how to get text (just simple word) where the mouse cursor is, if it is possible?

For example the user moves mouse cursor anywhere in any windows application, presses some keys combination (shift+ctrl+a or other), then my application must get text at the mouse cursor. I need this to do a translation of that text or word, and possible to show a hint for that text o displaying translated message in some window on top.

I know that there is such possibility. But I don't know where to start from. If it is too complicated  plesase give some ideas where to start from. Thanks.

Marko
0
Comment
Question by:MarkoB
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:raidos
ID: 9678848
only method i can think of is some form of OCR reader of the screen...
and that isn't very simple to do...

never done it, don't know how to do it either...but theoretically it could work.

Regards
//raidos
0
 

Author Comment

by:MarkoB
ID: 9678980
If it is so difficult, maybe to limit the task, is to get the text from Microsoft native applications, in text editing (viewing) control: Word, Internet Explorer, WordPad.
It should be a way to get text at mouse cursor, I think.

I've seen such implementation in MacMillian English dictionary.
0
 
LVL 1

Accepted Solution

by:
rvicentiu earned 250 total points
ID: 9679695
there are a few ways to do it,  here's the easy way :)
this should work for anything (except some listboxes without LBS_HASSTRINGS defined)
to test this make sure the mouse is where it should be before calling the function

var text: PChar;
    hWnd: dword;
    point: tpoint;
begin
  text:=stralloc(1000);
  GetCursorPos(point);
  hwnd:=WindowFromPoint(point);
  SendMessage(hwnd, WM_GETTEXT, 1000, DWORD(@text[0]));
  if length(text)=0 then begin
    Windows.ScreenToClient(hwnd, point);
    SendMessage(hwnd, LB_GETTEXT, SendMessage(hwnd, LB_ITEMFROMPOINT, 0, MAKELPARAM(point.X, point.Y)), DWORD(@text[0]));
  end;
  strdispose(text);
end;
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Expert Comment

by:rvicentiu
ID: 9679761
oh, in case you haven't figured this out yet the maximum length of the text in my example is 1000
use WM_GETTEXTLENGTH and LB_GETTEXTLEN to get the length of the text and allocate the buffer before getting the text
in a rich edit control, if the text to be copied exceeds 64K, use either the message EM_STREAMOUT or EM_GETSELTEXT
see Win32 help for more info
0
 

Author Comment

by:MarkoB
ID: 9684756
Thanks rvicentiu,
I'll try this code soon. And what is that another not easy way? :)


0
 
LVL 1

Expert Comment

by:rvicentiu
ID: 9689662
you can install a WH_GETMESSAGE hook with SetWindowsHookEx and intercept for example the WM_MOUSEMOVE message to get the text from the window it was being sent to
0
 

Author Comment

by:MarkoB
ID: 9694398
ok. I managed to get text, but sometimes it gets too large to analyse big amounts of text, For 99% of cases I just need a single word (at mouse cursor),
the easier task would be to get just a selected text,
the only message I know is EM_GETSELTEXT, but that works fine for RichEdit,
but
how to get selected text from Internet Explorer, Outlook, or Word?
I mean without asking users to copy that to clipboard.
0
 

Author Comment

by:MarkoB
ID: 9731210
It looks that it is not possible without some OCR code. So I acceppt the answer fot the rivcentiu answer. Although I didn't managed to do, what I intended to do - to get a word at mouse cursor.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now