原贴100分
http://community.csdn.net/Expert/TopicView1.asp?id=5442852答对的两边都给分
我的需求,获取当前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对象呢?