gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:07:51 24 4
gpt4 key购买 nike

我有一个来自 Delphi Chromium Embedded (http://code.google.com/p/delphichromiumembedded) 的 TChromium 浏览器。我想为其附加一个上下文菜单。我怎样才能实现这一目标?

最佳答案

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

关于delphi - 如何将上下文菜单附加到 TChromium 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8408077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com