Creating TWebBrowser in Runtime with Delphi

别等时光非礼了梦想. 提交于 2019-12-06 03:34:37

问题


I have a TWebBrowser object which is created in runtime and used in background, that is, not visible. The problem is that events like OnDocumentComplete dont work or are not triggered in Delphi2009. Any advice?

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  FWebBrowser:= TWebBrowser.Create(Self);
  FWebBrowser.RegisterAsBrowser:= True;
  FWebBrowser.OnDocumentComplete:= WhenDocIsCompleted;
end;

procedure TfrmMain.WhenDocIsCompleted(ASender: TObject; const pDisp: IDispatch;
  var URL: OleVariant);
begin
  ShowMessage('Doc is completed!');
end;

There is any difference important between Navigate and Navigate2? How can I enable cookies here?

Thanks in advance.


回答1:


TWinControl(FWebBrowser).Parent := Form1;  // Parent property is read-only unless cast



回答2:


You may have this issue because the TWebBrowser internally works closely together with the handle of the parent form to get messages posted from windows. Try using a hidden form with the TWebBrowser on (optionally run-time created as well), and/or investigate if the HandleAllocated and HandleNeeded methods could help you.




回答3:


Call for the OnDocumentComplete Problem:

WebBrowser1.HandleNeeded;

or in your case:

FWebBrowser.HandleNeeded;

before webBrowser.Navigate




回答4:


A component working perfectly with web-pages cookies is TEmbeddedWB from EmbeddedWB and is free.




回答5:


procedure TForm1.ReCreateBrowser();
begin
    if(WebBrowser <> NIL) then
    begin
       WebBrowser.Stop;
       WebBrowser.Destroy;
    end;

    WebBrowser        := TWebBrowser.Create(Form1);
    TWinControl(WebBrowser).Name   := 'WebBrowser';
    TWinControl(WebBrowser).Parent := Form1; //set parent...can be panel, tabs etc
    WebBrowser.Silent := true;  //don't show JS errors
    WebBrowser.Visible:= true;  //visible...by default true

    //don't set width/heigh/top/left before TWinControl(WebBrowser).Parent := Form1;
    WebBrowser.Top    := 10;
    WebBrowser.Left   := 10;
    WebBrowser.Height := 600;
    WebBrowser.Width  := 800;
    WebBrowser.OnDocumentComplete  := WebBrowserDocumentComplete;
  //WebBrowser.OnNavigateComplete2 := WebBrowserNavigateComplete2;
end;


来源:https://stackoverflow.com/questions/1964976/creating-twebbrowser-in-runtime-with-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!