2011-12-06 13 views

Respuesta

5

Debe controlar el evento OnBeforeMenu. En ese caso, el controlador es suficiente para establecer el parámetro de salida Result en True lo que suprimirá los menús contextuales predeterminados para emergente. Después de eso, puede mostrar su propio menú en las posiciones obtenidas de la estructura menuInfo.

Aquí está el ejemplo de código con un menú emergente personalizado:

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; 

Actualización:

Por ejemplo creado de forma dinámica tiene que asignar el controlador de eventos manualmente. Pruebe el siguiente código.

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

En realidad adjuntando el menú emergente para un padre TPanel funcionó bastante bien, pero voy a aceptar su respuesta como precisa y recta. –

+0

Probablemente esté creando dinámicamente el control de cromo, pero intente diseñarlo en un formulario. E incluso esto es extraño, diría que el cromo debería mantener su menú emergente sea lo que sea su propietario. De esta manera se pretende anular el menú emergente;) – TLama

+0

Todavía no puedo hacer que funcione cuando el navegador se crea en tiempo de ejecución con TPanel como propietario. El controlador de eventos simplemente no se llama. ¿Algunas ideas? –

3

realidad usted no necesita popupmenu y usted no tiene que tener unidad de añadir vcl.menus en su aplicación si ya tiene puede construir el menú contextual de cromo. también el menú propio del cromo es más moderno y claro y tiene una perfomance más rápida que un vcl que utiliza la biblioteca vintage api win32.

cef3 tiene su menú totalmente configurable como este.

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; 


nota: atajos SetAccelerator sólo funcional si emergente appears.so es posible que necesite onPreKeyEvent

+0

Este enfoque también le permite mantener los elementos emergentes Chromium existentes si los quiere; pero tenga en cuenta [Todos los identificadores de comando definidos por el usuario deben estar entre MENU_ID_USER_FIRST y MENU_ID_USER_LAST] (http://magpcss.org/ceforum/apidocs3/projects/ (predeterminado) /CefMenuModel.html) –

Cuestiones relacionadas