There are no direct settings or events to hide Chromium pop-up menu items. However, you have at least several options for continuing, you can, for example:
1. Notify the user that the option "View Source" is disabled and reject action
You can decide which action you allow or reject in the OnMenuAction
event OnMenuAction
, where if you assign True to the Result
parameter, the action is rejected. The following code verifies that you performed the action of the view source, and if so, clear the action and display an informational message:
type TCefMenuId = TCefHandlerMenuId; procedure TForm1.Chromium1MenuAction(Sender: TObject; const browser: ICefBrowser; menuId: TCefMenuId; out Result: Boolean); begin if menuId = MENU_ID_VIEWSOURCE then begin Result := True; ShowMessage('View page source is not allowed!'); end; end;
2. Fake a menu item for something ordinary by changing the title of the item with its action
You can use the menu item for something else by changing the title of the menu item and performing some custom action. The following code example shows how to change the view source menu item to a window menu item:
type TCefMenuId = TCefHandlerMenuId; procedure TForm1.Chromium1GetMenuLabel(Sender: TObject; const browser: ICefBrowser; menuId: TCefMenuId; var caption: ustring; out Result: Boolean); begin if menuId = MENU_ID_VIEWSOURCE then caption := 'About my application...'; end; procedure TForm1.Chromium1MenuAction(Sender: TObject; const browser: ICefBrowser; menuId: TCefMenuId; out Result: Boolean); begin if menuId = MENU_ID_VIEWSOURCE then begin Result := True; ShowMessage('About box...!'); end; end;
3. Create your own pop-up menu of your own page (frame)
You can create your own pop-up menu, but you need to consider that this menu is pretty tough, so you will need to maintain it if you need to have the same with every new version of Delphi Chromium wrapper. Here is the code on how to create a page menu without an image source menu item:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Menus, cefvcl, ceflib; type PCefMenuInfo = PCefHandlerMenuInfo; type TForm1 = class(TForm) Chromium1: TChromium; procedure FormCreate(Sender: TObject); procedure Chromium1BeforeMenu(Sender: TObject; const browser: ICefBrowser; const menuInfo: PCefMenuInfo; out Result: Boolean); private PageMenu: TPopupMenu; procedure OnNavigateBackMenuItemClick(Sender: TObject); procedure OnNavigateForwardMenuItemClick(Sender: TObject); procedure OnPrintMenuItemClick(Sender: TObject); public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.OnNavigateBackMenuItemClick(Sender: TObject); begin Chromium1.Browser.GoBack; end; procedure TForm1.OnNavigateForwardMenuItemClick(Sender: TObject); begin Chromium1.Browser.GoForward; end; procedure TForm1.OnPrintMenuItemClick(Sender: TObject); begin Chromium1.Browser.GetFocusedFrame.Print; end; procedure TForm1.Chromium1BeforeMenu(Sender: TObject; const browser: ICefBrowser; const menuInfo: PCefMenuInfo; out Result: Boolean); begin if menuInfo.typeFlags = MENUTYPE_PAGE then begin Result := True; PageMenu.Items[0].Enabled := browser.CanGoBack; PageMenu.Items[1].Enabled := browser.CanGoForward; PageMenu.Popup(menuInfo^.x, menuInfo^.y); end; end; procedure TForm1.FormCreate(Sender: TObject); var MenuItem: TMenuItem; begin PageMenu := TPopupMenu.Create(Self); MenuItem := TMenuItem.Create(PageMenu); MenuItem.Caption := 'Back'; MenuItem.OnClick := OnNavigateBackMenuItemClick; PageMenu.Items.Add(MenuItem); MenuItem := TMenuItem.Create(PageMenu); MenuItem.Caption := 'Forward'; MenuItem.OnClick := OnNavigateForwardMenuItemClick; PageMenu.Items.Add(MenuItem); MenuItem := TMenuItem.Create(PageMenu); MenuItem.Caption := '-'; PageMenu.Items.Add(MenuItem); MenuItem := TMenuItem.Create(PageMenu); MenuItem.Caption := 'Print'; MenuItem.OnClick := OnPrintMenuItemClick; PageMenu.Items.Add(MenuItem); Chromium1.Load('www.stackoverflow.com'); end; end.
Footnote
There are type definitions used in all code samples because I noticed that in some version of Delphi Chromium there are incorrect event handler definitions.