这有何难?用GetClassName函数获取这个HWND的类名称, 然后判断这个类名称是不是"ExploreWClass"就行了。Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As LongPrivate Sub Form_Load() Dim sName As String sName = Space(250) GetClassName &H80354, sName, 250 sName = Left(sName, InStr(sName, vbNullChar) - 1) If sName = "ExploreWClass" Then ... End If End Sub
这个代码还是有点问题,就是如何得到MOUSE下的句柄的父窗体的句柄有问题。 ------------------------ FORM: Option ExplicitPrivate Sub Form_Load() Timer1.Interval = 100 End SubPrivate Sub Timer1_Timer() Dim hWnd1 As Long Dim hWnd2 Dim Cur As POINTAPI GetCursorPos Cur hWnd1 = WindowFromPoint(Cur.x, Cur.y) hWnd2 = GetParent(hWnd1) If hWnd2 = 0 Then hWnd2 = hWnd1 EnumChildWindows GetParent(hWnd2), AddressOf WndEnumChildProc, 0& End Sub --------------------------- MODULE: Option ExplicitPublic Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd 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 EnumChildWindows Lib "user32" (ByVal hwndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long Public Declare Function WindowFromPoint Lib "user32" (ByVal x As Long, ByVal y As Long) As Long Public Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPublic Const WM_GETTEXT = &HD Public Const WM_GETTEXTLENGTH = &HEPublic Type POINTAPI x As Long y As Long End TypePublic Function WndEnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Long Dim bRet As Long Dim myStr As String * 50 bRet = GetClassName(hWnd, myStr, 50) If bRet <> 0 Then If UCase(Left(myStr, 4)) = "EDIT" Then Debug.Print GetText(hWnd) End If WndEnumChildProc = 1 End FunctionFunction GetText(ByVal hWnd As Long) As String Dim Textlen As Long Dim Text As String Textlen = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0) If Textlen = 0 Then Exit Function Textlen = Textlen + 1 Text = Space(Textlen) Textlen = SendMessage(hWnd, WM_GETTEXT, Textlen, ByVal Text) GetText = Left(Text, Textlen) End Function
呵呵,我写的TIMER是一秒运行一次,就是太占系统资源了。你再加个MOUSE的操作。 ----------------- Option ExplicitPublic Declare Function WindowFromPoint Lib "user32" (ByVal x As Long, ByVal y As Long) As Long Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Public Type POINTAPI x As Long y As Long End TypePublic Type DGfor3 ponX As Long ponY As Long MDC As Long End TypePublic Function MouseDC() As DGfor3 On Error Resume Next Dim Cur As POINTAPI GetCursorPos Cur MouseDC.MDC = WindowFromPoint(Cur.x, Cur.y) MouseDC.ponX = Cur.x MouseDC.ponY = Cur.y End Function
楼主写完贴出来,大家共享先!我还不会SHELLWINDOWS呢!
Option ExplicitPrivate Sub Command1_Click() '向上用 Dim Arr() As String Dim i As Long Dim sRet As String Dim Str As String Str = Text1.Text If Right(Str, 1) = "\" Then Str = Left(Str, Len(Str) - 1) Arr = Split(Str, "\") For i = LBound(Arr) To UBound(Arr) - 1 If Len(Arr(i)) > 0 Then sRet = sRet & Arr(i) & "\" End If Next MsgBox sRet End SubPrivate Sub Timer1_Timer() 'timer1.Interval=3000,后退用 SendKeys Chr(8) End Sub
'一个按钮,两个LISTBOX。 '请引用 Microsoft Internet Controls Private Sub Command1_Click() Dim objIE As InternetExplorer Dim dWinFolder As New ShellWindows
For Each objIE In dWinFolder List1.AddItem objIE.hWnd '为什么这一句会出现“自动化错误”呢?你们测试一下会不会? List2.AddItem objIE.LocationURL NextEnd Sub如果把那一句去掉就正常????为什么呢?
Option Explicit Dim MyWin As ShellWindowsPrivate Sub Form_Load() Set MyWin = New ShellWindows Timer1.Interval = 1000 End SubPrivate Sub Form_Unload(Cancel As Integer) Set MyWin = Nothing End SubPrivate Sub Timer1_Timer() If MyWin.Count = 0 Then Exit Sub Dim i As Long For i = 0 To MyWin.Count - 1 If WndToProcId(MyWin(i).hwnd) = WndToProcId(MouseDC.MDC) Then Dim Str As String Str = Right(MyWin(i).LocationURL, Len(MyWin(i).LocationURL) - 8) Text1.Text = Replace(Str, "/", "\") End If Next End Sub ------------------------------ Option Explicit Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Public Declare Function WindowFromPoint Lib "user32" (ByVal x As Long, ByVal y As Long) As Long Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Public Type POINTAPI x As Long y As Long End TypePublic Type DGfor3 ponX As Long ponY As Long MDC As Long End TypePublic Function MouseDC() As DGfor3 On Error Resume Next Dim Cur As POINTAPI GetCursorPos Cur MouseDC.MDC = WindowFromPoint(Cur.x, Cur.y) MouseDC.ponX = Cur.x MouseDC.ponY = Cur.y End FunctionPublic Function TitleToWnd(ByVal strTitle As String, Optional strClassName As String = vbNullString) As Long TitleToWnd = FindWindow(strClassName, strTitle) End FunctionPublic Function WndToProcId(ByVal hwnd As Long) As Long GetWindowThreadProcessId hwnd, WndToProcId End Function --------------- FORM1中有一个TEXT、TIMER。
我的思路遇到难题了!!'一个按钮,两个LISTBOX。 '请引用 Microsoft Internet Controls Private Sub Command1_Click() Dim objIE As InternetExplorer Dim dWinFolder As New ShellWindows
For Each objIE In dWinFolder List1.AddItem objIE.hWnd '为什么这一句会出现“自动化错误”呢?你们测试一下会不会? List2.AddItem objIE.LocationURL NextEnd Sub如果把那一句去掉就正常????为什么呢?不知道该如何解决》
For i = 0 To MyWin.Count - 1 If WndToProcId(MyWin(i).hwnd) = WndToProcId(MouseDC.MDC) Then Dim Str As String Str = Right(MyWin(i).LocationURL, Len(MyWin(i).LocationURL) - 8) Text1.Text = Replace(Str, "/", "\") End If Next -------------- 我这句怎么就行呢,楼主没有看吗?
参考http://weblogs.asp.net/oldnewthing/archive/2004/07/20/188696.aspx
Dim sName As String
sName = Space(250)
GetClassName &H80354, sName, 250
sName = Left(sName, InStr(sName, vbNullChar) - 1)
If sName = "ExploreWClass" Then
...
End If
End Sub
......
GetClassName the_HWND_you_got, sName, 250
......
------------------------
FORM:
Option ExplicitPrivate Sub Form_Load()
Timer1.Interval = 100
End SubPrivate Sub Timer1_Timer()
Dim hWnd1 As Long
Dim hWnd2
Dim Cur As POINTAPI
GetCursorPos Cur
hWnd1 = WindowFromPoint(Cur.x, Cur.y)
hWnd2 = GetParent(hWnd1)
If hWnd2 = 0 Then hWnd2 = hWnd1 EnumChildWindows GetParent(hWnd2), AddressOf WndEnumChildProc, 0&
End Sub
---------------------------
MODULE:
Option ExplicitPublic Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) 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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd 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 EnumChildWindows Lib "user32" (ByVal hwndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long
Public Declare Function WindowFromPoint Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPublic Const WM_GETTEXT = &HD
Public Const WM_GETTEXTLENGTH = &HEPublic Type POINTAPI
x As Long
y As Long
End TypePublic Function WndEnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim bRet As Long
Dim myStr As String * 50
bRet = GetClassName(hWnd, myStr, 50)
If bRet <> 0 Then
If UCase(Left(myStr, 4)) = "EDIT" Then Debug.Print GetText(hWnd)
End If
WndEnumChildProc = 1
End FunctionFunction GetText(ByVal hWnd As Long) As String
Dim Textlen As Long
Dim Text As String Textlen = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0)
If Textlen = 0 Then Exit Function
Textlen = Textlen + 1
Text = Space(Textlen)
Textlen = SendMessage(hWnd, WM_GETTEXT, Textlen, ByVal Text)
GetText = Left(Text, Textlen)
End Function
用API GetParent也不行。
首先,枚举所有的SHELLWINDOWS窗口的句柄,再得到当前鼠标指向的窗口的句柄(HWND),然后将鼠标指向的窗口的句柄与枚举的窗口的句柄进行比较,找到则说明该窗口是IE或资源管理器窗口。再用将该对象赋给临时的INTENNETEXPLORER对象,就可以对其进行操作了。比如tWindow.GOBACK(后退)。 大家觉得如何??但我觉得如果能实现的话,好象速度会很慢的,而且必须时刻枚举。另“枚举所有的SHELLWINDOWS窗口的句柄”的时候遇到了点问题,等会再说。
-----------------
Option ExplicitPublic Declare Function WindowFromPoint Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Type POINTAPI
x As Long
y As Long
End TypePublic Type DGfor3
ponX As Long
ponY As Long
MDC As Long
End TypePublic Function MouseDC() As DGfor3
On Error Resume Next
Dim Cur As POINTAPI
GetCursorPos Cur
MouseDC.MDC = WindowFromPoint(Cur.x, Cur.y)
MouseDC.ponX = Cur.x
MouseDC.ponY = Cur.y
End Function
Dim Arr() As String
Dim i As Long
Dim sRet As String
Dim Str As String
Str = Text1.Text
If Right(Str, 1) = "\" Then Str = Left(Str, Len(Str) - 1)
Arr = Split(Str, "\")
For i = LBound(Arr) To UBound(Arr) - 1
If Len(Arr(i)) > 0 Then
sRet = sRet & Arr(i) & "\"
End If
Next
MsgBox sRet
End SubPrivate Sub Timer1_Timer() 'timer1.Interval=3000,后退用
SendKeys Chr(8)
End Sub
'请引用 Microsoft Internet Controls
Private Sub Command1_Click() Dim objIE As InternetExplorer
Dim dWinFolder As New ShellWindows
For Each objIE In dWinFolder List1.AddItem objIE.hWnd '为什么这一句会出现“自动化错误”呢?你们测试一下会不会? List2.AddItem objIE.LocationURL
NextEnd Sub如果把那一句去掉就正常????为什么呢?
Dim MyWin As ShellWindowsPrivate Sub Form_Load()
Set MyWin = New ShellWindows
Timer1.Interval = 1000
End SubPrivate Sub Form_Unload(Cancel As Integer)
Set MyWin = Nothing
End SubPrivate Sub Timer1_Timer()
If MyWin.Count = 0 Then Exit Sub
Dim i As Long
For i = 0 To MyWin.Count - 1
If WndToProcId(MyWin(i).hwnd) = WndToProcId(MouseDC.MDC) Then
Dim Str As String
Str = Right(MyWin(i).LocationURL, Len(MyWin(i).LocationURL) - 8)
Text1.Text = Replace(Str, "/", "\")
End If
Next
End Sub
------------------------------
Option Explicit
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function WindowFromPoint Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Type POINTAPI
x As Long
y As Long
End TypePublic Type DGfor3
ponX As Long
ponY As Long
MDC As Long
End TypePublic Function MouseDC() As DGfor3
On Error Resume Next
Dim Cur As POINTAPI
GetCursorPos Cur
MouseDC.MDC = WindowFromPoint(Cur.x, Cur.y)
MouseDC.ponX = Cur.x
MouseDC.ponY = Cur.y
End FunctionPublic Function TitleToWnd(ByVal strTitle As String, Optional strClassName As String = vbNullString) As Long
TitleToWnd = FindWindow(strClassName, strTitle)
End FunctionPublic Function WndToProcId(ByVal hwnd As Long) As Long
GetWindowThreadProcessId hwnd, WndToProcId
End Function
---------------
FORM1中有一个TEXT、TIMER。
'请引用 Microsoft Internet Controls
Private Sub Command1_Click() Dim objIE As InternetExplorer
Dim dWinFolder As New ShellWindows
For Each objIE In dWinFolder List1.AddItem objIE.hWnd '为什么这一句会出现“自动化错误”呢?你们测试一下会不会? List2.AddItem objIE.LocationURL
NextEnd Sub如果把那一句去掉就正常????为什么呢?不知道该如何解决》
If WndToProcId(MyWin(i).hwnd) = WndToProcId(MouseDC.MDC) Then
Dim Str As String
Str = Right(MyWin(i).LocationURL, Len(MyWin(i).LocationURL) - 8)
Text1.Text = Replace(Str, "/", "\")
End If
Next
--------------
我这句怎么就行呢,楼主没有看吗?
非常感谢你的帮助。你的代码我也试了。但是VB老是提示错误。
运行到“If WndToProcId(MyWin(i).hwnd) = WndToProcId(MouseDC.MDC) Then”中的“MyWin(i).hwnd”就提示:对象HWND的方法‘IWebBrowser2’失败。晕啊!还有我说的代码中出现“objIE.hWnd ”也失败啊!真是搞不懂。
唉,结帖,另开新帖!