We help IT Professionals succeed at work.

text selection

f15iaf
f15iaf asked
on
I want to know how to get the current selected string of the current text no matter in which program and no matter what kind of program.
Comment
Watch Question

Commented:
you can find the active window and use a clipboard to retrieve the selected text.

WM_COPY message is one that can give you selected text from most applications (without take care of kind of active window), but using of windows clipboard can disturb other applications.

Author

Commented:
I don't want to use wm_copy and clipbord is there another way to save the selected text to other bufer

Commented:
I dont think that there is another way to retrieve window text without detect type of the window.

Author

Commented:
if i have a window there is the text how do i get the selected text without using wm_copy

Commented:
did you know window type?

Author

Commented:
look on window like notpad or something similar i use wm_gettext and em_getsel

But this function does not work on internet explorer and does not work on word and adobe acrobat reader

Commented:
sure :), because all window type has own commands. Instead, WM_COPY will work on most of them.

Author

Commented:
i don't want to change the clipboard

Author

Commented:
i don't want to change the clipboard

Author

Commented:
wm_copy also does not work on some windows where wm_gettext does not work either

Commented:
try GetWindowTextA
I think :) I dont have an example at the moment sorry.
regards
Smurff

Commented:
maybe something like :

function GetTextStuff(h: THandle; n: integer): PChar;
var
  p: PChar;
begin
  p := PChar('');
  GetWindowText(h, p, n);
  Result := p;
end;

The function GetWindowText has three parameters. The first one is the handle of the object you want the text of, the second one the PChar to put the text in and the third one is the maximum number of characters to read.

Try it?
hope this helps

Commented:
another one to try is GetDlgItemtTextA....
Experts, I believe f15iaf wants to only know the *currently selected text*... perhaps for something like a dictionary programme... highlight any text anywhere, press a hotkey and voila! the definition will come out.

Commented:
Hi Dragon,

I meaned that, and I have a dictionary that work in that way using windows clipboard.

Commented:
listening

Author

Commented:
hi getwindowtext function is the same as sending wm_gettext message but it doesn't work on some windows and the wm_copy also doesn't work on some windows.

right now i am using kebd_event ctrl+c to save the text to clipboard but is is not the solution i want i don't want to change the clipboard

Commented:
Sorry yeh Ive just read that getwindowstext doesnt work on some "delphi" programs.
Im reading about hmemcpy ill let you know
regards
you CAN always read unpassworded text with GetWindowText !
it the edit or any other control has the password char set then you can't read the text on Win NT in other applications !
what you have to do is you need to make a library that maps itself to the selected apps memoryspace
then you are able to read even the passworded text

wm_gettext message but it doesn't work on some windows
??? about this .. are you sure you got the windows handle right ?
I don't mean the forms window but the Edits window

Author

Commented:
i didn't ask to read password text how do you read selected text from internet explorer for example

Author

Commented:
and yes i got handle of edited text

Commented:
The problem is that if WM_SELTEXT/WM_COPY don't work then there is no standard way of getting the selection.  You can try & implement various solution for every major piece of software, but I don't think that's the answer you're looking for. The only other option I can think of would be some way of trying to grab a screenshot & OCR'ing that to get the text value, but Idon't like your chances of accuracy.

GL
MIke

Author

Commented:
I know many programs for example babylon you can click everywhere in the windows and the program will select the clicked word and translate it i don't think the program has picture recognition engine but still it works.
f15iaf, Babylon *does* have picture recognition... go ahead, give it a try... start Paint, type something, save it as Bitmap, then use Babylon to translate it... it will work.

In any case, perhaps this would work?
1. get current clipboard, buffer it
2. copy current selection to clipboard, if any
3. if clipboard content is not text, go to 5
4. do whatever you want with the text
5. restore the clipboard content

Author

Commented:
it is alreay done

Author

Commented:
i've checked that picture issue you are right the babylon uses the picture recognition engine

Commented:
So I guess the question is if anyone knows of a freely redistributable OCR engine?

GL
Mike

Author

Commented:
how the ocr engine gonna help in my case

Commented:
The only thing every program with displayed text have in common is that they, well, display text. Hence, logically, the only general way of getting that text is _after_ it's been displayed, which means having to OCR it to get real text, instead of just a picture of text.

GL
Mike

Author

Commented:
where can i get the ocr engine and how to use it to get the selected text

Author

Commented:
no body answered my last comment so the problem is not solved yet.
thus i'm deleting this question
Commented:
Hi f15iaf,

Since your question wasn't answered here and it contains some info that might help others i'm moving this question to PAQ and refund your points

**JGould-EE Moderator**

Explore More ContentExplore courses, solutions, and other research materials related to this topic.