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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With