Rob*_*ler 5 delphi shellexecute chromium delphi-6
我在我的Delphi 6应用程序中使用Chromium Web浏览器控件.
每当用户点击当前正在显示的不在我的主网站上的网页中的网页链接时,我通过使用带有"打开"动词的Windows ShellExecute()函数打开URL来启动带有URL的默认Web浏览器.我从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;
Run Code Online (Sandbox Code Playgroud)
另一种方法是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;
Run Code Online (Sandbox Code Playgroud)
以下代码是您的问题的模拟(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;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3167 次 |
最近记录: |