TWebBrowser 仍然被关注ActiveControl
并且TOleControl.HookControlWndProc
正在被调用,ActiveControl
它不再在内存中。结果EOleError
引发异常,因为无法获取窗口句柄。您可以通过在关闭应用程序之前设置ActiveControl
为(更改活动控制焦点)来避免这种情况。nil
ActiveControl := nil;
这是导致异常的函数(OleCtrls.pas):
procedure TOleControl.HookControlWndProc;
var
WndHandle: HWnd;
begin
if (FOleInPlaceObject <> nil) and (WindowHandle = 0) then
begin
WndHandle := 0;
FOleInPlaceObject.GetWindow(WndHandle);
// Exception is raised here because WndHandle could not be obtained
if WndHandle = 0 then raise EOleError.CreateRes(@SNoWindowHandle);
WindowHandle := WndHandle;
DefWndProc := Pointer(GetWindowLong(WindowHandle, GWL_WNDPROC));
CreationControl := Self;
SetWindowLong(WindowHandle, GWL_WNDPROC, Longint(@InitWndProc));
SendMessage(WindowHandle, WM_NULL, 0, 0);
end;
end;
另一种方法是在将销毁消息发送到 TWebBrowser 句柄时WM_PARENTNOTIFY
使用参数捕获消息,WM_DESTROY
因为父表单(嵌套 TWebBrowser 的地方)收到一条WM_PARENTNOTIFY
消息:
procedure ParentNotify(var Msg: TMessage); message WM_PARENTNOTIFY;
消息处理程序的实现:
procedure TMyForm.ParentNotify(Var Msg: TMessage);
begin
if (Msg.WParamLo = WM_DESTROY) and (Msg.LParam = mywebbrowser.Handle) then close;
end;