How to disable context menu in cef sharp chromium browser and replace a new custom context menu that consists of only buttons
You implement the IContextMenuHandler interface, and then in IContextMenuHandler.OnBeforeContextMenu you call model.Clear();. Then you have to set your ChromiumWebBrowser's MenuHandler property to an instance of your implementation. From the code doc on that method (model.Clear()):
Remove all menu items. Can be used to disable the context menu.
Related
I am working on a custom context menu in an open-source text editor project I found on the internet. The custom context menu is made out of a Form. The problem is when I open the custom context menu, the text editor(main form) lost focus(becomes gray). I want it to look like this when the context menu opens, but not this. I have tried some solutions but they didn't work:
Solution 1: Caused the custom context menu to be on the back of the text editor once opened. It then stopped the custom context menu from working properly.
Solution 2: Caused my program to be crashed so it automatically closed.
Solution 3: I tried using either Control.Focus() or Control.Activate() to have focus on the text editor when invoking the custom context menu, but still didn't work.
Are there any other solutions I could try or am I missing something here?
Link(Head to: Branch -> v2.01) to the custom context menu project that I am working on. Please note that I have made it so that Control Key(CTRL) + Right mouse button(RMB) to invoke the custom context menu.
I am not sure whether this is conventionally the correct way to do it, but it works for me.
However, another problem it poses is that the context menu item functions cannot be executed. It can be clicked but then the nothing happens.
//activated event handler for the context menu
private void ContextMenu_Activated(object sender, EventArgs e)
{
//solution A
this.Activate(); //gives the main form the focus
//solution B
//this.Focus(); //gives the main form the focus
contextMenuObj.Visible = true; //forces the context menu to be displayed
//to maintain highlighted text in textbox in main form
richTextBox1.HideSelection = false;
//reset default flag once custom context menu opens
ctrlIsDown = false;
rmbIsUp = false;
}
This way, the main form is not greyed out when the context menu is displayed.
I have a custom RichTextBox control that inherits from WPF's RichTextBox. I want to add the option "Add to dictionary" to the default context menu.
I've scoured the net for tips on accessing the Default context menu of WPF's RichTextBox but can't find anything. I plan on using the solution Adding menu item to default context menu but I would like to reuse as much of .NET's code and logic before creating my own context menu.
So far, my attempts have only resulted with the default context menu being replaced. The closest I've gotten to adjusting the context menu is when I overrided OnContextMenuOpening method. However, it consistently states the ContextMenu is null.
How do I get access to the default context menu and add my desired option?
RichTextBox.ContextMenu is null whenever I examine it, even though the default context menu shows up. If I set the ContextMenu with a new ContextMenu, it replaces the existing context menu with a blank menu.
public class CustomRTB : RichTextBox
{
protected override void OnContextMenuOpening(ContextMenuEventArgs e)
{
this.ContextMenu = new ContextMenu();
}
}
So my question is:
I got an Windows Forms application in C# and an webBrowser control in this application. When you for example right-click on a video in youtube, a context menu shows up. Now, is it possible to programmatically rightclick in the webBrowser control an then, again programmatically, click a specific entry in that context menu?
Yes it is, but you always have to start from the same pixel, or better said an actual pixel range, so you can be sure that the clicked result will be the required one. Also you cant click an item by specifying its text, you must do everything programatticaly, from the graphics point of view(just work on the X - Y Axis since its only 2 dimensional). That is the way most web bots are made for various purposes.
Do you really have to simulate a click of the context menu or is just having the desired action good enough? If so than you can just get the item from the ContextMenu.Items list and assuming it a button raise its Click event. If you do need to at least show the context menu while do this you can call the ContextMenu.Show event. This all assumes that the contextmenu for your WebBrowser control is public (not some third party inherited control that hides it or something).
Is it possible to enable the "Open In New Tab" context menu item in a WebBrowser? Then I'll figure out how to handle it, but first I need to make it at least look enabled.
The WebBrowser control does not support multi-tab navigation. You'll have to create your own tabs (for example, using the TabControl control) which would contain instances of WebBrowser. There are great explanations on how it could be implemented here, there and there.
Then, you'll need to customize the context menu of your WebBrowser by adding the "Open In New Tab" action. That action will trigger your own implementation of multi-tab navigation. To customize the context menu, check this article: How to customize the WebBrowser context menu in C# WebBrowser Customization.
I have a form. This form has a user control. This user control has a panel and a context menu. The context menu is not attached to the panel. There are other controls that are dynamically created and added to this panel. One of those controls is a button. When you click this button, I set the contextmenustrip property to my context menu.
My problem is that I need to read the items in that context menu prior to there being the opportunity to attach the context menu to the button.
Each time a form is loaded, I iterate though all the child controls of the form. If a control has children, I iterate through those, and so on... I can't seem to get at the context menu that is unassigned so to speak. It has not been attached to any control so it does not appear to be a child control of any controls on the form.
myConectMenu is never added to the user conrol like this.Controls.Add(myConectMenu). How can that context menu not be nested in the forms control collection? How can I get at that context menu?
Here is the designer code:
private System.Windows.Forms.ContextMenuStrip myContextMenu;
void InitializeComponent()
{
this.myContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
this.myContextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.myToolStripMenuItem1,
this.myToolStripMenuItem2});
this.myContextMenu.Name = "myContextMenu";
this.myContextMenu.Size = new System.Drawing.Size(158, 92);
}
Update
The control iteration happens in a base class from which all forms in my application derive.
There is a private components object that the myContextMenu is added to. I imagine this is there so you can see the context menu in design view when it's not attached to a control. Perhaps I could leverage this?
private System.ComponentModel.IContainer components = null;
this.myContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
As you correctly observed, myContextMenu is not added to the Controls connection. Control has ContextMenuStrip property which you should check.
public void FindContextMenuStrip(Control input)
{
foreach(Control control in input.Controls)
{
if(control.ContextMenuStrip != null)
DoSomethingWithContextMenuStrip(control.ContextMenuStrip)
if(control.Controls.Count > 0)
FindContextMenuStrip(control);
}
}
Put relevant code in DoSomethingWithContextMenuStrip method.
EDIT:
I saw your comment where you specified what you wanted to do with ContextMenuStrip.
How about creating a method in Base class which takes user details and creates a context menu strip?
public ContextMenuStrip GetContextMenuStripForUser(User user)
{
//code to create context menu strip, with only those items enabled for which user has access.
}
In your final form, use this method to get ContextMenuStrip.
Create a custom contextmenu (SecureContextMenu in my case) that derives from contextmenu. Implement the open event and iterate through the items collection disabling the items that are not authorized.
Be sure to create a HasBeenOpened property and set it to true the first time the open event fires so that you don't have to keep checking the same controls every time the context menu is opened.
Use the SecureContextMenu everywhere you want context menu items checked against the list of authorized items.
It's a component and not a control attached to the form. Compare it to another form: you can manually .Show() a form from another form, but neither of them will show in in each other's .Control collection. Well, maybe that analogy wasn't the best... :s