U
user1335
Unregistered / Unconfirmed
GUEST, unregistred user!
其它在程序语言都有了访问网页中Frame的内容,找了很久C#关于这方面的内容,居然没有,看了其它语言写的程序,摸索了一阵,得知可用IHTMLDocument2的Frame访问内嵌网页,但没怎么用,用IOleContainer和IWebBrowser2也可以,但我写的还是出错了,好郁闷。不知为何,程序如果,提示“未将实例引用到对象”,result=container.EnumObjects((long)OLECONTF.OLECONTF_EMBEDDINGS,out searchenum);但用了out好像不用先赋值。请看程序先。
[ComImport,
Guid("00000100-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IEnumUnknown
{
void Next(long celt, out IUnknown rgelt, out long pceltFetched );
void Skip(long celt);
void Reset();
void Clone(out IEnumUnknown ppenum );
}
[ComImport,
Guid("0000000B-0000-0000-C000-000000000046")]
interface IUnknown
{
void QueryInterface(long iid,out int ppvObject);
long AddRef();
long Release();
}
[ComImport,
Guid("0000011B-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IOleContainer
{
[PreserveSig()]
int EnumObjects([In, MarshalAs(UnmanagedType.U8)] long grfFlags, [Out, MarshalAs(UnmanagedType.Interface)]out IEnumUnknown ppenum);
void LockContainer(bool fLock);
}
.................................
.................................
private enum OLECONTF: long {OLECONTF_EMBEDDINGS=1, OLECONTF_LINKS,OLECONTF_OTHERS ,OLECONTF_ONLYUSER ,OLECONTF_ONLYIFRUNNING } ;
..........................
..........................
private void button1_Click(object sender, System.EventArgs e)
{
IWebBrowser2 myweb=this.getfrmae(0);
IHTMLDocument2 mydocument=(IHTMLDocument2)(myweb.Document);
this.richTextBox1.Text=mydocument.body.innerHTML;
}
private IWebBrowser2 getfrmae(long Frameno)
{
int result;
IEnumUnknown searchenum;
IOleContainer container;
IUnknown unkframe;
long Fetched;
container=(IOleContainer)(this.axWebBrowser1.Document);
result=container.EnumObjects((long)OLECONTF.OLECONTF_EMBEDDINGS,out searchenum);
if(result==0)
{
MessageBox.Show("没有内嵌框架");
return null;
}
searchenum.Skip(Frameno);
searchenum.Next((long)(OLECONTF.OLECONTF_EMBEDDINGS),out unkframe,out Fetched);
return (IWebBrowser2)(unkframe);
}
想到测试访问内嵌网页的内容放到RichtextBoxj里,在result=container.EnumObjects((long)OLECONTF.OLECONTF_EMBEDDINGS,out searchenum); 就出错了。大家帮忙把程序拷贝下来测试下,帮忙看看什么问题。如果有谁写过用IHTMLDocument2访问的也可以写出来给大家看下,谢谢各位。
[ComImport,
Guid("00000100-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IEnumUnknown
{
void Next(long celt, out IUnknown rgelt, out long pceltFetched );
void Skip(long celt);
void Reset();
void Clone(out IEnumUnknown ppenum );
}
[ComImport,
Guid("0000000B-0000-0000-C000-000000000046")]
interface IUnknown
{
void QueryInterface(long iid,out int ppvObject);
long AddRef();
long Release();
}
[ComImport,
Guid("0000011B-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IOleContainer
{
[PreserveSig()]
int EnumObjects([In, MarshalAs(UnmanagedType.U8)] long grfFlags, [Out, MarshalAs(UnmanagedType.Interface)]out IEnumUnknown ppenum);
void LockContainer(bool fLock);
}
.................................
.................................
private enum OLECONTF: long {OLECONTF_EMBEDDINGS=1, OLECONTF_LINKS,OLECONTF_OTHERS ,OLECONTF_ONLYUSER ,OLECONTF_ONLYIFRUNNING } ;
..........................
..........................
private void button1_Click(object sender, System.EventArgs e)
{
IWebBrowser2 myweb=this.getfrmae(0);
IHTMLDocument2 mydocument=(IHTMLDocument2)(myweb.Document);
this.richTextBox1.Text=mydocument.body.innerHTML;
}
private IWebBrowser2 getfrmae(long Frameno)
{
int result;
IEnumUnknown searchenum;
IOleContainer container;
IUnknown unkframe;
long Fetched;
container=(IOleContainer)(this.axWebBrowser1.Document);
result=container.EnumObjects((long)OLECONTF.OLECONTF_EMBEDDINGS,out searchenum);
if(result==0)
{
MessageBox.Show("没有内嵌框架");
return null;
}
searchenum.Skip(Frameno);
searchenum.Next((long)(OLECONTF.OLECONTF_EMBEDDINGS),out unkframe,out Fetched);
return (IWebBrowser2)(unkframe);
}
想到测试访问内嵌网页的内容放到RichtextBoxj里,在result=container.EnumObjects((long)OLECONTF.OLECONTF_EMBEDDINGS,out searchenum); 就出错了。大家帮忙把程序拷贝下来测试下,帮忙看看什么问题。如果有谁写过用IHTMLDocument2访问的也可以写出来给大家看下,谢谢各位。