如何将上下文菜单附加到TChromium浏览器 [英] How to attach context menu to TChromium browser

查看:331
本文介绍了如何将上下文菜单附加到TChromium浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自Delphi Chromium Embedded(http://code.google.com/p/delphichromiumembedded)的TChromium broser。我想附加一个上下文菜单。我该如何实现?

I have a TChromium broser from Delphi Chromium Embedded (http://code.google.com/p/delphichromiumembedded). I would like to attach a context menu to it. How I can achieve that?

推荐答案

您需要处理 OnBeforeMenu 事件。在该事件处理程序中,足以将输出参数 Result 设置为 True ,这将禁止弹出默认上下文菜单。之后,您可以在从 menuInfo 结构。

You need to handle the OnBeforeMenu event. In that event handler is enough to set the output parameter Result to True what will suppress the default context menus to popup. After that you can display your own menu on the positions obtained from the menuInfo structure.

以下是带有自定义弹出菜单的代码示例:

Here's the code sample with a custom popup menu:

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;

更新:

Update:

对于动态创建的实例,您必须手动分配事件处理程序。尝试以下代码。

For dynamically created instance you have to assign the event handler manually. Try the following code.

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;

这篇关于如何将上下文菜单附加到TChromium浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆