unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, ActiveX, MSHTML, SHDocVw, StdCtrls;
type
TObjectFromLResult = function(LRESULT: lResult; const IID: TIID;
WPARAM: wParam; out pObject): HRESULT; stdcall;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): HRESULT;
function WB_GetHTMLCode(WebBrowser: iwebbrowser2; ACode: TStrings): Boolean;
implementation
{$R *.dfm}
function GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): HRESULT;
var
hInst: HWND;
lRes: Cardinal;
MSG: Integer;
pDoc: IHTMLDocument2;
ObjectFromLresult: TObjectFromLresult;
begin
hInst := LoadLibrary('Oleacc.dll');
@ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
if @ObjectFromLresult <> nil then begin
try
MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');
SendMessageTimeOut(WHandle, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
Result := ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc);
if Result = S_OK then
(pDoc.parentWindow as IServiceprovider).QueryService(IWebbrowserApp, IWebbrowser2, IE);
finally
FreeLibrary(hInst);
end;
end;
end;
function WB_GetHTMLCode(WebBrowser: iwebbrowser2; ACode: TStrings): Boolean;
var
ps: IPersistStreamInit;
ss: TStringStream;
sa: IStream;
s: string;
begin
ps := WebBrowser.Document as IPersistStreamInit;
s := '';
ss := TStringStream.Create(s);
try
sa := TStreamAdapter.Create(ss, soReference) as IStream;
Result := Succeeded(ps.Save(sa, True));
if Result then ACode.Add(ss.Datastring);
finally
ss.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
hIE: THandle;
IE: iwebbrowser2;
begin
hIE:=FindWindow('IEFrame',nil);
hIE:=FindWindowEx(hIE,0,'Frame Tab',nil);
hIE:=FindWindowEx(hIE,0,'TabWindowClass',nil);
hIE:=FindWindowEx(hIE,0,'Shell DocObject View',nil);
hIE:=FindWindowEx(hIE,0,'Internet Explorer_Server',nil);
if hIE <> 0 then
begin
GetIEFromHWnd(hIE, IE);
Memo1.Clear;
WB_GetHTMLCode(IE, Memo1.Lines);
end;
end;
end.
Nenhum comentário:
Postar um comentário