gpt4 book ai didi

delphi - 在启动用户的默认 Web 浏览器时,如何阻止 Chromium 创建 "WebViewHost"主机窗口?

转载 作者:行者123 更新时间:2023-12-03 15:07:49 30 4
gpt4 key购买 nike

我在 Delphi 6 应用程序中使用 Chromium Web 浏览器控件。

每当用户单击当前显示的网页中不在我的主网站上的 Web 链接时,我都会使用该 URL 启动他们的默认 Web 浏览器,方法是使用带有“的 Windows ShellExecute() 函数打开该 URL”打开'动词。我通过 BeforeBrowse() 事件处理程序执行此操作,同时取消导航。

换句话说,我不在 Chromium 控件中显示外部 URL,而是在用户的默认 Web 浏览器中显示它们。

它工作正常,但有时我也会弹出一个由我的应用程序拥有的独立窗口,它占据了大约一半的完全空白的屏幕(带有我的 Windows 主题的空白白色客户区域)。窗口的 Windows 类名称是“webviewhost”。

谁能告诉我如何抑制这个“幽灵”窗口?

最佳答案

这里的问题是弹出窗口。它们是在 OnBeforeBrowse 之前创建的事件被触发,您取消他们的导航,因此他们看起来像幽灵。

您可以通过设置OnBeforePopup的结果来阻止它们的创建事件为 True,但这将结束导航,因此 OnBeforeBrowse不会被解雇。如果您按照这种方式操作,那么您将必须执行 ShellExecute行动于 OnBeforePopup事件也是如此。

procedure TForm1.Chromium1BeforePopup(Sender: TObject;
const parentBrowser: ICefBrowser; var popupFeatures: TCefPopupFeatures;
var windowInfo: TCefWindowInfo; var url: ustring; var client: ICefBase;
out Result: Boolean);
begin
// you can set the Result to True here and block the window creation at all,
// but then you will stop also the navigation and the OnBeforeBrowse event
// won't be fired, so if you will follow this way then you'll have to perform
// your ShellExecute action here as well

if url <> 'http://www.yourdomain.com' then
begin
Result := True;
ShellExecute(Handle, 'open', PChar(url), '', '', SW_SHOWNORMAL);
end;
end;

另一种方法是设置 m_bWindowRenderingDisabled OnBeforePopup 中标记为 True事件应该阻止创建弹出窗口(如 ceflib.pas 中所述,而不是在官方文档中,恕我直言,窗口已创建但保持隐藏,我希望这不会导致任何泄漏,尚未验证) )并且导航将继续,因此 OnBeforePopup事件将被触发。

procedure TForm1.Chromium1BeforePopup(Sender: TObject;
const parentBrowser: ICefBrowser; var popupFeatures: TCefPopupFeatures;
var windowInfo: TCefWindowInfo; var url: ustring; var client: ICefBase;
out Result: Boolean);
begin
// you can set the m_bWindowRenderingDisabled flag to True here what should
// prevent the popup window to be created and since we don't need to take
// care about substitute parent for popup menus or dialog boxes of this popup
// window (because we will cancel the navigation anyway) we don't need to set
// the WndParent member here; but please check if there are no resource leaks
// with this solution because it seems more that the window is just hidden

if url <> 'http://www.yourdomain.com' then
windowInfo.m_bWindowRenderingDisabled := True;
end;

以下代码是您的问题的模拟(以this tutorial为例,页面下部的链接my popup,如果您单击将打开弹出窗口)。

uses
ShellAPI, ceflib, cefvcl;

const
PageURL = 'http://www.htmlcodetutorial.com/linking/linking_famsupp_72.html';
PopupURL = 'http://www.htmlcodetutorial.com/linking/popupbasic.html';

procedure TForm1.FormCreate(Sender: TObject);
begin
Chromium1.Load(PageURL);
end;

procedure TForm1.Chromium1BeforeBrowse(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const request: ICefRequest; navType: TCefHandlerNavtype; isRedirect: Boolean;
out Result: Boolean);
begin
if request.Url = PopupURL then
begin
Result := True;
ShellExecute(Handle, 'open', PChar(request.Url), '', '', SW_SHOWNORMAL);
end;
end;

procedure TForm1.Chromium1BeforePopup(Sender: TObject;
const parentBrowser: ICefBrowser; var popupFeatures: TCefPopupFeatures;
var windowInfo: TCefWindowInfo; var url: ustring; var client: ICefBase;
out Result: Boolean);
begin
{
// Solution 1
// this will block the popup window creation and cancel the navigation to
// the target, so we have to perform the ShellExecute action here as well
if url = PopupURL then
begin
Result := True;
ShellExecute(Handle, 'open', PChar(url), '', '', SW_SHOWNORMAL);
end;
}
{
// Solution 2
// or we can set the m_bWindowRenderingDisabled flag to True and the window
// won't be created (as described in ceflib.pas), but the navigation continue
if url = PopupURL then
windowInfo.m_bWindowRenderingDisabled := True;
}
end;

关于delphi - 在启动用户的默认 Web 浏览器时,如何阻止 Chromium 创建 "WebViewHost"主机窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249863/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com