2012-02-12 16 views
5

私はDelphi 6アプリケーションでChromium Webブラウザコントロールを使用しています。ユーザーのデフォルトWebブラウザを起動したときにChromiumが「WebViewHost」ホストウィンドウを作成しないようにするにはどうすればよいですか?

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

は、言い換えれば、私はクロムコントロールの外部URLを示していないが、代わりに、ユーザーのデフォルトWebブラウザでそれらを示しています。

それは正常に動作しますが、時々私も自分のアプリケーションが所有するスタンドアロンウィンドウのポップアップを取得し、それは完全に空である約半分の画面(私のWindowsテーマと空白のクライアント領域)を占めます。ウィンドウのWindowsクラス名は "webviewhost"です。

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

答えて

7

ここで問題となるのはポップアップウィンドウです。彼らはOnBeforeBrowseイベントが発生する前に作成され、あなたはゴーストのように見えるようにナビゲーションをキャンセルします。

OnBeforePopupイベントの結果をTrueに設定すると、その作成を防止できますが、これによりナビゲーションが終了し、OnBeforeBrowseは発生しません。このようにすれば、OnBeforePopupイベントでもShellExecuteアクションを実行する必要があります。

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; 

別の方法ceflib.pasに記載されていないウィンドウが作成されますが、非表示のままです私見公式ドキュメントで、(作成されるポップアップウィンドウを防ぐべきかOnBeforePopupイベントでTrueにm_bWindowRenderingDisabledフラグを設定することです私はこれがどんなリークにもつながることはないと確信しています)、ナビゲーションは続行されますので、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; 

次のコードは、問題のシミュレーション(例はクリックした場合、ポップアップウィンドウが開きますページの下部にリンクmy popupあるとしてthis tutorialで使用される)です。

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; 
+4

グレート答え、ありがとう。 –

関連する問題