Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2006 Jul : Grabbing text from other programs' windows
| Subject: | Grabbing text from other programs' windows |
| Posted by: | "DP" (askaquesti..@rogers.com) |
| Date: | Wed, 12 Jul 2006 03:55:09 |
Some web pages won't let you print them easily (I can explain if you don't
believe me), and you can't even select the text and press Ctrl-C to copy the
text (not sure if this feature is caused by Flash or Javascript). The best
that you can do is press PRTSC and paste the bitmap into another application
for printing. This is absurd. So by doing some web research I was able to
tweak existing code and create a program that fetches the text from the
windows of programs that are running on the desktop. It works for some
applications but not for others. For example, if you have a NOTEPAD.EXE
running, it will grab the text from inside the notepad window (even if the
text isn't visible because it requires scrolling down!) and let you view it
using my program. However for MS Internet Explorer (also from Firefox) it
doesn't grab the text from the window. I don't understand. Why do some
programs return the text but others do not? Here's the code. All you need is
a single TButton and a single TTreeView on the Form to make this work.
Don
=======================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, comctrls;
type
TForm1 = class(TForm)
Button1: TButton;
TreeView1: TTreeView;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type
PWindows = ^TWindows;
TWindows = record
WindowHandle: HWND;
WindowText: string;
end;
var
PNode, CNode: TTreeNode;
AWindows: PWindows;
function EnumChildWindowsProc(Wnd: HWnd; Form: TForm1): Bool; export;
{$ifdef Win32} stdcall; {$endif}
var
Buffer: array[0..99] of Char;
Text: array[0..999] of Char;
begin
GetWindowText(Wnd, Buffer, 100);
SendMessage(Wnd, WM_GETTEXT, SizeOf(Text), integer(@Text)) ;
//if StrLen(Buffer) 0 then
if StrPas(Buffer) = '' then Buffer := 'Empty';
new(AWindows);
with AWindows^ do
begin
WindowHandle := Wnd;
WindowText := StrPas(Buffer)+'@'+strpas(text);
end;
CNode := Form1.TreeView1.Items.AddChildObject(PNode,
AWindows^.WindowText + ':' +
IntToHex(AWindows^.WindowHandle, 8), AWindows);
if GetWindow(Wnd, GW_CHILD) = 0 then
begin
PNode := CNode;
Enumchildwindows(Wnd, @EnumChildWindowsProc, 0);
end;
Result := True;
end;
function EnumWindowsProc(Wnd: HWnd; Form: TForm1): Bool;
export; {$ifdef Win32} stdcall; {$endif}
var
Buffer: array[0..99] of Char;
Text: array[0..999] of Char;
begin
GetWindowText(Wnd, Buffer, 100);
SendMessage(Wnd, WM_GETTEXT, SizeOf(Text), integer(@Text)) ;
//if StrLen(Buffer) 0 then
if StrPas(Buffer) = '' then Buffer := 'Empty';
new(AWindows);
with AWindows^ do
begin
WindowHandle := Wnd;
WindowText := StrPas(Buffer)+'@'+strpas(text);
end;
PNode := Form1.TreeView1.Items.AddObject(nil, AWindows^.WindowText + ':' +
IntToHex(AWindows^.WindowHandle, 8), AWindows);
EnumChildWindows(Wnd, @EnumChildWindowsProc, 0);
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc, Longint(Self));
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Dispose(AWindows);
end;
end.