delphi使用TWebBrowser而不使用TForm

时间:2014-02-05 07:03:20

标签: delphi twebbrowser

我想在delphi中开发一个COM DLL,它将在内部创建一个窗口或窗体,然后在其上显示TWebBrowser导航。这样做的原因是我不想使用TWebbrowser控件来拖动我的每个客户端应用程序。这个客户端应用程序只是使用这个DLL,因为这个DLL也有一些其他相关的逻辑,这里不提及。 请帮我解决这个问题

1 个答案:

答案 0 :(得分:3)

你应该留意其他海报的预订,但如果你想要一个启动TWebBrowser的dll,这应该让你开始。它编译并运行,但只进行了非常简单的测试。

希望有所帮助。

library BrowserDLL;

uses
  ShareMem,
  SysUtils,
  Classes,
  Forms,
  Windows,
  DLLMainForm in 'DLLMainForm.pas' {MainForm};

{$R *.RES}

function ShowBrowserForm(AHandle: THandle; const AURL : String): Longint; stdcall;
begin
  Application.Handle := AHandle;
  result := TMainForm.ShowForm(AURL);
end;


exports
  ShowBrowserForm;

var
  DLLApplication : TApplication;


procedure DLLHandler(Reason: Integer);
begin
  case Reason of
    DLL_PROCESS_DETACH:
       begin
         Application := DLLApplication;
       end;
  end;
end;

begin
  DLLApplication := Application;
  DLLproc:=@DLLHandler;
end.

unit DLLMainForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  OleCtrls, SHDocVw, ExtCtrls;

type
  TMainForm = class(TForm) 
    wb1: TWebBrowser;
  private 
    FURL: string;
    procedure SetUrl(const Value: string);
  public
    class function ShowForm(const AURL: String): Longint;
    property URL : string read FURL write SetUrl;
  end;

implementation

{$R *.DFM}

{ TBrowserForm }

procedure TMainForm.SetUrl(const Value: string);
begin
  if FURL <> Value then begin
    FURL := Value;
    wb1.Navigate(Value);
  end; 
end;

class function TMainForm.ShowForm(const AURL : String): Longint;
var
  form: TMainForm;
begin
  form := Create(Application);
  try
    form.URL := AURL;
    form.ShowModal;
    Result := LongInt(form);
  finally
    FreeAndNil(form);
  end;
end;

end.

unit LauncherMainform;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw; 

type

  TShowDllForm = function(AHandle : THandle; const AUrl : String) : LongInt; stdcall;

  TMainForm = class(TForm)
    edt1: TEdit;
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    LibHandle : THandle;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.btn1Click(Sender: TObject);
var
  DLLProc : TShowDllForm;
begin
  LibHandle := LoadLibrary(PChar('BrowserDLL.dll'));
  if LibHandle <> 0 then  begin
    @DLLProc := GetProcAddress(LibHandle,'ShowBrowserForm');
    if (@DLLProc <> nil) then try
      DLLProc(Application.Handle, edt1.Text);
    except
      on E:Exception do
        ShowMessage('Error Running dll.' + #13#10 + E.Message);
    end;
  end else
    ShowMessage('Error Loading dll');
end;

end.