获得IHTMLDocument2
时间:2019/5/24 10:43:40

  private void button4_Click(object sender, EventArgs e)
        {
              IntPtr ParenthWndldsl = new IntPtr(0);
               IntPtr EdithWnd = new IntPtr(0);
               Thread.Sleep(200);
               ParenthWndldsl = FindWindow(null, "电话服务平台1.01 - [欢迎使用]");

               if (!ParenthWndldsl.Equals(IntPtr.Zero))
               {
                   //得到User Name这个子窗体,并设置其内容
                   EdithWnd = FindWindowEx(ParenthWndldsl, IntPtr.Zero, "MDIClient", "");
                   IntPtr e1 = FindWindowEx(EdithWnd, IntPtr.Zero, "TForm_welcome", "欢迎使用");
                   IntPtr e2 = FindWindowEx(e1, IntPtr.Zero, "Shell Embedding", "");
                   IntPtr e3 = FindWindowEx(e2, IntPtr.Zero, "Shell DocObject View", "");
                   IntPtr e4 = FindWindowEx(e3, IntPtr.Zero, "Internet Explorer_Server", "");

 

 


                    mshtml.IHTMLDocument2 id = getIEWindowDocument(int.Parse(e4.ToString()));//上边代码是获得句柄的
               IHTMLElementCollection links = id.links;
               foreach (mshtml.IHTMLElement k in links)
               {
                  // mshtml.HTMLAnchorElementClass ks = k.GetType();
                 //  string aa = ((mshtml.HTMLAnchorElementClass)(k)).href;
                   string aaa = k.outerHTML;//这个是指定单击
                   if (aaa=="<AREA title=电话受理 href="+"\"$dhsl\""+" shape=rect coords=188,137,251,210>")
                   {
                       k.click();
                   }
                   //mshtml.HTMLAnchorElementClass ks = (mshtml.HTMLAnchorElementClass)(k);
                 
               }

                  
               }
              
        }

 

  class Win32API
        {
            [DllImport("User32.dll")] //User32.dll是Windows操作系统的核心动态库之一
            public static extern int FindWindow(string lpClassName, string lpWindowName);


            [DllImport("user32", EntryPoint = "RegisterWindowMessage")]
            public static extern int RegisterWindowMessage(
                    string lpString
            );

            [DllImport("user32", EntryPoint = "SendMessage")]
            public static extern int SendMessage(
                    int hwnd,
                    int wMsg,
                    int wParam,
                    ref int lParam
            );

            [DllImport("OLEACC.DLL", EntryPoint = "ObjectFromLresult")]
            public static extern int ObjectFromLresult(
            int lResult,
            ref System.Guid riid,
            int wParam,
            [System.Runtime.InteropServices.MarshalAs(UnmanagedType.Interface), System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out]ref System.Object ppvObject
                //注意这个函数ObjectFromLresult的声明.
            );
        }


      
            public mshtml.IHTMLDocument2 getIEWindowDocument(int jubing)
            {
                System.Object domObject = new System.Object();
                int tempInt = 0;
                System.Guid guidIEDocument2 = new Guid(); //应该是IHTMLDocument2的接口id
                int WM_Html_GETOBJECT = Win32API.RegisterWindowMessage("WM_Html_GETOBJECT");


                //开始查找指定的ie窗体
                int hwnd = jubing;

                int W = Win32API.SendMessage(hwnd, WM_Html_GETOBJECT, 0, ref tempInt);
                int lreturn = Win32API.ObjectFromLresult(W, ref guidIEDocument2, 0, ref domObject);
                mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)domObject;

                //MessageBox.Show(doc.toString());
                MessageBox.Show("到这里2");
                MessageBox.Show(doc.url);
                MessageBox.Show(doc.title);

                return doc;
            }