如何将上下文菜单附加到 TChromium 浏览器

发布于 2024-12-19 15:52:26 字数 133 浏览 5 评论 0原文

我有一个来自 Delphi Chromium Embedded 的 TChromium broser (http://code.google.com/p/delphichromiumembedded)。我想为其附加一个上下文菜单。我怎样才能做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紅太極 2024-12-26 15:52:26

您需要处理 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;

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;
不必了 2024-12-26 15:52:26

实际上,如果您已经可以构建 chromium 的上下文菜单,则不需要 popupmenu 并且不必将 vcl.menus 单元添加到您的应用程序中。而且 chromium 自己的菜单比使用老式 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;


note:SetAccelerator shortcuts only functional if popup appears.so you may need onPreKeyEvent

actually you dont need popupmenu and you dont have to have add vcl.menus unit into your application if you have already can build chromium's context menu. also chromium's own menu is more modern and clear look like and faster draw perfrmance rather than a vcl which uses vintage win32 api library.

cef3 has its menu totally configurable like this.

 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;


note:SetAccelerator shortcuts only functional if popup appears.so you may need onPreKeyEvent

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文