2009. november 25., szerda

How to launch the default web browser


Problem/Question/Abstract:

How to launch the default web browser

Answer:

procedure LaunchBrowser(URL: string);
var
  HTMLbrowser: string;
  TheRegistry: TRegistry;
  Value: string;
  L: Integer;
begin
  HTMLBrowser := '';
  TheRegistry := TRegistry.Create;
  try
    TheRegistry.Rootkey := HKEY_CLASSES_ROOT;
    if TheRegistry.OpenKey('.htm', false) then
    begin
      Value := TheRegistry.ReadString('');
      TheRegistry.CloseKey;
      if Value <> '' then
        if TheRegistry.OpenKey(Value + '\shell\open\command', false) then
        begin
          HTMLbrowser := TheRegistry.ReadString('');
          if (HTMLBrowser[1] = '"') and (Pos('"', Copy(HTMLBrowser, 2,
            Length(HTMLBrowser))) > 0) then
            HTMLbrowser := Copy(HTMLbrowser, 1, Pos('"', Copy(HTMLBrowser,
              2, Length(HTMLBrowser))) + 1)
          else
          begin
            L := 1;
            while (L <= Length(HTMLBrowser)) and (HTMLBrowser[L] <> ' ') do
              Inc(L);
            HTMLBrowser := Copy(HTMLBrowser, 1, L);
          end;
          TheRegistry.CloseKey;
        end;
    end;
  finally
    TheRegistry.Free;
    if HTMLBrowser <> '' then
      ShellExecute(0, 'open', pchar(HTMLbrowser), pchar(URL), '', SW_SHOWNORMAL)
    else
      ShellExecute(0, 'open', PChar(URL), '', '', SW_SHOWNORMAL);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése