I. The core of the procedure
The core of this program is to obtain the window handle through the API function and obtain the URL that the browser accesses, on this basis can realize the remote monitoring and management with Winsock control.
1. First create a project and in the window Form1, and declare the following four API functions and two constants:
Option Explicit Private Declare Function FindWindow lib″user32″alias″findwindowa″ (ByVal lpcl assname as String, ByVal Lpwindowname as String) as Long
The function of the ′findwindow function is to find the handle of the URL address of the currently running IE window
Private Declare Function SendMessage Lib″user32″alias″sendmessagea″ (ByVal hwnd as Long, ByVal wmsg as Long, ByVal WPar Am as long, lParam as long) as long
The function of the ′sendmessage function is to send a message to the operating system
Private Declare Function FindWindowEx Lib″user32″alias″findwindowexa″ (ByVal hWnd1 as Long,byval hWnd2 as Long, ByVal l Psz1 As String, ByVal lpsz2 as String) as Long
The function of the ′findwindowex function is to find the handle of the subform
Private Declare Function sendmessagebystring Lib″user32″alias″sendmessagea″ (ByVal hwnd as Long, ByVal wmsg as Long, by Val WParam as Long, ByVal LParam as String) as Long
Private Const Wm_gettext = &hd
Private Const wm_gettextlength = &he
2. Add command control on the form and name it geturlstring, click the command button and add the following program code to it:
Private Sub Geturlstring_click ()
On Error GoTo Callerrora
Dim Sclassname as string′ set a string variable, which is a class variable
Dim Lhwnd as long′ set a long shaping variable to receive the function return value
Dim WindowHandle as long′ set a long shaping variable to receive the return handle of the function
Lhwnd = 0
Sclassname = (″ieframe″)
Lhwnd = FindWindowEx (Lhwnd, 0, Sclassname, vbNullString) to obtain the handle of the URL address bar and get the handle of the IE window
Sclassname = (″workera″)
Lhwnd = FindWindowEx (Lhwnd, 0, Sclassname, vbNullString) to get the handle to the workspace of the IE window
Sclassname = (″rebarwindow32″)
Lhwnd = FindWindowEx (Lhwnd, 0, Sclassname, vbNullString) to get the handle to the menu bar of the IE window
Sclassname = (″comboboxex32″)
Lhwnd = FindWindowEx (Lhwnd, 0, Sclassname, vbNullString) to get the handle to the dropdown menu of IE window
Sclassname = (″combobox″)
Lhwnd = FindWindowEx (Lhwnd, 0, Sclassname, vbNullString) The handle to the current item of the Drop-down menu for the IE window
Sclassname = (″edit″)
Lhwnd = FindWindowEx (Lhwnd, 0, Sclassname, vbNullString) to get the edit box handle to this Drop-down menu
WindowHandle = lhwnd′ receive the handle we want now
Dim buffer as string′ set string variable to receive the current string
Dim TextLength as long′ set long shaping variable receive string length
TextLength = SendMessage (WindowHandle, Wm_gettextlength, 0&, 0&) to the system send the string length command in the address bar of the IE window
Buffer = String (textlength, 0)
Call Sendmessagebystring (WindowHandle, Wm_gettext, TextLength + 1, buffer) send a string command to the system to get the IE form Address bar
If Buffer =″″then
Msgbox″microsoft InternetExplorer Browser is not running. ″, vbOKOnly
Else
MsgBox Buffer′ie Displays the current URL at run time
End If
Exit Sub
Callerrora:
MsgBox Err.Description
Err.Clear
End Sub