2014-09-02 225 views
3

我想要實現,當用戶點擊TChromium瀏覽器頁面內的超鏈接時,新頁面將在其默認瀏覽器中打開。如何在默認瀏覽器中打開鉻瀏覽器鏈接的點擊?

+3

在'OnBeforeBrowse'事件檢查,如果'navType'等於'NAVTYPE_LINKCLICKED'如果是的話,返回True到'Result'參數(這將取消對Chromium的請求)並調用例如'ShellExecute'傳遞'request.Url'。 – TLama 2014-09-02 15:28:16

+2

太棒了。你爲什麼不發佈這個答案?太不適合你了? :) – Domus 2014-09-02 15:56:06

回答

4

在如果navType參數等於NAVTYPE_LINKCLICKED並且如果是這樣的OnBeforeBrowse事件檢查,返回True到Result參數(這將取消對鉻的請求),並調用例如ShellExecute傳遞request.Url值在用戶的默認瀏覽器中打開鏈接:

uses 
    ShellAPI, ceflib; 

procedure TForm1.Chromium1BeforeBrowse(Sender: TObject; 
    const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; 
    navType: TCefHandlerNavtype; isRedirect: boolean; out Result: Boolean); 
begin 
    if navType = NAVTYPE_LINKCLICKED then 
    begin 
    Result := True; 
    ShellExecuteW(0, nil, PWideChar(request.Url), nil, nil, SW_SHOWNORMAL); 
    end; 
end; 
+0

mabybe add an 「else Result:= False」 ? – Domus 2014-09-03 11:40:15

+0

在該方法開始時可能會出現'Result:= False',但不需要,因爲False是['initial result'](https://code.google.com/p/delphichromiumembedded/source/browse/)軀幹/ SRC/cefvcl.pas#644)。 – TLama 2014-09-03 11:51:30

+1

你當然是對的,但是Delphi的一個缺乏參數更多的暗示情況。 :) – Domus 2014-09-03 12:05:54

2

在CEF3,navType = NAVTYPE_LINKCLICKED不再可能在OnBeforeBrowse事件,如TLama的答案。相反,我發現瞭如何檢測到這種使用TransitionType財產...

procedure TfrmEditor.BrowserBeforeBrowse(Sender: TObject; 
    const browser: ICefBrowser; const frame: ICefFrame; 
    const request: ICefRequest; isRedirect: Boolean; out Result: Boolean); 
begin 
    case Request.TransitionType of 
    TT_LINK: begin 
     // User clicked on link, launch URL... 
     ShellExecuteW(0, nil, PWideChar(Request.Url), nil, nil, SW_SHOWNORMAL); 
     Result:= True; 
    end; 
    TT_EXPLICIT: begin 
     // Source is some other "explicit" navigation action such as creating a new 
     // browser or using the LoadURL function. This is also the default value 
     // for navigations where the actual type is unknown. Do nothing. 
    end; 
    end; 
end; 
相關問題