private void Build(CefMenuModel model, ToolStripItemCollection menu) { CefColor color = default; int count = model.Count; for (int i = 0; i < count; i++) { ToolStripMenuItem menuItem; switch (model.GetTypeAt(i)) { case CefMenuItemType.Separator: menu.Add(new ToolStripSeparator()); continue; case CefMenuItemType.Check: menuItem = new ToolStripMenuItem(model.GetLabelAt(i)); menuItem.CheckOnClick = true; menuItem.Checked = model.IsCheckedAt(i); break; case CefMenuItemType.Radio: menuItem = new ToolStripRadioMenuItem(model.GetLabelAt(i)); menuItem.Checked = model.IsCheckedAt(i); break; case CefMenuItemType.Command: menuItem = new ToolStripMenuItem(model.GetLabelAt(i)); break; case CefMenuItemType.Submenu: menuItem = new ToolStripMenuItem(model.GetLabelAt(i)); if (model.IsEnabledAt(i)) { menuItem.DropDownItemClicked += Menu_ItemClicked; Build(model.GetSubMenuAt(i), menuItem.DropDownItems); } break; default: continue; } menuItem.Enabled = model.IsEnabledAt(i); menuItem.Tag = 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; } }
/// <summary> /// The on before context menu. /// </summary> /// <param name="browser"> /// The browser. /// </param> /// <param name="frame"> /// The frame. /// </param> /// <param name="state"> /// The state. /// </param> /// <param name="model"> /// The model. /// </param> protected override void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams state, CefMenuModel model) { base.OnBeforeContextMenu(browser, frame, state, model); for (int x = model.Count - 1; x > -1; x--) { var item = model.GetLabelAt(x); if (item == "&Back" || item == "&Forward" || item == "&Print…" || item == "&View page source") { model.RemoveAt(x); } } if (model.Count == 1 && model.GetItemTypeAt(0).ToString() == "Separator") { model.RemoveAt(0); } model.AddItem((int)(CefMenuId)ShowDevTools, "Show DevTools"); model.AddItem((int)(CefMenuId)Refresh, "Refresh"); }