CodeItBetter Programming Another VB Programming Blog

How to display what URL Internet Explorer/Netscape is displaying

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'Internet - How to display what URL Internet Explorer/Netscape is displaying

'Add a Command Button and a Label to your form.

Option Explicit
 
Public Type ProcData
    AppHwnd As Long
    title As String
    Placement As String
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, _
    ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, _
    ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Private Const GW_HWNDFIRST = 0
 
Public Function EditInfo(window_hwnd As Long) As String
    Dim txt As String, buf As String
    Dim buflen As Long, child_hwnd As Long, children() As Long
    Dim num_children As Integer, I As Integer
    buflen = 256
    buf = Space$(buflen - 1)
    buflen = GetClassName(window_hwnd, buf, buflen)
    buf = Left$(buf, buflen)
    'Check whether it is an Edit object.
    If buf = "Edit" Then
        EditInfo = WindowText(window_hwnd)
        Exit Function
    End If
    'If it is not an Edit object, Search the children. Make a list of the child windows.
    num_children = 0
    child_hwnd = GetWindow(window_hwnd, GW_CHILD)
    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
    'Get info of the child windows.
    For I = 1 To num_children
        txt = EditInfo(children(I))
        If txt <> "" Then Exit For
    Next I
    EditInfo = txt
End Function
 
'Return the text associated with the window.
Public Function WindowText(window_hwnd As Long) As String
    Dim txtlen As Long
    Dim txt As String
    WindowText = ""
    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)
    WindowText = Left$(txt, txtlen)
End Function
 
Public Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean
    Dim buf As String * 1024
    Dim title As String
    Dim length As Long
    'Get the window's title.
    length = GetWindowText(app_hwnd, buf, Len(buf))
    title = Left$(buf, length)
    If Right$(title, 30) = " - Microsoft Internet Explorer" Then
        'This is it. Find the ComboBox information.
        Form1.Label1 = EditInfo(app_hwnd)
        'Stop searching.
        EnumProc = 0
    Else
        'Continue searching til find it.
        EnumProc = 1
    End If
End Function
 
Private Sub Command1_Click()
    Label1.Caption = ""
    EnumWindows AddressOf EnumProc, 0
End Sub
Filed under: Internet Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.