delphi中WebBrowser的parent改变时变成空白问题的解决

本文解决了一个使用Delphi开发时遇到的问题:在改变网页所在窗口时,WebBrowser控件会变成空白。通过CS社区提供的解决方案,实现了网页窗口的完整显示与灵活缩放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

uses
  SHDocVw, Windows, Controls, Forms, Classes;

type
  TMyWebBrowser = class(TWebBrowser)
  private
  protected
    ActualHandle: HWND;
    procedure CreateWnd; override;
    procedure DestroyWnd; override;
  public
  end;


{ TMyWebBrowser }
procedure TMyWebBrowser.CreateWnd;
begin
  if (ActualHandle <> 0) and IsWindow(ActualHandle) then
  begin
    WindowHandle := ActualHandle;
    ActualHandle := 0;
    Windows.SetParent(WindowHandle, TWinControl(Self).Parent.Handle);
    //Force a resize on the client window
    MoveWindow(WindowHandle, 0, 0, TWinControl(Self).Parent.Width,
      TWinControl(Self).Parent.Height, true); 
  end
  else
    inherited;
end;

procedure TMyWebBrowser.DestroyWnd;
begin
  if (csDestroying in ComponentState) then
    inherited
  else
  begin
    //Parent to the Application window which is 0x0 in size
    Windows.SetParent(WindowHandle, Forms.Application.Handle);
    //save the WindowHandle
    ActualHandle := WindowHandle; 
    //set it to 0 so Createwnd will be called again...
    WindowHandle := 0; 
  end;
end;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值