取得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
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
Form1.List1.AddItem GetWinText End Function 相信大家看了上面的代码应该知道取得"任意窗口值"的原理,本程序在 VB6.0、Windows 98 下运行良好。--------------------------------------------------问如何取得当前运行的ie的标题(存放到字符串中)。 以及如何关闭当前的ie. 首先在工程中加入对Microsoft Internet Controls的引用 代码: Dim WithEvents objIEMain As WebBrowser_V1 Dim objIE As Object Dim dWinFolder As New ShellWindows Dim objDoc As Object
For Each objIE In dWinFolder List1.AddItem objIE.Document.Title List2.AddItem objIE.LocationURL Next 就可以在List1中列出所有IE文档的标题了。 下面是关闭其中一个窗口的代码: If InStr(objIE.Document.Title, "Apple") Then objIE.Quit End If
上面当然不行,IE中的内容只是它自己"画"出来的,不是窗体,没有句柄首先安装一个ActiveX控件-Microsoft Shell Controls and AutomationIShellWindows,IWebBrowser2,IHTMLDocument2...一级级接口下去,可以控制任何Web页中的 东西或者写一个IE进程内的COM组件,通过IObjectWithSite获得IWebBrowser2,再获得其它接口控制Web页中的各种对象这些MSDN里都有,说的再多不如自己去看MSDN
大致就是这个意思,程序中的myie你应该通过遍历的方法获得,而不是象我这样: Option Explicit Dim myie As New InternetExplorerPrivate Sub Command1_Click() myie.Visible = True myie.Navigate2 "http://mail.163.com" End SubPrivate Sub Command2_Click() Dim vDoc, vTag Dim i As Integer Set vDoc = myie.document List1.Clear For i = 0 To vDoc.All.length - 1 If UCase(vDoc.All(i).tagName) = "INPUT" Then Set vTag = vDoc.All(i) List1.AddItem vTag.Value End If Next i End Sub
In order to run the following code, it is necessary to add a reference to "Microsoft Internet Controls" (Shdocvw.dll) and "Microsoft HTML Object Library" (Mshtml.dll) to the Visual Basic project: Dim SWs As New SHDocVw.ShellWindows Dim IE As SHDocVw.InternetExplorerPrivate Sub Form_Load() Dim Doc List1.Clear List2.Clear Text1.Text = SWs.count For Each IE In SWs Set Doc = IE.Document If TypeOf Doc Is HTMLDocument Then '中间的暴风雨写出来了,我小偷一下懒 End If Next End Sub
作者:未知 通过实例学习窗口函数---取得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 下运行良好。--------------------------------------------------问如何取得当前运行的ie的标题(存放到字符串中)。
以及如何关闭当前的ie.
首先在工程中加入对Microsoft Internet Controls的引用
代码:
Dim WithEvents objIEMain As WebBrowser_V1
Dim objIE As Object
Dim dWinFolder As New ShellWindows
Dim objDoc As Object
For Each objIE In dWinFolder
List1.AddItem objIE.Document.Title
List2.AddItem objIE.LocationURL
Next
就可以在List1中列出所有IE文档的标题了。 下面是关闭其中一个窗口的代码:
If InStr(objIE.Document.Title, "Apple") Then
objIE.Quit
End If
___________________________________________________________________________
我不知道如何获得某个textbox的value,请帮忙。
东西或者写一个IE进程内的COM组件,通过IObjectWithSite获得IWebBrowser2,再获得其它接口控制Web页中的各种对象这些MSDN里都有,说的再多不如自己去看MSDN
Option Explicit
Dim myie As New InternetExplorerPrivate Sub Command1_Click()
myie.Visible = True
myie.Navigate2 "http://mail.163.com"
End SubPrivate Sub Command2_Click()
Dim vDoc, vTag
Dim i As Integer
Set vDoc = myie.document
List1.Clear
For i = 0 To vDoc.All.length - 1
If UCase(vDoc.All(i).tagName) = "INPUT" Then
Set vTag = vDoc.All(i)
List1.AddItem vTag.Value
End If
Next i
End Sub
我遍历的时候如何判断是当前当前IE呢?
http://www.china-askpro.com/msg43/qa65.shtml
Dim IE As SHDocVw.InternetExplorerPrivate Sub Form_Load()
Dim Doc
List1.Clear
List2.Clear Text1.Text = SWs.count For Each IE In SWs
Set Doc = IE.Document
If TypeOf Doc Is HTMLDocument Then
'中间的暴风雨写出来了,我小偷一下懒
End If
Next
End Sub