5

Delphi6アプリケーションでChromiumWebブラウザコントロールを使用しています。

ユーザーが現在表示されているWebページでプライマリWebサイトにないWebリンクをクリックするたびに、Windows ShellExecute()関数を使用して「Open」動詞を使用してURLを開くことにより、URLを使用してデフォルトのWebブラウザーを起動します。 。これをBeforeBrowse()イベントハンドラーから実行し、同時にナビゲーションをキャンセルします。

つまり、Chromiumコントロールに外部URLを表示せず、代わりにユーザーのデフォルトのWebブラウザーに表示します。

正常に動作しますが、アプリケーションが所有するスタンドアロンのウィンドウポップアップが表示され、画面の約半分が完全に空になります(Windowsテーマの空白の白いクライアント領域)。ウィンドウのWindowsクラス名は「webviewhost」です。

この「ゴースト」ウィンドウを抑制する方法を教えてもらえますか?

4

1 に答える 1

9

ここでの問題はポップアップウィンドウにあります。イベントが発生する前に作成され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がある場合に、フラグをTrueに設定することです(公式ドキュメントではなく、IMHOで説明されているように、ウィンドウは作成されますが非表示のままです。これが原因にならないことを願っています。リークがある場合は、それを確認していません)。ナビゲーションは続行されるため、イベントが発生します。OnBeforePopupceflib.pasOnBeforePopup

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;
于 2012-02-13T00:45:11.303 に答える