delphi中WebBrowser的parent改变时变成空白问题的解决,覆盖CreateWnd和DestroyWnd

这段时间在做一个delphi界面打开网页的功能,且此网页所在窗口可完整显示,可缩小到另一个窗口的panel上显示

可是在改变网页所在窗口时,WebBrowser控件变成了空白

上网google了半天,终于在csdn上查到了解决方案:

原帖地址:http://bbs.csdn.NET/topics/200046109

[Delphi]view plaincopy

  1. uses
  2. SHDocVw, Windows, Controls, Forms, Classes;
  3. type
  4. TMyWebBrowser = class(TWebBrowser)
  5. private
  6. protected
  7. ActualHandle: HWND;
  8. procedure CreateWnd; override;
  9. procedure DestroyWnd; override;
  10. public
  11. end;
  12. { TMyWebBrowser }
  13. procedure TMyWebBrowser.CreateWnd;
  14. begin
  15. if (ActualHandle <> 0) and IsWindow(ActualHandle) then
  16. begin
  17. WindowHandle := ActualHandle;
  18. ActualHandle := 0;
  19. Windows.SetParent(WindowHandle, TWinControl(Self).Parent.Handle);
  20. //Force a resize on the client window
  21. MoveWindow(WindowHandle, 0, 0, TWinControl(Self).Parent.Width,
  22. TWinControl(Self).Parent.Height, true);
  23. end
  24. else
  25. inherited;
  26. end;
  27. procedure TMyWebBrowser.DestroyWnd;
  28. begin
  29. if (csDestroying in ComponentState) then
  30. inherited
  31. else
  32. begin
  33. //Parent to the Application window which is 0x0 in size
  34. Windows.SetParent(WindowHandle, Forms.Application.Handle);
  35. //save the WindowHandle
  36. ActualHandle := WindowHandle;
  37. //set it to 0 so Createwnd will be called again...
  38. WindowHandle := 0;
  39. end;
  40. end;

http://blog.csdn.net/youthon/article/details/8450610