//How to invoke commands on the Web Browser control such as
//Find
//View Source
//Options Dialog
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct OLECMDTEXT
{
public UInt32 cmdtextf;
public UInt32 cwActual;
public UInt32 cwBuf;
public char rgwz;
}
[StructLayout(LayoutKind.Sequential)]
public struct OLECMD
{
public long cmdID;
public UInt64 cmdf;
}
// Interop definition for IOleCommandTarget.
[ComImport(), Guid("b722bccb-4f68-101b-a2bc-00fa00404770"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleCommandTarget
{
void QueryStatus(ref Guid pguidCmdGroup, UInt32 cCmds, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] OLECMD prgCmds, ref OLECMDTEXT pCmdText);
void Exec(ref Guid pguidCmdGroup, long nCmdId, long nCmdExecOpt, ref object pvaIn, ref object pvaOut);
}
private Guid cmdGUID = new Guid("ed016940-bd5b-11cf-ba4e-00c04fd70816");
private enum MiscCommandTarget
{
Find = 1,
ViewSource,
Options,
}
private object GetDocument()
{
try
{
return this.axWebBrowser1.Document;
}
catch(Exception ex)
{
throw new Exception("Cannot retrieve document from WebBrowser Control: " + ex.Message);
}
}
public void ViewSource()
{
IOleCommandTarget cmdt;
object o = null;
try
{
cmdt = (IOleCommandTarget) GetDocument();
cmdt.Exec(ref cmdGUID, (long) MiscCommandTarget.ViewSource, (long) SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
public void Find()
{
IOleCommandTarget cmdt;
object o = null;
try
{
cmdt = (IOleCommandTarget) GetDocument();
cmdt.Exec(ref cmdGUID, (long) MiscCommandTarget.Find, (long) SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
public void InternetOptions()
{
IOleCommandTarget cmdt;
object o = null;
try
{
cmdt = (IOleCommandTarget) GetDocument();
cmdt.Exec(ref cmdGUID, (long) MiscCommandTarget.Options, (long) SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
}
catch{}
}
|