Solved

get text(word) at mouse cursor from any program

Posted on 2003-11-04
8
363 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

15 Experts available now in Live!

Get 1:1 Help Now