在VB中如何取得当前运行的IE窗口中的url地址!急!决解就给分啊!

pbfighter 2002-10-16 08:53:55
我想在我的程序运行时,具有IE收藏夹的功能能够取得当前活动的IE窗口中的url地址,并进行收藏取个自定义的标题。
...全文
557 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
三楼の郎 2002-10-21
  • 打赏
  • 举报
回复
文章来源:杂牌军军营技术特区
作者:(我不知)

取得IE地址栏的地址
通过实例学习窗口函数---取得IE地址栏的地址

  大家知道在 Windows 中,每个程序都是窗口的形式,不管是隐藏的还是显示的,都可以取得它们的句柄,而每一个程序中的控件又都是一个一个的子窗口,同样也可以取得它们的句柄,这样在理论坛上可以取得任意程序中任意位置的值,这里我们就以取得 IE 浏览器地址栏中的地址为例子,在下面的例子中我分别自定义的 3 个函数:
1、EnumProc `遍查主窗口
2、GetZiWin `遍查子窗口
3、GetWinText `取得指定句柄的值

  这 3 个函数只要做一定的修改,就可以在你任意的程序中单独使用,最后希望大家通过这个例子能够掌握窗口函数的基础技巧。

程序界面:
如图1所示,装载1个CommandButton(Caption为取得IE地址栏的地址)、1个ListBox控件,其他属性全部为默认。

程序代码:

'Form1.frm 文件
'--------------
Option Explicit
Private Sub Command1_Click()
 List1.Clear
 EnumWindows AddressOf EnumProc, 0
 If List1.ListCount = 0 Then List1.AddItem "没有启动 IE 浏览器"
End Sub

'Module1.bas 文件
'---------------
Option Explicit
'相关 API 函数声明
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam As Long) _
As Long '枚举窗口列表中的所有父窗口(顶级和被所有窗口)
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, ByVal cch As Long) As Long '取得指定窗口的司法题
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal _
lpClassName As String, ByVal nMaxCount As Long) As Long '为指定的窗口取得类名
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long '取得窗口句柄
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, lParam As Any) As Long '发送消息

Const GW_CHILD = 5
Const GW_HWNDNEXT = 2
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE

'遍查主窗口
Public Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean
Dim buf As String * 1024
Dim length As Long
Dim title As String

 length = GetWindowText(app_hwnd, buf, Len(buf))
 title = Left$(buf, length)

 '判断是否为 IE 浏览器窗口
 If InStr(title, " - Netscape") Or InStr(title, " - Microsoft Internet Explorer") Or InStr(title, "Offline Explorer") Then
  Call GetZiWin(app_hwnd)
 End If
 
 EnumProc = 1
End Function

'遍查子窗口
Public Function GetZiWin(window_hwnd As Long) As String
Dim buf As String
Dim buflen As Long
Dim child_hwnd As Long
Dim children() As Long
Dim num_children As Integer
Dim i As Integer
  
 buflen = 256
 buf = Space$(buflen - 1)
 buflen = GetClassName(window_hwnd, buf, buflen)
 buf = Left$(buf, buflen) '取得子窗口的类名
 
 If Right(buf, 4) = "Edit" Then '判断是否为地址栏子窗口
  GetZiWin = GetWinText(window_hwnd)
  Exit Function
 End If
  
 num_children = 0
 child_hwnd = GetWindow(window_hwnd, GW_CHILD) '取得第 1 个子窗口的句柄
 Do While child_hwnd <> 0 '如果有子窗口
  num_children = num_children + 1
  ReDim Preserve children(1 To num_children)
  children(num_children) = child_hwnd
  child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT) '取得下一个兄弟窗口的句柄
 Loop
  
 For i = 1 To num_children
  Call GetZiWin(children(i))
 Next i
End Function

Public Function GetWinText(window_hwnd As Long) As String '取得子窗口的值
Dim txtlen As Long
Dim txt As String

 '通过 SendMessage 发送 WM_GETTEXT 取得 IE 地址栏的值
 GetWinText = ""
 If window_hwnd = 0 Then Exit Function
  
 txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, 0)
 If txtlen = 0 Then Exit Function
  
 txtlen = txtlen + 1
 txt = Space$(txtlen)
 txtlen = SendMessage(window_hwnd, WM_GETTEXT, txtlen, ByVal txt)
 GetWinText = Left$(txt, txtlen)
 
 Form1.List1.AddItem GetWinText
End Function

  相信大家看了上面的代码应该知道取得"任意窗口值"的原理,本程序在 VB6.0、Windows 98 下运行良好。
TechnoFantasy 2002-10-16
  • 打赏
  • 举报
回复
怎样编程得到当前Web窗口文本信息
Dim dWinFolder As New ShellWindows
Dim WithEvents eventIE As WebBrowser_V1

Private Sub Command1_Click()
Dim objIE As Object

For Each objIE In dWinFolder
If objIE.LocationURL = List1.List(List1.ListIndex) Then
Set eventIE = objIE
Command1.Enabled = False
List1.Enabled = False
Text1.Text = ""
Exit For
End If
Next
End Sub

Private Sub eventIE_NavigateComplete(ByVal URL As String)
Text1.Text = Text1.Text + Chr(13) + Chr(10) + URL
End Sub

在运行前。点击菜单 Projects ¦ References 项,在Available References 列表中选择Microsoft Internet Controls项将Internet对象引用介入到工程中

Private Sub Form_Load()
Dim objIE As Object

For Each objIE In dWinFolder
If InStr(1, objIE.FullName, "IEXPLORE.EXE", vbTextCompare) <> 0 Then
List1.AddItem objIE.LocationURL
End If
Next
Command1.Caption = "正文"
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set dWinFolder = Nothing
End Sub

Private Sub List1_Click()
Dim objDoc As Object
Dim objIE As Object

For Each objIE In dWinFolder
If objIE.LocationURL = List1.List(List1.ListIndex) Then
Set objDoc = objIE.Document

For i = 1 To objDoc.All.length - 1
If objDoc.All(i).tagname = "BODY" Then
Text1.Text = objDoc.All(i).innerText
End If
Next
Exit For
End If
Next
End Sub
trytryba 2002-10-16
  • 打赏
  • 举报
回复
学习。
守城小轩 2002-10-16
  • 打赏
  • 举报
回复
我也在搞这方面的开发
上面的程序还不完善 你有什么好的想法 给我留言!!!
互相学习!
守城小轩 2002-10-16
  • 打赏
  • 举报
回复
新建一个工程。加入一个internet transfer contril 一个commandbutton
两个textbox
text1用来输入要取的网页的地址
text2用来输入要存文件的完整文件名
private sub command1_click()
dim b() as byte
inet1.cancel
inet1.protocol=ichttp
inet1.url=text1.text
b()=inet1.openurl(,icbytearray)
open text2.text for binary access write as #1
put #1,,b()
close #1
end sub
看看吧
radyljm 2002-10-16
  • 打赏
  • 举报
回复
绝对通过的程序。
先引用Microsoft Internet Controls
再写程序:
Dim dWinFolder As ShellWindows


Private Sub Command1_Click()
dim i as integer
List1.Clear
For i = o To dWinFolder.Count - 1
List1.AddItem dWinFolder.Item(i).LocationURL
Next i
End Sub

Private Sub Form_Load()
Set dWinFolder = New ShellWindows
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set dWinFolder = Nothing
End Sub

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧