我有一个来自Delphi Chromium Embedded的TChromium broser(http://code.google.com/p/delphichromiumembedded)。我想附上一个上下文菜单。我怎么能做到这一点?
答案 0 :(得分: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;
答案 1 :(得分:4)
实际上你不需要popupmenu,如果你已经可以构建chrome的上下文菜单,你就不必将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快捷方式仅起作用。因此您可能需要onPreKeyEvent