2012-05-31 22 views

回答

12

沒有直接設置或事件允許隱藏Chromium彈出式菜單項。但是你至少有幾個選項如何繼續,例如你可以:

的查看源代碼的選項是被禁止的,並拒絕採取行動

你可以決定什麼樣的行動1.告訴用戶你會允許或拒絕OnMenuAction事件處理程序,其中如果將True分配給Result參數,則操作將被拒絕。已經執行查看源代碼的動作,如果是這樣,拒絕動作和顯示信息消息下面的代碼檢查:自定義通過更改菜單項的標題與

type 
    TCefMenuId = TCefHandlerMenuId; 

procedure TForm1.Chromium1MenuAction(Sender: TObject; 
    const browser: ICefBrowser; menuId: TCefMenuId; out Result: Boolean); 
begin 
    if menuId = MENU_ID_VIEWSOURCE then 
    begin 
    Result := True; 
    ShowMessage('View page source is not allowed!'); 
    end; 
end; 

2.假菜單項的東西了動作

您可以通過更改菜單項的標題並執行一些自定義操作來利用其他菜單項。下面的示例代碼演示瞭如何更改視圖源菜單項到約箱菜單項:

type 
    TCefMenuId = TCefHandlerMenuId; 

procedure TForm1.Chromium1GetMenuLabel(Sender: TObject; 
    const browser: ICefBrowser; menuId: TCefMenuId; var caption: ustring; 
    out Result: Boolean); 
begin 
    if menuId = MENU_ID_VIEWSOURCE then 
    caption := 'About my application...'; 
end; 

procedure TForm1.Chromium1MenuAction(Sender: TObject; 
    const browser: ICefBrowser; menuId: TCefMenuId; out Result: Boolean); 
begin 
    if menuId = MENU_ID_VIEWSOURCE then 
    begin 
    Result := True; 
    ShowMessage('About box...!'); 
    end; 
end; 

3.創建你自己的自定義頁面(幀)彈出菜單

您可以創建你自己的彈出式菜單,但是你需要考慮到這個菜單是相當硬編碼的,所以如果你需要使用每個新版本的Delphi Chromium包裝,那麼你需要維護它。下面是代碼如何在不查看源代碼菜單項中創建的頁面菜單:

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls, Menus, cefvcl, ceflib; 

type 
    PCefMenuInfo = PCefHandlerMenuInfo; 

type 
    TForm1 = class(TForm) 
    Chromium1: TChromium; 
    procedure FormCreate(Sender: TObject); 
    procedure Chromium1BeforeMenu(Sender: TObject; const browser: ICefBrowser; 
     const menuInfo: PCefMenuInfo; out Result: Boolean); 
    private 
    PageMenu: TPopupMenu; 
    procedure OnNavigateBackMenuItemClick(Sender: TObject); 
    procedure OnNavigateForwardMenuItemClick(Sender: TObject); 
    procedure OnPrintMenuItemClick(Sender: TObject); 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.OnNavigateBackMenuItemClick(Sender: TObject); 
begin 
    Chromium1.Browser.GoBack; 
end; 

procedure TForm1.OnNavigateForwardMenuItemClick(Sender: TObject); 
begin 
    Chromium1.Browser.GoForward; 
end; 

procedure TForm1.OnPrintMenuItemClick(Sender: TObject); 
begin 
    Chromium1.Browser.GetFocusedFrame.Print; 
end; 

procedure TForm1.Chromium1BeforeMenu(Sender: TObject; 
    const browser: ICefBrowser; const menuInfo: PCefMenuInfo; 
    out Result: Boolean); 
begin 
    if menuInfo.typeFlags = MENUTYPE_PAGE then 
    begin 
    Result := True; 
    PageMenu.Items[0].Enabled := browser.CanGoBack; 
    PageMenu.Items[1].Enabled := browser.CanGoForward; 
    PageMenu.Popup(menuInfo^.x, menuInfo^.y); 
    end; 
end; 

procedure TForm1.FormCreate(Sender: TObject); 
var 
    MenuItem: TMenuItem; 
begin 
    PageMenu := TPopupMenu.Create(Self); 
    MenuItem := TMenuItem.Create(PageMenu); 
    MenuItem.Caption := 'Back'; 
    MenuItem.OnClick := OnNavigateBackMenuItemClick; 
    PageMenu.Items.Add(MenuItem); 
    MenuItem := TMenuItem.Create(PageMenu); 
    MenuItem.Caption := 'Forward'; 
    MenuItem.OnClick := OnNavigateForwardMenuItemClick; 
    PageMenu.Items.Add(MenuItem); 
    MenuItem := TMenuItem.Create(PageMenu); 
    MenuItem.Caption := '-'; 
    PageMenu.Items.Add(MenuItem); 
    MenuItem := TMenuItem.Create(PageMenu); 
    MenuItem.Caption := 'Print'; 
    MenuItem.OnClick := OnPrintMenuItemClick; 
    PageMenu.Items.Add(MenuItem); 
    Chromium1.Load('www.stackoverflow.com'); 
end; 

end. 

腳註

類型定義的所有代碼樣本中使用的那裏,因爲我注意到,一些版本Delphi Chromium的錯誤事件處理程序定義。

+0

好的。非常感謝你 ! – henry60

+1

很高興幫助;-) – TLama

+1

謝謝你的幫助;) – Beny

1

也許事情改變了多年,今天的直接方法存在:

uses 
    ceflib; 

[..] 

implementation 

procedure TForm1.Chromium1BeforeContextMenu(Sender: TObject; 
    const browser: ICefBrowser; const frame: ICefFrame; 
    const params: ICefContextMenuParams; const model: ICefMenuModel); 
begin 
    //model.Clear; 
    model.Remove(Integer(MENU_ID_VIEW_SOURCE)); 
end; 

您可以使用model.Clear,如果你想徹底擺脫彈出菜單。

相關問題