C#通过Win32 API操作IE浏览器 --- 获得IE的URL

获取IE浏览器URL
本文介绍了一种使用C#调用Win32 API的方法,来获取当前打开的IE浏览器窗口的URL。该方法适用于IE6和IE7,并展示了针对不同版本IE浏览器的窗口类名差异。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //操作Win32API必须要引入的命名空间

namespace IEManipulation
{
    
public class IEManipulation
    {
        [DllImport(
"User32.dll")] //User32.dll是Windows操作系统的核心动态库之一
         static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport(
"User32.dll")]
         
static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport(
"User32.dll")]
         
static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);
        [DllImport(
"User32.dll")]
         
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);

        
const int WM_GETTEXT = 0x000D//获得文本消息的16进制表示

        
/// <summary>
        
/// Get the URL of the current opened IE
        
/// </summary>
        public static string GetURL() 
        {
            
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;
        }
    }
}
IE6和IE7的访问方式不太一样,IE7要改为:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //操作Win32API必须要引入的命名空间

namespace IEManipulation
{
    
public class IEManipulation
    {
        [DllImport(
"User32.dll")] //User32.dll是Windows操作系统的核心动态库之一
         static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport(
"User32.dll")]
         
static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport(
"User32.dll")]
         
static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);
        [DllImport(
"User32.dll")]
         
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam);

        
const int WM_GETTEXT = 0x000D//获得文本消息的16进制表示

        
/// <summary>
        
/// Get the URL of the current opened IE
        
/// </summary>
        public static string GetURL() 
        {
            
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;
        }
    }
}
大家都知道, Windows API是Windows操作系统的一系列的底层函数,是操作系统提供给用户进入操作系统核心进行高级编程的途径, 虽然微软在 Microsoft Visual Studio 6.0 及以下版本中提供了一个用于API浏览的API Text Viewer, 不过功能十分简陋, 部分声明有错误, 而且提供不够充足;而在 Microsoft Visual Studio 7.0 (.net) 及以上版本中为了达到跨平台、安全等一系列新特性,微软已经写好了一个内容丰富的受管制的代码类集合(.net基类库), 来完成以前要通过Windows API来完成的绝大多数任务, 虽然如此但还是有相当一部分功能必需调用Windows API来完成(如果你是VB.net或C#使用者, 强烈建议你尽可能使用.net基类库来完成你的工作); MSDN 中只有部分API的使用帮助, 而且并没有可立即复制到IDE中的API声明, 要使用某个函数不但要很好的了解这个函数的调用方法, 而且需要手动把声明转换成相应语言的API声明方式。于是我就将自己以前写的API浏览软件进行了适当的改动,让它提供上面的需求。它与 Microsoft Visual Studio 6.0 自带的浏览器相比有如下增强: 1、函数、类型、常数可自由删除、添加、编辑。 2、自动代码整理功能可免去你把声明复制到IDE后还需手工整理的麻烦。 3、雷达功能,可查看任意窗口句柄与类名,可作编程时的辅助工具。 4、在函数中能实现类型自动探测、搜索、添加。 5、在查询中支持使用通配符"?"、"*"、"[]"、"[^]"来进行代替一个或多个字符,包含不包含指定字符来查询。 6、可直接将函数、类型、常数转换成VB.net/C#/易语言声明方式。 7、添加函数时可智能自动添加与函数相关的类型、注释, 添加类型时自动添加类型中的类型, 类型中的常数, 常数中的常数功能, 大大加快了函数与相关类型、常数的复制添加速度。 8、可以根据功能分组浏览声明。   9、收藏夹功能,可将一组(功能相关)API声明添加到收藏夹。 10、颜色采集功能,可以采集当前屏幕上指定点的颜色。 11、可查看同api功能的.net类库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值