Как взять URL из окна IE?

Материал из DRKB


Как получить активный URL из браузера?[править | править код]

uses windows, ddeman;

function Get_URL(Servicio: string): String;
var
  Cliente_DDE: TDDEClientConv;
  temp: PChar;
begin
  Result := '';
  Cliente_DDE := TDDEClientConv.Create(nil);
  with Cliente_DDE do
  begin
    SetLink(Servicio, 'WWW_GetWindowInfo');
    temp := RequestData('0xFFFFFFFF');
    Result := StrPas(temp);
    StrDispose(temp);  // <<-Предотвращаем утечку памяти
    CloseLink;
  end;
  Cliente_DDE.Free;
end;

procedure TForm1.Button1Click(Sender);
begin
  ShowMessage(Get_URL('Netscape'));
  // или
  ShowMessage(Get_URL('IExplore'));
end;


Author: Song
Source: Vingrad.ru http://forum.vingrad.ru
ID: 03501
ID: 03508



Пример показывает, как найти окно Internet Explorer, и захватить из него текущий URL, находящийся в поле адреса IE. В Исходнике используются простые функции win32 api на delphi.

function GetText(WindowHandle: hwnd): string;
var
  TxtLength: Integer;
  buffer: string;
begin
  TxtLength := SendMessage(WindowHandle, WM_GETTEXTLENGTH, 0, 0);
  TxtLength := TxtLength + 1;
  SetLength(buffer, TxtLength);
  SendMessage(WindowHandle, wm_gettext, TxtLength, LongInt(@buffer[1]));
  Result := buffer;
end;

function GetURL: string;
var
  ie, toolbar, combo, comboboxex, edit: hwnd;
  worker, toolbarwindow: hwnd;
begin
  ie := FindWindow(PChar('IEFrame'), nil);
  worker := FindWindowEx(ie, 0, 'WorkerA', nil);
  toolbar := FindWindowEx(worker, 0, 'rebarwindow32', nil);
  comboboxex := FindWindowEx(toolbar, 0, 'comboboxex32', nil);
  combo := FindWindowEx(comboboxex, 0, 'ComboBox', nil);
  edit := FindWindowEx(combo, 0, 'Edit', nil);
  toolbarwindow := FindWindowEx(comboboxex, 0, 'toolbarwindow32', nil);

  Result := GetText(edit);
{-------------------------------------------------------}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetURL);
end;


Source: http://forum.sources.ru
ID: 03502