解决方法
没有允许隐藏Chromium弹出菜单项的直接设置或事件.但是,您至少有几个选项可以继续,例如:
您可以决定OnMenuAction事件处理程序中允许或拒绝的操作,如果为Result参数指定True,则操作将被拒绝.以下代码检查您是否已执行视图源操作,如果是,请拒绝操作并显示信息消息:
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;
您可以通过更改菜单项的标题并执行一些自定义操作来利用菜单项.以下示例代码显示如何将视图源菜单项更改为about框菜单项:
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;
您可以创建自己的弹出菜单,但是您需要考虑此菜单是非常硬编码的,因此如果您需要在每个新版本的Delphi Chromium包装中使用它,则需要对其进行维护.以下是如何在没有查看源菜单项的情况下创建页面菜单的代码:
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.
脚注