|
|
Title | See what URL Netscape is displaying |
Description | This example shows how to see what URL Netscape is displaying in Visual Basic 6. It uses the EnumWindows and GetWindowText API functions to find the browser and then examines its controls to find the URL. |
Keywords | Netscape, URL, browser |
Categories | Internet |
|
|
The main program uses the following statement to enumerate the system's active windows.
EnumWindows AddressOf EnumProc, 0
The EnumProc routine looks for a window with title ending in " -Netscape." If it finds such a window, the routine uses the EditInfo function to get the text in the window's Edit control child. It displays this text in a label and ends the listing of windows.
|
|
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)
' See if the title ends with " - Netscape".
If Right$(title, 11) = " - Netscape" Then
' This is it. Find the ComboBox information.
frmWindowList.lblAddress = EditInfo(app_hwnd)
' Stop searching.
EnumProc = 0
Else
' Continue searching til find it.
EnumProc = 1
End If
End Function
|
|
Subroutine EditInfo examines a window. If the window is an Edit control, the routine calls function WindowText to return the control's text. If the control is not an Edit control, the routine recursively calls itself for the control's children, looking for an Edit control.
|
|
Public Function EditInfo(window_hwnd As Long) As String
Dim txt 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
' Get the class name.
buflen = 256
buf = Space$(buflen - 1)
buflen = GetClassName(window_hwnd, buf, buflen)
buf = Left$(buf, buflen)
' See if we found an Edit object.
If buf = "Edit" Then
EditInfo = WindowText(window_hwnd)
Exit Function
End If
' It's 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 information on the child windows.
For i = 1 To num_children
txt = EditInfo(children(i))
If txt <> "" Then Exit For
Next i
EditInfo = txt
End Function
|
|
Subroutine WindowText uses the SendMessage API function to send the WM_GETTEXTLENGTH message to a control to get the length of its text. It allocates space and then sends the WM_GETTEXT message to get the text.
|
|
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
|
|
|
|
|
|