2011-12-06 77 views

回答

5

您需要處理OnBeforeMenu事件。在那個事件處理器中,足以將輸出參數Result設置爲True什麼會抑制默認的上下文菜單彈出。之後,您可以在從menuInfo結構獲得的職位上顯示自己的菜單。

這裏有一個自定義彈出菜單的代碼示例:

uses 
    ceflib, cefvcl; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    Chromium1.Load('www.example.com'); 
end; 

procedure TForm1.Chromium1BeforeMenu(Sender: TObject; 
    const browser: ICefBrowser; const menuInfo: PCefHandlerMenuInfo; 
    out Result: Boolean); 
begin 
    Result := True; 
    PopupMenu1.Popup(menuInfo.x, menuInfo.y); 
end; 

procedure TForm1.PopupMenuItemClick(Sender: TObject); 
begin 
    ShowMessage('You''ve clicked on a custom popup item :)'); 
end; 

更新:

對於動態創建的實例,你必須手動分配事件處理程序。嘗試下面的代碼。

uses 
    ceflib, cefvcl; 

type 
    TForm1 = class(TForm) 
    Panel1: TPanel; 
    Button1: TButton; 
    PopupMenu1: TPopupMenu; 
    procedure Button1Click(Sender: TObject); 
    private 
    procedure ChromiumOnBeforeMenu(Sender: TObject; 
     const browser: ICefBrowser; const menuInfo: PCefHandlerMenuInfo; 
     out Result: Boolean); 
    public 
    { Public declarations } 
    end; 

implementation 

procedure Form1.ChromiumOnBeforeMenu(Sender: TObject; const browser: ICefBrowser; 
    const menuInfo: PCefHandlerMenuInfo; out Result: Boolean); 
begin 
    Result := True; 
    PopupMenu1.Popup(menuInfo.x, menuInfo.y); 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
var 
    Chromium: TChromium; 
begin 
    // owner is responsible for destroying the component 
    // in this case you are telling to Panel1 to destroy 
    // the Chromium instance before he destroys itself, 
    // it doesn't affect the event handling 
    Chromium := TChromium.Create(Panel1); 

    Chromium.Parent := Panel1; 
    Chromium.Left := 10; 
    Chromium.Top := 10; 
    Chromium.Width := Panel1.Width - 20; 
    Chromium.Height := Panel1.Height - 20; 

    // this line is important, you are assigning the event 
    // handler for OnBeforeMenu event, so in fact you tell 
    // to the Chromium; hey if the OnBeforeMenu fires, run 
    // the code I'm pointing at, in this case will execute 
    // the ChromiumOnBeforeMenu procedure 
    Chromium.OnBeforeMenu := ChromiumOnBeforeMenu; 

    Chromium.Load('www.example.com'); 
end; 
+0

實際上安裝的彈出菜單父TPanel做得很不錯,但我會接受你的答案是準確和直接的。 –

+0

您可能會動態創建鉻控件,但嘗試在窗體上進行設計。即使這很奇怪,我會說鉻應該保留它的彈出菜單,不管它的主人是什麼。這種方式旨在覆蓋彈出式菜單;) – TLama

+0

當運行時創建瀏覽器時,TPanel作爲所有者,我仍然無法使其工作。事件處理程序根本不被調用。有任何想法嗎? –

3

其實你不需要彈出菜單,你不必須有加vcl.menus單位到您的應用程序,如果你已經可以構建鉻的上下文菜單。鉻自己的菜單更現代和清晰的樣子和更快的繪製性能,而不是使用復古的win32 api庫的vcl。

cef3的菜單完全可以像這樣配置。

procedure Tfmmain.Chromium1BeforeContextMenu(Sender: TObject; 
    const browser: ICefBrowser; const frame: ICefFrame; 
    const params: ICefContextMenuParams; const model: ICefMenuModel); 
    begin 
    model.Clear; 
    model.AddItem(1, 'Your Command 1'); 
    model.AddItem(2, 'Your Command 2'); 
    model.AddSeparator; 
    model.AddItem(3, 'Your Command 3'); 
    model.AddItem(4, 'your Command 4'); 
    model.AddSeparator; 
    model.AddItem(999, 'Quit'); 

    model.SetAccelerator(1, VK_RIGHT, false, false, false); 
    model.SetAccelerator(2, VK_LEFT, false, false, false); 

    model.SetAccelerator(3, VK_DOWN, false, false, false); 
    model.SetAccelerator(4, VK_UP, false, false, false); 

    model.SetAccelerator(999, VK_ESCAPE, false, false, false); 

    end; 

procedure Tfmmain.Chromium1ContextMenuCommand(Sender: TObject; 
    const browser: ICefBrowser; const frame: ICefFrame; 
    const params: ICefContextMenuParams; commandId: Integer; 
    eventFlags: TCefEventFlags; out Result: Boolean); 
    begin 
    case commandId of 
     1: 
     begin 
      DoIt1; 
      Result := true; 
     end; 
     2: 
     begin 
     DoIt2; 
      Result := true; 
     end; 
     3: 
     begin 
     DoIt3; 
      Result := true; 
     end; 
     4: 
     DoIt4; 
      Result := true; 
     end; 
     999: 
     begin 
      Application.MainForm.Close; 
      Result := true; 
     end; 
    end; 

    end; 


注:SetAccelerator快捷鍵唯一的功能,如果彈出appears.so你可能需要onPreKeyEvent

+0

此方法還允許您保留現有的Chromium彈出項目(如果您需要);但請注意[所有用戶定義的命令ID都應在MENU_ID_USER_FIRST和MENU_ID_USER_LAST之間](http://magpcss.org/ceforum/apidocs3/projects/(默認)/CefMenuModel.html) –

相關問題