获取 IE 当前 URL 的代码

博客介绍了获取IE当前URL的代码,指出网上类似代码在WINDOWS XP下不能运行,原因是Win2000、WINXP基于Unicode代码,以WorkerW类取代WorkerA类。还给出了具体代码,通过一系列FindWindowEx函数获取句柄,最终获取并显示IE当前网址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取 IE 当前 URL 的代码,网上有许多类似代码,但在WINDOWSXP 下不能运行。查了一些资料,发现由于Win2000,WINXP 是基于Unicode代码的操作系统,所以没有WorkerA类,而以WorkerW类取而代之(XXXXA should be used on not unicode compliant windows oses likes Windows 95,98 etc and on unicode enabled oses replace A with W. Remember WorkerA or WorkerW doesn't have something related to IE version. To obtain all of the opened IEs URL use EnumWindows callback function and cheers.

Option Explicit


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 'Findwindow函数的功能是找到当前运行的IE窗口的url地址的句柄

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long 'FindwindowEx函数的功能是找到子窗体的句柄

Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

Private Const WM_GETTEXT = &HD


Private Sub Command1_Click()

getcurrenturl

End Sub

Sub getcurrenturl(Optional ByRef URL As String)

Dim hwnd As Long '设定一个长整形变量用来接收函数返回值

hwnd = 0 '初始化

hwnd = FindWindowEx(hwnd, 0, "IEFrame", vbNullString) 'IE窗口句柄

hwnd = FindWindowEx(hwnd, 0, "Workerw", vbNullString) 'IE窗口的工作区句柄

hwnd = FindWindowEx(hwnd, 0, "ReBarWindow32", vbNullString) 'IE窗口的菜单栏句柄

hwnd = FindWindowEx(hwnd, 0, "ComboBoxEx32", vbNullString) 'IE窗口下拉菜单句柄

hwnd = FindWindowEx(hwnd, 0, "ComboBox", vbNullString) 'IE窗口下拉菜单当前项句柄

hwnd = FindWindowEx(hwnd, 0, "Edit", vbNullString) ''IE窗口下拉菜单编辑框句柄

URL = String(1024, Chr(0)) '初始化字符串

SendMessageByString hwnd, WM_GETTEXT, 1025, URL '向系统发送获得IE窗体地址栏中的字符串命令

URL = Split(URL, Chr(0))(0) '根据 URL 长度得到 URL 值

MsgBox URL '显示IE当前网址

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值