简体   繁体   中英

TWebBrowser Handle close request

I am using TWebBrowser with a class helper that introduces some new functions (in case this is relevant to the problem below):

type
  TWebBrowserEx = class helper for TWebBrowser
  public
    procedure Clear;
    procedure Wait;
    function LoadHTML(const HTML: string; const AFakeURL: string=''): boolean;
  end;

I noticed that if a JavaScript wants to close the browser window, the handle of TWebBrowser gets destroyed.

<script type="text/javascript">
   window.close();
</script>

Calling TWebBrowser.LoadHTML() etc. will result in an Exception.

I would like to decide what my application does when the JavaScript wants to close the window. (My idea is, based on a configuration setting, either clear the page and let the application stay open, or close the application).

Question: How do I handle this? My first guess was to receive the WM_DESTROY message, but I think it doesn't work with class helpers, because nothing happens.

type
  TWebBrowserEx = class helper for TWebBrowser
  private
    procedure MessageDestroy(var msg: TMessage); message WM_DESTROY; // <-- will never be called
  public
    procedure Clear;
    procedure Wait;
    function LoadHTML(const HTML: string; const AFakeURL: string=''): boolean;
  end;

You assign the OnWindowClosing handler of your TWebBrowser to something like this:

procedure TForm1.WebBrowser1WindowClosing(
    ASender: TObject;
    IsChildWindow: WordBool;
    var Cancel: WordBool
);
begin
    Cancel := True;
end;

This will not spawn a "Do you want to close the window?" dialog and simply discard the JavaScript request to close the window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM