Re: Delphi + browser: get URL and title?

Posted by webmaster Guido on February 10, 2004

In Reply to: Delphi + browser: get URL and title? posted by Chris Ford p14300 on February 06, 2004
: How can I get the URL (location) and the title of the webpage that is displayed in the webbrowser Internet Explorer?

The following function returns the URL of the page that is displayed in Internet Explorer. If several browser windows are open, the last browser window (uppermost) is used.

function GetURL_IE: string;
var
  hIeWindow, hWorkerA, hToolbar, hComboBoxEx,
  hComboBox, hEdit: hWnd;
  Buffer: array[0..255] of Char;
begin
  hIeWindow := FindWindow('IEFrame', nil);
  if hIeWindow  0 then begin
    hWorkerA := FindWindowEx(hIeWindow, 0, 'WorkerA', nil);
    hToolbar := FindWindowEx(hWorkerA, 0, 'RebarWindow32', nil);
    hComboBoxEx := FindWindowEx(hToolbar, 0, 'ComboBoxex32', nil);
    hComboBox := FindWindowEx(hComboBoxEx, 0, 'ComboBox', nil);
    hEdit := FindWindowEx(hComboBox, 0, 'Edit', nil);
    Sendmessage (hEdit, WM_GETTEXT, Length(Buffer), Longint(@Buffer));
    Result := String(Buffer);
  end
  else
    Result := '';
end;

The following function returns the title (caption) of the Internet Explorer window. If several browser windows are open, the last one is accessed used.

function GetCaption_IE: string;
var
  hIeWindow: hWnd;
  Buffer: array[0..255] of Char;
begin
  hIeWindow := FindWindow('IEFrame', nil);
  if hIeWindow  0 then begin
    Sendmessage (hIeWindow, WM_GETTEXT, Length(Buffer), Longint(@Buffer));
    Result := String(Buffer);
  end
  else
    Result := '';
end;

To test the code:

1. Add the two functions to a form unit, in the "implementation" part.
2. Drop two labels and a button on the form.
3. Create an OnClick event handler for the button and complete it as follows:

procedure TformMain.Button1Click(Sender: TObject);
begin
  Label1.Caption := GetCaption_IE;
  Label2.Caption := GetURL_IE;
end;
 


[ Delphi Forum ]