protected override void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams state, CefMenuModel model) { for (var index = model.Count - 1; index >= 0; index--) { var nCmd = model.GetCommandIdAt(index); if (!ContextMenuHelper.IsEdition(nCmd) && !ContextMenuHelper.IsUserDefined(nCmd)) { model.Remove(nCmd); } } if (WinFormium.Runtime.IsDebuggingMode && (state.ContextMenuType & CefContextMenuTypeFlags.Editable) != CefContextMenuTypeFlags.Editable) { if (model.Count > 0) { model.AddSeparator(); } model.AddItem((int)((CefMenuId)ContextMenuHelper.MenuIdentifier.MENU_ID_SHOW_DEVTOOLS), Resources.Messages.ContextMenu_ShowDevTools); model.AddItem((int)((CefMenuId)ContextMenuHelper.MenuIdentifier.MENU_ID_HIDE_DEVTOOLS), Resources.Messages.ContextMenu_CloseDevTools); } var e = new BeforeContextMenuEventArgs(frame, state, model); _owner.InvokeIfRequired(() => _owner.OnBeforeContextMenu(e)); }
protected override void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams menuParams, CefMenuModel model) { var count = model.Count; if (count > 0) { for (int i = 0; i < count; i++) { var commandId = (CefMenuId)model.GetCommandIdAt(i); switch (commandId) { case CefMenuId.Undo: case CefMenuId.Redo: case CefMenuId.Cut: case CefMenuId.Copy: case CefMenuId.Paste: case CefMenuId.Delete: case CefMenuId.SelectAll: case CefMenuId.Find: break; default: break; } } } if (AppHelper.EnableDevtools) { model.Remove((int)CefMenuId.Print); model.Remove((int)CefMenuId.ViewSource); if (model.Count >= 1) { model.RemoveAt(model.Count - 1); } model.AddItem(SHOW_DEV_TOOLS, "&Show DevTools"); } else { model.Clear(); // 禁用右键菜单 } //model.InsertItemAt(model.Count > 0 ? 1 : 0, (int)CefMenuId.ReloadNocache, "Refresh"); //model.AddSeparator(); //model.AddItem(SHOW_DEV_TOOLS, "&Show DevTools"); //model.AddItem(INSPECT_ELEMENT, "Inspect element"); //CefMenuModel submenu = model.AddSubMenu(0, "Submenu Test"); //submenu.AddItem((int)CefMenuId.Copy, "Copy"); //submenu.AddItem((int)CefMenuId.Paste, "Paste"); //submenu.SetColorAt(submenu.Count - 1, CefMenuColorType.Text, CefColor.FromArgb((int)Colors.Blue.ToUint32())); //submenu.AddCheckItem(0, "Checked Test"); //submenu.SetCheckedAt(submenu.Count - 1, true); //submenu.AddRadioItem(0, "Radio Off", 0); //submenu.AddRadioItem(0, "Radio On", 1); //submenu.SetCheckedAt(submenu.Count - 1, true); }
private void Build(CefMenuModel model, MenuItemCollection menu) { CefColor color = default; int count = model.Count; for (int i = 0; i < count; i++) { MenuItem menuItem; switch (model.GetTypeAt(i)) { case CefMenuItemType.Separator: menu.Add(new MenuSeparatorItem()); continue; case CefMenuItemType.Check: menuItem = new MenuItem(); //menuItem.CheckOnClick = true; //menuItem.Checked = model.IsCheckedAt(i); break; case CefMenuItemType.Radio: menuItem = new MenuItem(); //menuItem.Checked = model.IsCheckedAt(i); break; case CefMenuItemType.Command: menuItem = new MenuItem(); break; case CefMenuItemType.Submenu: menuItem = new MenuItem(); if (model.IsEnabledAt(i)) { //menuItem.DropDownItemClicked += Menu_ItemClicked; Build(model.GetSubMenuAt(i), menuItem.Items); } break; default: continue; } menuItem.Text = model.GetLabelAt(i).Replace("&", ""); menuItem.Click += Menu_ItemClicked; menuItem.Enabled = model.IsEnabledAt(i); //menuItem.Tag = model.GetCommandIdAt(i); MenuItemsTags.Add(menuItem, model.GetCommandIdAt(i)); //menuItem.ForeColor = model.GetColorAt(i, CefMenuColorType.Text, ref color) ? Color.FromArgb(color.ToArgb()) : SystemColors.ControlText; menu.Add(menuItem); } }
private static void CopyItems(CefMenuModel model, MenuModelItem[] dest) { for (int i = 0; i < dest.Length; i++) { var item = new MenuModelItem { Command = model.GetCommandIdAt(i), Label = model.GetLabelAt(i), Type = model.GetTypeAt(i), Group = model.GetGroupIdAt(i), Visible = model.IsVisibleAt(i), Enabled = model.IsEnabledAt(i), Checked = model.IsCheckedAt(i), }; CefColor color = default; item.Colors = new CefColor[(int)CefMenuColorType.Count]; for (int j = 0; j < item.Colors.Length; j++) { item.Colors[j] = model.GetColorAt(i, (CefMenuColorType)j, ref color) ? color : default; } int keycode = 0, alt = 0, ctrl = 0, shift = 0; if (model.GetAcceleratorAt(i, ref keycode, ref shift, ref ctrl, ref alt)) { item.Accelerator = new Accelerator { KeyCode = keycode, Alt = alt != 0, Ctrl = ctrl != 0, Shift = shift != 0 }; } if (item.Type == CefMenuItemType.Submenu) { CefMenuModel submenu = model.GetSubMenuAt(i); var items = new MenuModelItem[submenu.Count]; CopyItems(submenu, items); item.SubMenu = new MenuModel(items) { IsSubMenu = true }; } dest[i] = item; } }