roknjohn
asked on
Retrieve text of focused control in another application, namely IE
I want to write a function to retrieve the text of the active (focused) control, even if the control belongs to another application.
The function below does that in most cases. However, it doesn't work if Internet Explorer is the foreground application and the user is in, for example, a TEXTAREA field. (Like the one that I am typing in right now.) Does that control not respond to the WM_GetText message or do you think GetFocus() is returning the wrong handle? In either case, how would I go about reading the text from this type of control within IE?
function GetActiveText: String;
const
MaxTextLen = 5000;
var
FW, FC : HWND;
ThID : DWORD;
Buf : array[0..MaxTextLen] of Char;
begin
FW := 0;
FC := 0;
FW := GetForegroundWindow;
ThID := GetWindowThreadProcessId(F W,0);
AttachThreadInput(GetCurre ntThreadId (),ThID,TR UE);
try
FC := GetFocus();
finally
AttachThreadInput(GetCurre ntThreadId (),ThID,FA LSE);
end;
SendMessage(FC,WM_GETTEXT, MaxTextLen ,Integer(@ Buf));
Result := Buf;
end;
The function below does that in most cases. However, it doesn't work if Internet Explorer is the foreground application and the user is in, for example, a TEXTAREA field. (Like the one that I am typing in right now.) Does that control not respond to the WM_GetText message or do you think GetFocus() is returning the wrong handle? In either case, how would I go about reading the text from this type of control within IE?
function GetActiveText: String;
const
MaxTextLen = 5000;
var
FW, FC : HWND;
ThID : DWORD;
Buf : array[0..MaxTextLen] of Char;
begin
FW := 0;
FC := 0;
FW := GetForegroundWindow;
ThID := GetWindowThreadProcessId(F
AttachThreadInput(GetCurre
try
FC := GetFocus();
finally
AttachThreadInput(GetCurre
end;
SendMessage(FC,WM_GETTEXT,
Result := Buf;
end;
ASKER
geoff,
can you get your method to read text from an IE TextArea control? For example, the control that I am typing this message into?
can you get your method to read text from an IE TextArea control? For example, the control that I am typing this message into?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're right. That's the first time I've come across a program in which that didn't work. It does work in the address bar though.
Geoff M.
Geoff M.
ASKER
I found this:
Peeking into Password Edit Controls & Internet Explorer - Super Password Spy++
http://www.developer.com/net/cplus/article.php/1561741
It seems to focus on password fields, but I think I can extract the information I need from this. Now, if I can only find the time. :-)
Peeking into Password Edit Controls & Internet Explorer - Super Password Spy++
http://www.developer.com/net/cplus/article.php/1561741
It seems to focus on password fields, but I think I can extract the information I need from this. Now, if I can only find the time. :-)
Yes, saw that one two....
But like i said, you need to delve into the document interface in order to get this information. This will work fine if you only intend on dealing with IE though... (won't work with anything else ;-)
Regards,
Russell
ASKER
Hmm, looks more complicated than its worth right now.
My plan was to put together a freeware spell checker that would work across applications. But, I think I'll just google for "Spell Checker" instead of "IHTMLDocument2 delphi"...lol
Every heard of "As-U-Type"?
http://www.asutype.com
My plan was to put together a freeware spell checker that would work across applications. But, I think I'll just google for "Spell Checker" instead of "IHTMLDocument2 delphi"...lol
Every heard of "As-U-Type"?
http://www.asutype.com
ASKER
I think I found a real gem! The trail version works cool as heck! It works on IE & Outlook, just like I wanted to do. Only $29.95 (US) and I don't have to learn IHTMLDocument2!!
PS. You can disable it for certain apps, like Delphi.
Thanks Russell, your information solved my curousity.
PS. You can disable it for certain apps, like Delphi.
Thanks Russell, your information solved my curousity.
function GetWindowTextEx(win : HWND) : string;
var
s : array [1..513] of char;
i : integer;
res : DWORD;
begin
Result := '';
SendMessageTimeout(win, WM_GETTEXT, 512, Integer(@s[1]), 0, 1000, res);
i := 1;
while (i < 513) and (s[i] <> #0) do begin
Result := Result + s[i];
Inc(i);
end;
end;
It could be improved but it works.
Geoff M.