Single post

Cut/Copy/Paste for the .Net Windows Forms Web Browser Control

Implementing custom Cut/Copy/Paste functionality with the .NET Web Browser control is quite easy to achieve. Here is how:

Sample WebBrowser Context Menu

First, add a reference to Microsoft.mshtml. Then:

using mshtml;

private bool IsCommandEnabled(string wCmd)
{
  IHTMLDocument2 doc2 = tBrowser.Document.DomDocument
    as IHTMLDocument2;
  if (doc2 != null)
    return doc2.queryCommandEnabled(wCmd);
  return false;
}

private void UpdateButtons()
{
  // Button states are handled here.
  ctxMenuCut.Enabled = IsCommandEnabled("Cut");
  ctxMenuCopy.Enabled = IsCommandEnabled("Copy");
  ctxMenuPaste.Enabled = IsCommandEnabled("Paste");
}

Next, assign CtxMenuClickHandler as the Click Event Handler for all the Cut/Copy/Paste buttons.

private void CtxMenuClickHandler(object sender, EventArgs e)
{
  if (sender == ctxMenuCut)
  {
    tBrowser.Document.ExecCommand("Cut", false, null);
    UpdateButtons();
  }
  else if (sender == ctxMenuCopy)
  {
    tBrowser.Document.ExecCommand("Copy", false, null);
    UpdateButtons();
  }
  else if (sender == ctxMenuPaste)
  {
    tBrowser.Document.ExecCommand("Paste", false, null);
    UpdateButtons();
  }
}

Now, disable the Web Browser component’s default context menu by setting the IsWebBrowserContextMenuEnabled to false. Attach a custom ContextMenu to it, and add an Opening event handler that calls UpdateButtons:

private void tBrowserContextMenu_Opening(object sender, CancelEventArgs e)
{
  UpdateButtons();
}

You could also use IsCommandEnabled with SelectAll, Undo and Redo options.

September 9th, 2007 at 7:59 am

Nice!

bytescream
February 13th, 2014 at 7:10 am

Now under .NET 4.5 and a wrapper around the old mshtml:

using mshtml;

private mshtml.IHTMLDocument2 ThisDoc;

namespace PortalView
{
[PermissionSet(SecurityAction.Demand, Name = “FullTrust”)]
public partial class PortalView1 : Form
{

// when the document is loaded assign the old mshtml.IHTMLDocument2 value
// to the private property declared in this class.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Here is the glue between the old mshtml and the new webBrowser control
// so you can get to the object that has the queryCommandEnabled() method.

this.ThisDoc = (mshtml.IHTMLDocument2) this.webBrowser1.Document.DomDocument;
}

private void UpdateButtons()
{
// Button states are handled here.
cutToolStripMenuItem1.Enabled = IsCommandEnabled(“Cut”);
copyToolStripMenuItem1.Enabled = IsCommandEnabled(“Copy”);
pasteToolStripMenuItem1.Enabled = IsCommandEnabled(“Paste”);
}

// Disables the Back button at the beginning of the navigation history.
private bool IsCommandEnabled(string wCmd)
{

if ( this.ThisDoc != null )
return this.ThisDoc.queryCommandEnabled(wCmd);
return false;
}

}

guest
June 8th, 2016 at 10:55 pm

Dude. I know this is super old, but it is exactly what I was looking for! I knew this had to be possible. You really helped me out by publishing this…

LEAVE A COMMENT

theme by teslathemes