日期:2014-05-19  浏览次数:20847 次

我想找到当前IE(7)当前的文档对象,已有部分代码,请求大家帮忙!C#_WinFrom
我的需求,获取当前IE窗口的IHTMLDocument2对象来操作IE文档,如果是IE7,因为有多个Tab的情况,需要找到当前的IHTMLDocument2对象
问题:怎样找到当前IE窗口及当前的IHTMLDocument2对象呢?
已有代码提示:
SHDocVw.WebBrowser   m_browser   =   null;
SHDocVw.ShellWindows   shellWindows   =   new   SHDocVw.ShellWindowsClass();
string   filename;
foreach   (SHDocVw.WebBrowser   ie   in   shellWindows)
{
      filename   =   Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

      if   (filename.Equals( "iexplore "))
      {
      m_browser   =   ie;
      break;
      }
}


mshtml.IHTMLDocument2   myDoc   =   (mshtml.IHTMLDocument2)m_browser.Document;
但这种方式获取到的IHTMLDocument2不一定是当前激活Tab的IE7的文档,


IntPtr   hwnd   =   (IntPtr)m_browser.HWND;
hwnd   =   FindWindowEx(hwnd,   IntPtr.Zero,   "TabWindowClass ",   IntPtr.Zero);
IntPtr   HchildWND   =   FindWindowEx(hwnd,   (IntPtr)0,   "Shell   DocObject   View ",   IntPtr.Zero);
if   (HchildWND   !=   (IntPtr)0)
{
        string   c   =   "Internet   Explorer_Server ";
        HchildWND   =   FindWindowEx(HchildWND,   (IntPtr)0,   c,   IntPtr.Zero);
}
到这一步可以访问到当前文档窗口的句柄,但是接下来该如何通过这个句柄获得IHTMLDocument2对象呢?

请各位高手支招,给出有效代码或者参考的的给分,谢谢阿

------解决方案--------------------
up
------解决方案--------------------

------解决方案--------------------
分...
------解决方案--------------------
up
------解决方案--------------------
public static IHTMLDocument2 GetDomFromHwnd(IntPtr hwnd)
{
object obj1 = RuntimeHelpers.GetObjectValue(new object());
Guid guid1 = new Guid( "626FC520-A41E-11CF-A731-00A0C9082637 ");
string text1 = "WM_Html_GETOBJECT ";
int num2 = RegisterWindowMessage(ref text1);
if (num2 == 0)
{
throw new Exception( "Unable to register windows message in getDOMFromHwnd() ");
}
IntPtr ptr1 = SendMessage(hwnd, num2, IntPtr.Zero, IntPtr.Zero);
int num1 = ObjectFromLresult(ptr1, ref guid1, IntPtr.Zero, out obj1);
if (num1 != 0)
{
throw new Exception( "Unable to get IE DOM from window message result in getDOMFromHwnd() ");
}
return (IHTMLDocument2)obj1;

}