C# 教您一步一步从IE窗口里获取document对象
1, 用Win32 API 或是UI Atomation 获取窗口, 窗口的类型为"Internet Explorer_Server"
2。 添加引用: MSHTML, SHDocVw(Microsoft Internet Controls) , Accessibility
3。 导入API AccessibleObjectFromWindow: 您可以参考pInvoke.net 获取更多有用信息。
4。从窗获取 Acc: (hIENativeWindowHandle 是第一步里取得的窗口指针)
object objAcc = null;
Guid guidAcc = typeof( IAccessible).GUID;
AccessibilityAPI.AccessibleObjectFromWindow( (IntPtr)hIENativeWindowHandle,
0,
ref guidAcc,
ref objAcc);
IAccessible oAccIE = objAcc as IAccessible;
5。 获取IServiceProvider
IServiceProvider oServiceProvider = oAccIE as IServiceProvider
6. 取Document
object objHTMLWin2 = null;
Guid guidHTML2 = typeof(mshtml.IHTMLWindow2).GUID;
oServiceProvider.QueryService(ref guidHTML2, ref guidHTML2, out objHTMLWin2 )
IHTMLWindow2 oIEWin2 = objHTMLWin2 as IHTMLWindow2;
IHTMLDocument2 docRoot = oIEWin2.document as IHTMLDocument2;
以上代码在VS 2008, Vista上通过。