2012-02-12 20 views
5

Używam kontrolki przeglądarki Chromium w mojej aplikacji Delphi 6.Jak zatrzymać tworzenie przez Chromium okna hosta "WebViewHost" po uruchomieniu domyślnej przeglądarki internetowej?

Za każdym razem, gdy użytkownik kliknie link na stronie aktualnie wyświetlanej, która nie jest w mojej głównej witrynie internetowej, uruchamiam domyślną przeglądarkę internetową z adresem URL, otwierając adres URL za pomocą funkcji Windows ShellExecute() z Czasownik "otwarty". Robię to z obsługi zdarzeń BeforeBrowse() i jednocześnie anuluję nawigację.

Innymi słowy, nie pokazuję zewnętrznych adresów URL w kontrolce Chromium, lecz wyświetlam je w domyślnej przeglądarce użytkownika.

To działa dobrze, ale czasami dostaję także osobne okno pop up posiadane przez moją aplikację i to zajmuje około połowy ekranu, który jest całkowicie pusty (pusty biały obszar klienta z moim motywem Windows). Nazwa klasy Windows okna to "webviewhost".

Czy ktoś może mi powiedzieć, jak stłumić okno "duchów"?

Odpowiedz

7

Problem dotyczy okien wyskakujących. Są one tworzone przed wyzwoleniem zdarzenia OnBeforeBrowse, a Ty anulujesz nawigację, aby wyglądały jak duchy.

Możesz zapobiec ich utworzeniu, ustawiając wynik zdarzenia OnBeforePopup na True, ale to zakończy nawigację, aby OnBeforeBrowse nie został zwolniony. Jeśli podążysz tą drogą, będziesz musiał wykonać swoją akcję ShellExecute również w wydarzeniu 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; 

Innym sposobem jest ustawienie flagi m_bWindowRenderingDisabled True w przypadku OnBeforePopup co powinno zapobiec okienko zostać utworzony (jak opisano w ceflib.pas, a nie w oficjalnej dokumentacji, IMHO okno zostanie utworzone, ale pozostaje ukryty i mam nadzieję, że to nie doprowadzi do żadnego wycieku, nie zweryfikowałem tego) i nawigacja będzie kontynuowana, więc zdarzenie OnBeforePopup zostanie zwolnione.

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; 

Poniższy kod jest symulacja problemu (w this tutorial używana jako przykład jest link my popup w dolnej części strony, która po kliknięciu na otworzy okienko).

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

Świetna odpowiedź, dzięki. –

Powiązane problemy