none
求助:C# 获得浏览器的URL问题,IE6.7都解决了,IE8不知道怎么搞。 RRS feed

  • 问题

  • 我在做一个软件,需要监测IE的地址栏的URL,我目前测试的IE6.IE7都能获得URL。

    但是IE8下面没有反应,求助高人帮忙解决下,我贴出我的代码,麻烦各位:

    using (Microsoft.Win32.RegistryKey versionKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Internet Explorer"))
                {
                    string version = versionKey.GetValue("Version").ToString();
                    if (version.Substring(0, 1) == "6")
                    {
                        int parent = FindWindow("IEFrame", null);
                        int child = FindWindowEx(parent, 0, "WorkerW", null);
                        child = FindWindowEx(child, 0, "ReBarWindow32", null);
                        child = FindWindowEx(child, 0, "ComboBoxEx32", null);
                        child = FindWindowEx(child, 0, "ComboBox", null);
                        child = FindWindowEx(child, 0, "Edit", null);   //通过SPY++获得地址栏的层次结构,然后一层一层获得
                        StringBuilder buffer = new StringBuilder(1024);

                        //child表示要操作窗体的句柄号
                        //WM_GETTEXT表示一个消息,怎么样来驱动窗体
                        //1024表示要获得text的大小
                        //buffer表示获得text的值存放在内存缓存中
                        int num = SendMessage(child, WM_GETTEXT, 1024, buffer);
                        string URL = buffer.ToString();
                        return URL;
                    }
                    else
                    {
                       
                        int parent = FindWindow("IEFrame", null);
                        int child = FindWindowEx(parent, 0, "WorkerW", null);
                        child = FindWindowEx(child, 0, "ReBarWindow32", null);
                        child = FindWindowEx(child, 0, ("Address Band Root"), null);
                        child = FindWindowEx(child, 0, "ComboBoxEx32", null);
                        child = FindWindowEx(child, 0, "ComboBox", null);
                        child = FindWindowEx(child, 0, "Edit", null);   //通过SPY++获得地址栏的层次结构,然后一层一层获得
                        StringBuilder buffer = new StringBuilder(1024);

                        //child表示要操作窗体的句柄号
                        //WM_GETTEXT表示一个消息,怎么样来驱动窗体
                        //1024表示要获得text的大小
                        //buffer表示获得text的值存放在内存缓存中
                        int num = SendMessage(child, WM_GETTEXT, 1024, buffer);
                        string URL = buffer.ToString();
                        return URL;
                    }
                }

    2010年7月16日 9:34

答案

  • 有更简单的办法:

    1.引用C:\Windows\System32\shdocvw.dll
    2.代码如下:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Threading;
    using SHDocVw;
    using System.IO;
    using Microsoft.Win32;

    namespace WindowsApplication6
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private ShellWindows shellWindows;
            private void Form1_Load(object sender, EventArgs e)
            {
                shellWindows = new ShellWindows();
                shellWindows.WindowRegistered += new DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);
            }
            private void shellWindows_WindowRegistered(int ICookie)
            {
                IEnumerator items = shellWindows.GetEnumerator();
                while (items.MoveNext())
                {
                    if (items.Current is InternetExplorer)
                    {
                        InternetExplorer ie = (InternetExplorer)items.Current;
                        ie.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(ie_BeforeNavigate2);
                    }
                }
            }
            private void ie_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
            {
                string url = URL.ToString();
            }
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                shellWindows.WindowRegistered -= new DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);
            }
        }
    }

    说明:程序会监控IE浏览器。

    2010年7月16日 11:55
  • 以下程序可以获得当前打开的IE浏览器数,和每个浏览器的当前URL。

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private ShellWindows shellWindows;
            private void Form1_Load(object sender, EventArgs e)
            {
                shellWindows = new ShellWindows();
                int openIECount = shellWindows.Count;
                IEnumerator items = shellWindows.GetEnumerator();
                while (items.MoveNext())
                {
                    if (items.Current is InternetExplorer)
                    {
                        InternetExplorer ie = (InternetExplorer)items.Current;
                        string url = ie.LocationURL;
                    }
                }

    2010年7月16日 12:16
  • 触发ie_BeforeNavigate2事件的只能是当前窗口。这个事件在你输入URL并回车后触发。
    2010年7月17日 6:05

全部回复

  • 有更简单的办法:

    1.引用C:\Windows\System32\shdocvw.dll
    2.代码如下:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Threading;
    using SHDocVw;
    using System.IO;
    using Microsoft.Win32;

    namespace WindowsApplication6
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private ShellWindows shellWindows;
            private void Form1_Load(object sender, EventArgs e)
            {
                shellWindows = new ShellWindows();
                shellWindows.WindowRegistered += new DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);
            }
            private void shellWindows_WindowRegistered(int ICookie)
            {
                IEnumerator items = shellWindows.GetEnumerator();
                while (items.MoveNext())
                {
                    if (items.Current is InternetExplorer)
                    {
                        InternetExplorer ie = (InternetExplorer)items.Current;
                        ie.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(ie_BeforeNavigate2);
                    }
                }
            }
            private void ie_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
            {
                string url = URL.ToString();
            }
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                shellWindows.WindowRegistered -= new DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);
            }
        }
    }

    说明:程序会监控IE浏览器。

    2010年7月16日 11:55
  • 以下程序可以获得当前打开的IE浏览器数,和每个浏览器的当前URL。

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private ShellWindows shellWindows;
            private void Form1_Load(object sender, EventArgs e)
            {
                shellWindows = new ShellWindows();
                int openIECount = shellWindows.Count;
                IEnumerator items = shellWindows.GetEnumerator();
                while (items.MoveNext())
                {
                    if (items.Current is InternetExplorer)
                    {
                        InternetExplorer ie = (InternetExplorer)items.Current;
                        string url = ie.LocationURL;
                    }
                }

    2010年7月16日 12:16
  • 你这个方法是可以实现遍历所有打开的IE的URL,可是我的那个方法是读出当前的这个获得焦点的窗口的URL,不需要循环的,而且我监测,也是监测当前获得焦点的这个窗口,判断的。

     

    不知道你还有什么思路,在你的这个方法上更进一步达到只获得当前的活动的窗口。。。

     

     

    2010年7月17日 1:55
  • 触发ie_BeforeNavigate2事件的只能是当前窗口。这个事件在你输入URL并回车后触发。
    2010年7月17日 6:05
  • 遍历只是在启动IE时挂接事件用。
    2010年7月17日 6:06