4) 应该在程序命名空间声明之前,包含下面的接口声明以添加对Microsoft HTML (MSHTML) IOleCommandTarget接口的参照引用:
Visual C#打造多页面网页浏览器
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct OLECMDTEXT
{
public uint cmdtextf;
public uint cwActual;
public uint cwBuf;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]public char rgwz;
}
[StructLayout(LayoutKind.Sequential)]
public struct OLECMD
{
public uint cmdID;
public uint cmdf;
}
// IOleCommandTarget的Interop定义
[ComImport,
Guid("b722bccb-4e68-101b-a2bc-00aa00404770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleCommandTarget
{
void QueryStatus(ref Guid pguidCmdGroup, UInt32 cCmds,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)]
OLECMD[] prgCmds, ref OLECMDTEXT CmdText);
void Exec(ref Guid pguidCmdGroup, uint nCmdId,
uint nCmdExecOpt, ref object pvaIn, ref object pvaOut);
}
2、为CGID_IWebBrowser定义一个GUID
private Guid cmdGuid = new Guid("ED016940-BD5B-11CF-BA4E-00C04FD70816");
private enum MiscCommandTarget { Find = 1, ViewSource, Options }
3、调用Exec()方法
private mshtml.HTMLDocument GetDocument()
{
try
{
mshtml.HTMLDocument htm = (mshtml.HTMLDocument)axWebBrowser2.Document;
return htm;
}
catch
{
throw (new Exception("不能从WebBrowser控件中获取文件对象"));
}
}
//查看源码的方法
public void ViewSource()
{
IOleCommandTarget cmdt;
Object o = new object();
try
{
cmdt = (IOleCommandTarget)GetDocument();
cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.ViewSource,
(uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
}
public void Find()
{
IOleCommandTarget cmdt;
Object o = new object();
try
{
cmdt = (IOleCommandTarget)GetDocument();
cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Find,
(uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
}
//显示“选项”对话框的方法
public void InternetOptions()
{
IOleCommandTarget cmdt;
Object o = new object();
try
{
cmdt = (IOleCommandTarget)GetDocument();
cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Options,
(uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
}
catch
{
//注意:因为该过程相应的CMDID是在Internet Explorer处理
//所以此处的异常代码块将总被激活,即使该对话框及其操作成功。
//当然,你可以通过浏览器选择设置来禁止这种错误的出现。
//不过,即使出现这种提示,对你的主机也无任何损害。
}
}
总结
0
相关文章