我想出这样一个思路:你先用SPY++找到IE的浏览器窗口类名,然后用FindWindowsEx获得它的窗口句柄,这样就可以获得一个CHtmlView类指针。然后你可以使用IWebBrowser2接口的get_LocationURL(BSTR *pbstrLocationURL);方法获取当前的URL,地址的变化可以通过BeforeNavigate2 事件获得。你试一下,看是否可行。

解决方案 »

  1.   

    to haiwangstar() :可否给一个实例?
      

  2.   

    To haiwangstar()
    我以前试过,IE报错,或者自己的程式报错的几率远大于正常情况,天知道为什么。
      

  3.   

    using System;
    using System.Diagnostics ;
    using System.Runtime.InteropServices;
    using System.Collections;
    using System.Net .Sockets ;
    namespace fupip 
    {
    public class geturl 
    {
    public geturl()
    {

    }
    [DllImport("user32.dll")]
    public  static extern int  GetClassName(int  hwnd, byte[]  lpClassName,Int32[]   nMaxCount);
    [DllImport("user32.dll")]
    public  static extern int  GetWindow(int  hwnd,int  wCmd); [DllImport("user32.dll")]
    public  static extern int  SendMessage(int  hwnd,int  wMsg,int  wParam, byte[]   lParam);
    public const int GW_CHILD = 5;
    public const int GW_HWNDNEXT = 2;
    public const int WM_GETTEXT =13;
    public const int WM_GETTEXTLENGTH = 14; public static string[] GetIEURL(string args)
    {

    string[] urls;

    Process[] ps =Process.GetProcessesByName(args);
    urls=new string[ps.Length];
    for(int i=0;i<ps.Length;i++)
    {
    int hwndIE=(int)ps[i].MainWindowHandle; int hwndEdit=getkid(hwndIE);    
    urls[i]= gettext(hwndEdit);
       
       }
    return urls;
    }
    static int getkid(int hwnd)
    {        
      byte[] str=new byte[100];  
      Int32[] len=new Int32[1];  
      int hwndkid=0;
      string classname;
      int handle=0;
      len[0]=100;  
      ArrayList al=new ArrayList();
         GetClassName(hwnd,str,len);
      classname=System.Text.Encoding.ASCII.GetString(str); 
    if(classname.Substring(0,4)=="Edit")
    {
      
       handle=  hwnd;
    }

      hwndkid=GetWindow(hwnd,GW_CHILD);
     
      while(hwndkid!=0)
      {
      al.Add(hwndkid);
      hwndkid=GetWindow(hwndkid,GW_HWNDNEXT);
      
      }

      foreach(int kid in al)
      {
       int i=getkid(kid);
       if(i!=0)
       handle=i;
       
      
      }  return handle;

    }
       static string gettext(int hwnd)
       {
    int txtlen;
    byte[] txt;
    string text;
    txtlen = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, new byte[]{0});

    if(txtlen==0)
    return "";
    txt=new byte[txtlen+1];
    txtlen = SendMessage(hwnd, WM_GETTEXT, txtlen+1,  txt);

    text=System.Text.Encoding.Default.GetString(txt); 

    return text;    }
    }}
      

  4.   

    to fupip(小贝) :非常感谢!
    但是有个问题,有时候IE的进程有2个,但是我的页面却有5个,请教,这个问什么?而且我不断的重新获取,有时候得到的URL地址却不一样,请教,这个该怎么处理?
      

  5.   

    to fupip(小贝) :还有,我如何能对IE发送我自己定义的信息(也就是在页面上显示我定制的信息)?根据得到的进程号,我是否可以杀死不同的IE进程?